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
原文地址:https://www.cnblogs.com/welhzh/p/9623810.html