导语:需要在关机和重启前执行一下关机前的脚本。避免某些服务没有正常关闭导致的问题,或者某些服务关闭慢的问题。
创建/lib/systemd/system/cleanup.service
[Unit]
Description=Run command at shutdown
# 假设要执行的命令依赖网络
Requires=network.target
DefaultDependencies=no
Before=shutdown.target reboot.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/bin/bash /srv/t.1.sh
#ExecStop=<要执行的命令>(/bin/touch /home/snail/hello)
[Install]
WantedBy=multi-user.target
应用服务
systemctl daemon-reload
systemctl enable cleanup.service
脚本/srv/t.1.sh
如下
#!/bin/bash
source /etc/profile
date >> /srv/date.txt
可参考
https://blog.csdn.net/weixin_44832060/article/details/115466429