简体   繁体   English

为什么自动运行服务在关机时无法执行?

[英]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? 重新启动后没有/home/test_sys/shutdown.txt,为什么我的服务无法执行?

I have rewrite the /lib/systemd/system/test_shutdown.service as following. 我已经按照以下方式重写了/lib/systemd/system/test_shutdown.service。

[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. systemd需要一条绝对路径。

You haven't gave the systemctl status of your first code. 您尚未提供第一个代码的systemctl status It could be write permission issue, or may be your shell interpreter is not /bin/bash . 可能是写权限问题,或者您的Shell解释器不是/bin/bash Might better use shebang #!/bin/sh in the shell script. 最好在shell脚本中使用shebang #!/bin/sh

[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 journalctl | grep test_shutdown.service
Apr 01 15:05:54 hwy systemd[1]: Configuration file /lib/systemd/system/test_shutdown.service is marked executable. Apr 01 15:05:54 hwy systemd [1]:配置文件/lib/systemd/system/test_shutdown.service被标记为可执行。 Please remove executable permission bits. 请删除可执行权限位。 Proceeding anyway. 仍要继续。

chmod 644 /lib/systemd/system/test_shutdown.service chmod 644 /lib/systemd/system/test_shutdown.service

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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