标签:dep systemd def esc 实验 enable spawn services you
注:实验发现关机及其重启后执行的脚本的脚本中不能嵌套调用其它脚本。
该脚本需要应该与服务程序中ExecStart项相对应。及该脚本名为/home/lhx/reboot.sh
且需要安装expectyum install expect
vim /home/lhx/reboot.sh
#!/bin/bash
passwd="123456"
echo "shutdown or reboot at `date +‘%Y%m%d%H%M‘`" >> /home/redflag/mock/reboot.log
/usr/bin/expect <<-EOF
spawn rsync -arv /home/redflag/mock/ root@192.168.30.200:/home/liuhuaxian/bak/mock
expect "*password"
send "123456\n"
expect eof
EOF
vi /usr/lib/systemd/system/rebootSrv.service
[Unit]
Description=close services before reboot and shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
[Service]
Type=oneshot
ExecStart=/home/lhx/reboot.sh #your path and filename
[Install]
WantedBy=halt.target reboot.target shutdown.target
然后执行
[root@localhost lhx]# systemctl enable rebootSrv.service
Created symlink from /etc/systemd/system/halt.target.wants/rebootSrv.service to /usr/lib/systemd/system/rebootSrv.service.
Created symlink from /etc/systemd/system/reboot.target.wants/rebootSrv.service to /usr/lib/systemd/system/rebootSrv.service.
Created symlink from /etc/systemd/system/shutdown.target.wants/rebootSrv.service to /usr/lib/systemd/system/rebootSrv.service.
重启关机后就能自动执行/home/lhx/reboot.sh
该脚本。
标签:dep systemd def esc 实验 enable spawn services you
原文地址:https://www.cnblogs.com/DXGG-Bond/p/13356684.html