第一步:新建服务启动脚本
1、在/usr/local/src/cbs/cbssh/目录下创建启动脚本cbs-web-service-start.sh
#!/bin/sh
#rt JAVA_HOME=/usr/local/software/jdk
#export PATH=$JAVA_HOME/bin:$PATH
nohup java -jar /usr/local/src/cbs/cbsjars/cbsweb.jar > /usr/local/src/cbs/cbslogs/cbs-web-service.log 2>&1 &
echo $! i> /var/run/cbs-web-service.pid
2、在/usr/local/src/cbs/cbssh/目录下创建停止脚本cbs-web-service-stop.sh
#!/bin/sh
PID=$(cat /var/run/cbs-web-service.pid)
kill -9 $PID
3、增加权限
chmod +x cbs-web-service-start.sh
chmod +x cbs-web-service-stop.sh
第二步:新建开机自启动脚本
1、在/usr/lib/systemd/system目录下创建服务开机启动脚本cbs-web-service.service
#!/bin/sh
[Unit]
Description=cbw-web-service
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/src/cbs/cbssh/cbs-web-service-start.sh
ExecStop=/usr/local/src/cbs/cbssh/cbs-web-service-stop.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
第三步:设置开机自启动
# 启用开机自启动
systemctl daemon-reload
systemctl enable cbs-web-service.service
# 禁用(移除)开机自启动
systemctl disable cbs-web-service.service
# 查看服务状态
systemctl status cbs-web-service.service
# 手动启动服务
systemctl start cbs-web-service.service
# 手动关闭服务
systemctl stop cbs-web-service.service
# 手动重启服务
systemctl restart cbs-web-service.service
# 查看开机启动项
systemctl list-unit-files | grep enable
注意:脚本开头的#!/bin/sh
不可少