L O A D I N G
«

Centos7 重启或关机前执行脚本

童话里,我们在一起 发布于 阅读:524 shell


为了实现在 Centos7 环境下重启前执行某些操作,在网上也找了许多资料。但是大多是以 Centos6 为教程,修改chkconfig 和 rc0.d, rc6.d 的内容。在 centos7 中已经不适用了。

本文是以使用 systemctl 的方式来自定义关机前执行脚本。

1.创建 systemd 服务

vim /usr/lib/systemd/system/shutdown-clean.service

创建该文件并写入如下内容,需要把 ExecStart=/some/scripts/com/monitor.sh 这一行换成自己的脚本路径

[Unit]Description=close services before reboot and shutdownDefaultDependencies=noBefore=shutdown.target reboot.target halt.target#Before=network.target iscsi.service iscsid.service shutdown.target reboot.target halt.target# This works because it is installed in the target and will be executed before the # target state is entered# Also consider kexec.target[Service]Type=oneshotExecStart=/some/scripts/com/monitor.sh[Install]WantedBy=halt.target reboot.target shutdown.target

2.启动服务

执行

systemctl enable shutdown-clean.service

关机和重启前,就会自动执行你自定义的脚本


扫描二维码,在手机上阅读

推荐阅读:

收藏