配置linux centos7.5系统 关机前执行指定脚本

为了实现在关机前执行脚本 在网上查了很久都没找到解决方法。

最后还是Google 帮了忙。

参考了以下链接:

https://unix.stackexchange.com/questions/39226/how-to-run-a-script-with-systemd-right-before-shutdown

https://opensource.com/life/16/11/running-commands-shutdown-linux

最终在centos7.5系统中配置成功。

以下是配置方法:

vim /etc/systemd/system/my-powerdown.service

输入下面的参数

[Unit]
Description= run my scripts only poweroff

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=<your script/program>

[Install]
WantedBy=poweroff.target halt.target

保存退出。

执行命令:

systemctl start my-powerdown.service

启动刚写好的服务。

systemctl status my-powerdown.service

查看服务状态,显示:

代表服务启动成功。

由于本服务指定了只执行一次。所以运行 enable 服务 不起作用。

可以将启动服务的命令加到开机执行脚本中。

这样就保证了服务一直是开启状态。

保证你设置的脚本能够在关机前正常运行。

我这里测试都是正常的。

 

posted @ 2018-09-29 16:35  Ray_lei  阅读(2524)  评论(0编辑  收藏  举报