I'm working on Ubuntu 16.04.3 and I need to create a file at the OS shutdown time.
After reading this link: https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown
I created a file named test.service
at /lib/systemd/system/
and here is the content:
[Unit]
Description=test
[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/home/test.sh
[Install]
WantedBy=multi-user.target
Here is /home/test.sh
:
#!/bin/bash
touch /home/myfile
However, after rebooting the system, I cannot see any file named /home/myfile
.
You are missing the line
RemainAfterExit=true
, so your service stops after executingtrue
. Add it to the [Service] section and it should work.Also you should rather put your unit files into
/etc/systemd/system/
.