Running a job before shutting down
Let's say that you want to have a job automatically run every time you shut down your computer. (I'll let you use your imagination about what kind of job that could be.) To set that up, just create your own custom service that's WantedBy
the
shutdown.target
. Let's check out how.
We'll demonstrate this by creating a dummy shell script that goes along with our new service. In the /usr/local/bin/
directory, create the script.sh
file with the following contents:
#!/bin/bash # Run script with systemd only at shutdown, and not for reboot. systemctl list-jobs | egrep -q 'reboot.target.*start' && echo "Testing myscript.service for reboot" > /root/reboot_test.txt systemctl list-jobs | egrep -q 'shutdown.target.*start' && echo "Testing myscript.service for shutdown" > /root/shutdown_test.txt
The first systemctl list-jobs
command will search through the list of running...