Reputation: 348
cat /lib/systemd/system/test_shutdown.service
[Unit]
Description=hello world
Before=poweroff.target shutdown.target reboot.target
[Service]
Type=oneshot
ExecStart=/bin/bash /home/test_shutdown.sh
[Install]
WantedBy=poweroff.target shutdown.target reboot.target
cat /home/test_shutdown.sh
echo "haha" > /home/test_sys/shutdown.txt
ls -al /home/test_shutdown.sh
-rwxrwxrwx 1 root root 42 Mar 31 19:08 /home/test_shutdown.sh
ls -al /home/test_sys
drwxrwxrwx 2 root root 4096 Mar 31 19:12
drwxr-xr-x 14 root root 4096 Mar 31 19:08
systemctl enable test_shutdown.service
No /home/test_sys/shutdown.txt after rebooting,why my service can't be executed?
I have rewrite the /lib/systemd/system/test_shutdown.service as following.
[Unit]
Description=hello world
DefaultDependencies=no
Before=reboot.target poweroff.target shutdown.target
[Service]
ExecStart=echo "haha" > /home/test_sys/test.txt
Type=oneshot
[Install]
WantedBy=reboot.target poweroff.target shutdown.target
systemctl status test_shutdown.service
test_shutdown.service - hello world
Loaded: error (Reason: Invalid argument)
Active: inactive (dead)
Upvotes: 0
Views: 144
Reputation: 1093
In your second service file, it should be
ExecStart=/bin/echo "haha" > /home/test_sys/test.txt
systemd need an absolute path.
You haven't gave the systemctl status
of your first code. It could be write permission issue, or may be your shell interpreter is not /bin/bash
. Might better use shebang #!/bin/sh
in the shell script.
Upvotes: 1
Reputation: 348
journalctl |grep test_shutdown.service
Apr 01 15:05:54 hwy systemd[1]: Configuration file /lib/systemd/system/test_shutdown.service is marked executable. Please remove executable permission bits. Proceeding anyway.
chmod 644 /lib/systemd/system/test_shutdown.service
Upvotes: 0
Reputation: 24
[Unit]
Description=hello world
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
[Service]
ExecStart=/bin/bash /home/test_shutdown.sh
Type=oneshot
Upvotes: 0