配置自定义的系统服务(自定义启动程序以名为askpcbvnc的程式为例)
1.编写自动执行脚本askpcbvnc.sh:
#!/bin/sh
export ASK_PCB_VNC_DIR=/home/incam # 设置程式安装目录环境变量
export LANG=en_US.UTF-8 # 设置语言环境
export LC_ALL=en_US.UTF-8 # 覆盖所有其他区域设置变量
case "$1" in
'start')
if [ -x $ASK_PCB_VNC_DIR ]; then
cd $ASK_PCB_VNC_DIR
echo '启动askpcbvnc服务中...'
for i in {10..30}; do
./askpro5new $i &
done
wait
fi
;;
'stop')
echo '终止askpcbvnc服务中...'
pkill -f askpro5new
ps -ef | grep 'Xvnc -depth 24' | grep -v grep | awk '{print $2}' | xargs kill -9
;;
'restart')
$0 stop
$0 start
;;
'status')
if pgrep -f askpcbvnc.sh > /dev/null; then
echo "askpcbvnc服务正在运行中."
else
echo "askpcbvnc服务异常."
fi
;;
*)
echo "例句: /etc/init.d/askpcbvnc {start|stop|restart|status}"
exit 1
;;
esac
exit 0
2.保存脚本文件到"/etc/init.d"目录并赋予执行权限:
cd /etc/init.d
chmod +x askpcbvnc.sh
注意事项:
1.代码主要调用askpro5new可执行文件,文件路径:/home/incam/askpro5new
3.创建systemd服务文件(/etc/systemd/system/askpcbvnc.service):
直接在root用户终端执行以下指令
vim /etc/systemd/system/askpcbvnc.service
将以下代码写入"/etc/systemd/system/askpcbvnc.service"文件并保存
[Unit]
Description=Ask PCB VNC Service
After=network.target
[Service]
# User=incam
Environment="HOME=/home/incam"
Type=simple
ExecStart=/bin/sh /etc/init.d/askpcbvnc.sh start
ExecStop=/bin/sh /etc/init.d/askpcbvnc.sh stop
User=incam
Group=incam
WorkingDirectory=/home/incam
Restart=on-failure
[Install]
WantedBy=multi-user.target
4.配置Centos7系统服务askpcbvnc.service
启用服务
运行以下命令以启用并启动服务:
# 重新加载 systemd 管理器配置
sudo systemctl daemon-reload
# 启动服务
sudo systemctl start askpcbvnc.service
# 设置开机自启
sudo systemctl enable askpcbvnc.service
5.管理服务
- 查看状态:
systemctl status askpcbvnc.service
- 停止服务:
systemctl stop askpcbvnc.service
- 重启服务:
systemctl restart askpcbvnc.service