linux关机时候执行命令脚本或程序

Write a service file and place it in /etc/systemd/system/beforeshuttingdown.service

code:

[Unit]
Description=Run mycommand at shutdown
Requires=network.target
DefaultDependencies=no
Before=shutdown.target reboot.target
 
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=mycommand [or path to script or program]

[Install]
WantedBy=multi-user.target

Your program or script must be executable. 脚本或者程序(mycommand)必须是可执行文件。

启用服务:

systemctl daemon-reload
systemctl enable beforeshuttingdown.service
systemctl start beforeshuttingdown.service

 

posted @ 2018-09-10 22:56  微信公众号--共鸣圈  阅读(534)  评论(0编辑  收藏  举报