1轨域、創(chuàng)建系統(tǒng)文件 vim /etc/rc.d/init.d/devserver
一定要以此開頭?#!/bin/bash
#!/bin/bash?
#
# devserver_dog ? ?UM Device Server 1.0 (with watch dog)
#
# chkconfig: 345 70 30
# description: UM Device Server 1.0, with watch dog
# processname: devserver
case "$1" in?
start)
? ?/data/app/devserver/start_dog.sh
? ?;;
stop)
? ?kill `cat /var/run/devserver_dog.pid`
? ?rm /var/run/devserver_dog.pid
? ?;;
restart)
? ?$0 stop
? ?$0 start
? ?;;
status)
? ?if [ -e /var/run/devserver_dog.pid ]; then
? ? ? echo start_dog.sh is running, pid=`cat /var/run/devserver_dog.pid`
? ?else
? ? ? echo start_dog.sh is NOT running
? ? ? exit 1
? ?fi
? ?;;
*)
? ?echo "Usage: $0 {start|stop|status|restart}"
esac
exit 0
2蜓萄、創(chuàng)建啟動(dòng)文件?
vim ?start_dog.sh
#!/usr/bin/env bash
cd "$(dirname "$0")"
filename="dog_$(date +'%Y%m%d_%H%M%S').log"
test="java -jar binetwork.jar -s=localhost -p=8788 -e=be -r='x:2:fafb|x:1:01|b:1:00000000|d:4:21|x:2:0000|s:utf-8:123456789012345|x:4:00000000'"
service="java -jar devserver.jar 8788"
java -jar watchdog.jar --test="$test" --service="$service" --timeout=30000 --onfailed=restart > "$(pwd)/logs/$filename" &
echo $!>/var/run/devserver_dog.pid
3、創(chuàng)建開機(jī)啟動(dòng)文件广恢,此文件可以放在任何非系統(tǒng)目錄只要執(zhí)行systemctl enable 時(shí)能找到這個(gè)文件就行。
vim ?devserver.service
# Automatically generated by systemd-sysv-generator
[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/rc.d/init.d/devserver
Description=SYSV: UM Device Server 1.0, with watch dog
Before=runlevel3.target
Before=runlevel4.target
Before=runlevel5.target
Before=shutdown.target
Before=agentwatch.service
Before=aegis.service
Before=redis.service
After=network-online.target
After=network.service
Conflicts=shutdown.target
[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/rc.d/init.d/devserver start
ExecStop=/etc/rc.d/init.d/devserver stop
添加到開機(jī)啟動(dòng)項(xiàng) systemctl enable /data/app/healthserver/devserver.service
參考:https://www.cnblogs.com/saneri/p/7778756.html
CentOS7自定義系統(tǒng)服務(wù)
CentOS7的服務(wù)systemctl腳本存放在:/usr/lib/systemd/,有系統(tǒng)(system)和用戶(user)之分,需要開機(jī)不登陸就能運(yùn)行的程序呀潭,存在系統(tǒng)服務(wù)里钉迷,即:/usr/lib/systemd/system目錄下.
CentOS7的每一個(gè)服務(wù)以.service結(jié)尾,一般會(huì)分為3部分:[Unit]钠署、[Service]和[Install]?
[Unit]部分主要是對(duì)這個(gè)服務(wù)的說(shuō)明篷牌,內(nèi)容包括Description和After,Description 用于描述服務(wù)踏幻,After用于描述服務(wù)類別
[Service]部分是服務(wù)的關(guān)鍵枷颊,是服務(wù)的一些具體運(yùn)行參數(shù)的設(shè)置.
Type=forking是后臺(tái)運(yùn)行的形式,
User=users是設(shè)置服務(wù)運(yùn)行的用戶,
Group=users是設(shè)置服務(wù)運(yùn)行的用戶組,
PIDFile為存放PID的文件路徑,
ExecStart為服務(wù)的具體運(yùn)行命令,
ExecReload為重啟命令夭苗,
ExecStop為停止命令信卡,
PrivateTmp=True表示給服務(wù)分配獨(dú)立的臨時(shí)空間
注意:[Service]部分的啟動(dòng)、重啟题造、停止命令全部要求使用絕對(duì)路徑傍菇,使用相對(duì)路徑則會(huì)報(bào)錯(cuò)!
[Install]部分是服務(wù)安裝的相關(guān)設(shè)置界赔,可設(shè)置為多用戶的
首先丢习,使用systemctl start? [ 服務(wù)名(也是文件名) ] 可測(cè)試服務(wù)是否可以成功運(yùn)行,如果不能運(yùn)行則可以使用systemctl status [ 服務(wù)名(也是文件名) ]查看錯(cuò)誤信息和其他服務(wù)信息淮悼,然后根據(jù)報(bào)錯(cuò)進(jìn)行修改咐低,直到可以start,如果不放心還可以測(cè)試restart和stop命令袜腥。
接著见擦,只要使用systemctl enable xxxxx就可以將所編寫的服務(wù)添加至開機(jī)啟動(dòng)即可。
實(shí)例:服務(wù)用于開機(jī)運(yùn)行tomcat項(xiàng)目:
#vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=java tomcat project
After=tomcat.service
[Service]
Type=forking
User=users
Group=users
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecReload=
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
?添加可執(zhí)行權(quán)限:
chmod 754 /usr/lib/systemd/system/tomcat.service
設(shè)置為開機(jī)自啟動(dòng):
#systemctl enable tomcat.service
?常用指令(以tomcat服務(wù)為例):
啟動(dòng)某服務(wù)
systemctl start?? tomcat.service
停止某服務(wù)
systemctl stop?? tomcat.service
重啟某服務(wù)
service tomcat?? restart
systemctl restart?? tomcat.service
使某服務(wù)自動(dòng)啟動(dòng)(如tomcat服務(wù))
systemctl enable?? tomcat.service
使某服務(wù)不自動(dòng)啟動(dòng)
systemctl disable?? tomcat.service
檢查服務(wù)狀態(tài)
systemctl?? status tomcat.service (服務(wù)詳細(xì)信息)
systemctl???is-active tomcat.service(僅顯示是否Active)
顯示所有已啟動(dòng)的服務(wù)
systemctl?? list-units --type=service