Linux使用systemctl方式设置agent开机自启动(ubuntu、centos、debian等都可以用)

如果是windows开机自启动服务,请查看将agent注册为windows系统服务
1、以Ubuntu 18为例,新建/etc/systemd/system/wgcloudAgent.service文件,
[root@localhost~]# vim /etc/systemd/system/wgcloudAgent.service
内容如下,将下列内容复制到/etc/systemd/system/wgcloudAgent.service文件
[Unit]
Description=auto Start Wgcloud Agent
After=network.target syslog.target

[Service]
Type=forking
ExecStart=/etc/wgcloudAgent.local 
    
[Install]
WantedBy=multi-user.target
2、创建文件/etc/wgcloudAgent.local
[root@localhost~]# vim /etc/wgcloudAgent.local
内容如下
#!/bin/sh -e
# 
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/usr/local/wgcloud-v3.3.5/agent/start.sh
exit 0
注意:/usr/local/wgcloud-v3.3.5/agent/start.sh改为自己的agent实际部署路径
3、给/etc/wgcloudAgent.local加可执行权限
[root@localhost~]# chmod +x /etc/wgcloudAgent.local
提示,此时可以手动测试下,是否能成功执行启动,先把agent关闭再测试:
[root@localhost~]# /etc/wgcloudAgent.local
4、最后两步,重新载入systemd服务和设置开机自启动,完成
[root@localhost~]# systemctl daemon-reload
[root@localhost~]# systemctl enable wgcloudAgent
5、server开机启动,和agent操作一致,只需要把/etc/wgcloudAgent.local中的启动脚本/usr/local/wgcloud-v3.3.5/agent/start.sh换成server的start.sh路径即可
如果server和agent在一个主机上,也可以把两个start.sh分先后,都写到/etc/wgcloudAgent.local中
#!/bin/sh -e
# 
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
/wgcloud/wgcloud-v3.4.1/server/start.sh
/wgcloud/agent-linux-amd64-v3.4.1/start.sh
exit 0
注意,如果server不能开机启动成功,查看说明
如果数据库和server在同一个主机,也可以把启动数据库服务的命令,放到/wgcloud/wgcloud-v3.4.1/server/start.sh之前
因为server启动依赖数据库,所以先启动数据库服务