linux服务开机自启动之二(forking方式)
linux服务开机自启动之二(forking方式)
start.sh脚本
#!/bin/bashLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib:../lib;export LD_LIBRARY_PATH
HOME=/home/gb;export HOMEif [ -x ${HOME}/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr ]
thenif [ "`ps -ef | grep USB-Device-Mgr | grep -v grep | awk '{print $NF}' | cut -d'/' -f7 `" != "USB-Device-Mgr " ]thenwhile truedoif [ "`ifconfig -a | grep 'ether' | grep -v grep | awk '{print $1}'`" != "ether" ]thenecho "network is not ok"sleep 120elsebreakfidonesleep 60echo "Start USB-Device-Mgr ..."nohup ${HOME}/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr > nohup.out 2>&1 &fi
elseecho "${HOME}/USB-Device-Mgr/USB-Device-Mgr/bin/USB-Device-Mgr is not executed"
fi
服务脚本
root@gb:/etc/systemd/system# vim gb_usbdevicemgr.service [Unit]
Description=My Service
After=network.target
After=network-online.target
Wants=network-online.target[Service]
ExecStart=/home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/run.sh
WorkingDirectory=/home/gb/USB-Device-Mgr/USB-Device-Mgr/bin/
Restart=always
User=root
RestartSec=30s
TimeoutStartSec=600s[Install]
WantedBy=multi-user.target
参考文献
systemctl服务文件管理指南
Linux Redis 服务设置开机自启动