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
4、最后两步,重新载入systemd服务和设置开机自启动,完成
[root@localhost~]# systemctl daemon-reload
[root@localhost~]# systemctl enable wgcloudAgent
5、server开机启动,和agent操作一致,只需要把/etc/wgcloudAgent.local中的启动脚本换成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/log下输出启动日志,那么可能是开机时候JDK环境变量未准备好,
这时候可以把server/start.sh中的java改为JDK绝对路径
server/start.sh修改前如下
nohup java  -server -Dloader.path=./lib  -Xms256m -Xmx512m  -jar $RUN_NAME >/dev/null 2>&1 &
修改为如下,其中/java/jdk1.8.0_152为jdk的实际路径
nohup /java/jdk1.8.0_152/bin/java  -server -Dloader.path=./lib  -Xms256m -Xmx512m  -jar $RUN_NAME >/dev/null 2>&1 &
再试试吧