美文网首页Yii2程序员
使用systemd在系统关闭时运行脚本

使用systemd在系统关闭时运行脚本

作者: 方圆百里找对手 | 来源:发表于2017-11-06 11:29 被阅读45次

假设您已经创建好了脚本,并且测试其运行无误。那么,如下步骤可以让您使用systemd在系统关闭时运行脚本。
首先,在 /etc/systemd/system 下创建一个文件 run-script-when-shutdown.service,并且让其内容如下

[Unit]
Description=service to run script when shutdown
After=syslog.target network.target

[Service]
Type=simple
ExecStart=/bin/true
ExecStop=path_to_script_to_run
RemainAfterExit=yes
[Install]
WantedBy=default.target

而后,执行如下命令,使能新创建的服务

systemctl enable run-script-when-shutdown
systemd start run-script-when-shutdown

为了便于调整,您可以配置run-script-when-shutdown运行固定的脚本。需要的时候,相关人员可以修改这个您固定的脚本。正如/etc/rc.local的工作方式。
当您不再需要运行这个服务时,您可以这样操作

systemctl disable run-script-when-shutdown

如此操作后您甚至可以删掉这个文件。

相关文章

网友评论

    本文标题:使用systemd在系统关闭时运行脚本

    本文链接:https://www.haomeiwen.com/subject/eizkmxtx.html