centos7 使用systemd 自定义关机前脚本

时间:2023-03-09 16:18:43
centos7 使用systemd 自定义关机前脚本

systemd (centos7)

需求,关机前执行脚本

关机脚本
vi /usr/bin/shutdown_cust.sh
#!/bin/bash
echo "zhengchangguanji" >> /tmp/log.log

chmod +x /usr/bin/shutdown_cust.sh
设置关机执行

[root@localhost ~]# vi /usr/lib/systemd/system/cust_power.service
[Unit]
Description=poweroff cust
After=getty@tty1.service display-manager.service plymouth-start.service
Before=systemd-poweroff.service
DefaultDependencies=no

[Service]
ExecStart=/usr/bin/shutdown_cust.sh
Type=forking
[Install]
WantedBy=poweroff.target

[root@localhost ~]# vi /usr/lib/systemd/system/cust_reboot.service
[Unit]
Description=reboot cust
After=getty@tty1.service display-manager.service plymouth-start.service
Before=systemd-reboot.service
DefaultDependencies=no

[Service]
ExecStart=/usr/bin/shutdown_cust.sh
Type=forking
[Install]
WantedBy=reboot.target

[root@localhost ~]# vi /usr/lib/systemd/system/cust_halt.service
[Unit]
Description=halt cust
After=getty@tty1.service display-manager.service plymouth-start.service
Before=systemd-halt.service
DefaultDependencies=no

[Service]
ExecStart=/usr/sbin/shutdown_cust.sh
Type=forking

[Install]
WantedBy=halt.target

软连接
ln -s /usr/lib/systemd/system/cust_halt.service /usr/lib/systemd/system/halt.target.wants/
ln -s /usr/lib/systemd/system/cust_power.service /usr/lib/systemd/system/power.target.wants/
ln -s /usr/lib/systemd/system/cust_reboot.service /usr/lib/systemd/system/reboot.target.wants/

重启 查看tmp下面会不会多出来log.log 文件