简体   繁体   中英

Why the autorun service can't execute at poweroff?

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)

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.

[Unit]
Description=hello world
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
ExecStart=/bin/bash /home/test_shutdown.sh
Type=oneshot

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM