繁体   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    

重新启动后没有/home/test_sys/shutdown.txt,为什么我的服务无法执行?

我已经按照以下方式重写了/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)

在您的第二个服务文件中,它应该是

ExecStart=/bin/echo "haha" > /home/test_sys/test.txt

systemd需要一条绝对路径。

您尚未提供第一个代码的systemctl status 可能是写权限问题,或者您的Shell解释器不是/bin/bash 最好在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
Apr 01 15:05:54 hwy systemd [1]:配置文件/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