簡介
在CentOS6中寫服務腳本,需要放在/etc/init.d/目錄下,且腳本編寫較復雜在。而在CentOS7中寫服務腳本,只需要按照格式編寫即可。
CentOS6中服務腳本zabbix_server 參考linux老男孩-shell編程實戰185頁
#!/bin/sh #chkconfig: 2345 80 05 #description: zabbix path=/user/local/zabbix/sbin pid=/tmp/zabbix_server.pid conf=/user/local/zabbix/etc/zabbix_server.conf RETVAL=0 . /etc/init.d/functions start(){ if [ ! -f $pid ];then $path/zabbix_server -c $conf RETVAL=$? if [ $RETVAL -eq 0 ];then action "zabbix_server is started" /bin/true return $RETVAL else action "zabbix_server is started" /bin/false return $RETVAL fi else echo "zabbix_server is running" return 0 fi } stop(){ if [ -f $pid ];then ps -ef |grep zabbix|grep -v grep |awk '{print $2}' |xargs kill -9 RETVAL=$? if [ $RETVAL -eq 0 ];then action "zabbix_server is stoped" /bin/true return $RETVAL else action "zabbix_server is stoped" /bin/false return $RETVAL fi else echo "zabbix_server is no running" return 0 fi } case $1 in start) start RETVAL=$? ;; stop) stop RETVAL=$? ;; restart) stop sleep 1 start RETVAL=$? ;; status) #ps -ef |grep zabbix|grep -v grep |grep -v status process=`ps -ef |grep zabbix|grep -v grep |grep -v status |wc -l` echo zabbix process $process ;; *) echo $"Usage: $0 {start|stop|restart|status}" exit 1 ;; esac exit $RETVAL
服務腳本編寫
存放位置
/usr/lib/systemd/system #系統服務,開機不需要登錄就能運行的程序(可以用於開機自啟)
/usr/lib/systemd/user #用戶服務,需要登錄后才能運行程序
服務腳本編寫
服務腳本一般以xxx.service命名,且腳本中分為三部分:[Unit]、[Service]、[Install]
示例: # vim /usr/lib/systemd/system/xxx.service
[Unit] # 主要是服務說明 Description=test # 簡單描述服務 After=network.target # 描述服務類別,表示本服務需要在network服務啟動后在啟動 Before=xxx.service # 表示需要在某些服務啟動之前啟動,After和Before字段只涉及啟動順序,不涉及依賴關系。 [Service] # 服務的關鍵 Type=forking # 表示后台運行模式。 PIDFile=/usr/local/test/test.pid # 存放PID的絕對路徑 ExecStart=/usr/local/test/bin/startup.sh # 服務啟動命令,命令需要絕對路徑 ExecReload=xxx #為重啟命令, ExecStop=xxx #為停止命令 PrivateTmp=true # 表示給服務分配獨立的臨時空間 [Install] WantedBy=multi-user.target # 多用戶
[Unit]區塊字段描述 Description:簡短描述 Documentation:文檔地址 Requires:當前 Unit 依賴的其他 Unit,如果它們沒有運行,當前 Unit 會啟動失敗 Wants:與當前 Unit 配合的其他 Unit,如果它們沒有運行,當前 Unit 不會啟動失敗 BindsTo:與Requires類似,它指定的 Unit 如果退出,會導致當前 Unit 停止運行 Before:如果該字段指定的 Unit 也要啟動,那么必須在當前 Unit 之后啟動 After:如果該字段指定的 Unit 也要啟動,那么必須在當前 Unit 之前啟動 Conflicts:這里指定的 Unit 不能與當前 Unit 同時運行 Condition...:當前 Unit 運行必須滿足的條件,否則不會運行 Assert...:當前 Unit 運行必須滿足的條件,否則會報啟動失敗 [Service]區塊字段描述 Type:定義啟動時的進程行為。它有以下幾種值。 Type=simple:默認值,執行ExecStart指定的命令,啟動主進程 Type=forking:以 fork 方式從父進程創建子進程,創建后父進程會立即退出 Type=oneshot:一次性進程,Systemd 會等當前服務退出,再繼續往下執行 Type=dbus:當前服務通過D-Bus啟動 Type=notify:當前服務啟動完畢,會通知Systemd,再繼續往下執行 Type=idle:若有其他任務執行完畢,當前服務才會運行 ExecStart:啟動當前服務的命令 ExecStartPre:啟動當前服務之前執行的命令 ExecStartPost:啟動當前服務之后執行的命令 ExecReload:重啟當前服務時執行的命令 ExecStop:停止當前服務時執行的命令 ExecStopPost:停止當其服務之后執行的命令 RestartSec:自動重啟當前服務間隔的秒數 Restart:定義何種情況 Systemd 會自動重啟當前服務 no(默認值): # 退出后無操作 on-success: # 只有正常退出時(退出狀態碼為0),才會重啟 on-failure: # 非正常退出時,重啟,包括被信號終止和超時等 on-abnormal: # 只有被信號終止或超時,才會重啟 on-abort: # 只有在收到沒有捕捉到的信號終止時,才會重啟 on-watchdog: # 超時退出時,才會重啟 always: # 不管什么退出原因,都會重啟(除了systemctl stop) # 對於守護進程,推薦用on-failure KillMode的類型: control-group(默認):# 當前控制組里的所有子進程,都會被殺掉 process: # 只殺主進程 mixed: # 主進程將收到SIGTERM信號,子進程收到SIGKILL信號 none: # 沒有進程會被殺掉,只是執行服務的stop命令 TimeoutSec:定義 Systemd 停止當前服務之前等待的秒數 Environment:指定環境變量 [Install]字段描述 WantedBy:它的值是一個或多個 Target,當前 Unit 激活時(enable)符號鏈接會放入/etc/systemd/system目錄下面以 Target 名 + .wants后綴構成的子目錄中 multi-user.target: # 表示多用戶命令行狀態,這個設置很重要 graphical.target: # 表示圖形用戶狀體,它依賴於multi-user.target RequiredBy:它的值是一個或多個 Target,當前 Unit 激活時,符號鏈接會放入/etc/systemd/system目錄下面以 Target 名 + .required后綴構成的子目錄中 Alias:當前 Unit 可用於啟動的別名 Also:當前 Unit 激活(enable)時,會被同時激活的其他 Unit
nginx服務腳本編寫
[root@n1 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecStop=/usr/local/nginx/sbin/nginx -s stop -c /usr/local/nginx/conf/nginx.conf ExecReload= /usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf PrivateTmp=ture [Install] WantedBy=multi-user.target
命令使用:
#啟動、停止服務
systemctl start nginx.service
systemctl stop nginx.service
#重啟服務
systemctl reload nginx.service #服務啟動時重啟有效
systemctl restart nginx.service
#查看nginx服務狀態
systemctl status nginx.service
#添加服務到開機自啟、取消開機自啟
systemctl enable nginx.service
systemctl disable nginx.service
zabbix_server服務腳本編寫
[root@localhost ~]# vim /usr/lib/systemd/system/zabbix_server.service
[Unit] Description=Zabbix Server After=syslog.target After=network.target [Service] Environment="CONFFILE=/usr/local/zabbix/etc/zabbix_server.conf" EnvironmentFile=-/etc/sysconfig/zabbix-server Type=forking Restart=on-failure PIDFile=/tmp/zabbix_server.pid KillMode=control-group ExecStart=/usr/local/zabbix/sbin/zabbix_server -c $CONFFILE ExecStop=/bin/kill -SIGTERM $MAINPID RestartSec=10s TimeoutSec=0 [Install] WantedBy=multi-user.target
vim /usr/lib/systemd/system/zabbix_agentd.service # 添加以服務為名的service文件 [Unit] Desciption=zabbix_agentd - zabbix monitor client After=network.target # 在network啟動后再啟動 #Before=xxx # After Before不存在依賴關系,只是啟動順序 [Service] User=zabbix Group=zabbix Type=forking # 此服務以forking模式運行 PIDFile=/tmp/zabbix_agentd.pid # PID文件存放位置 ExecStartPre=/usr/bin/rm -f /tmp/zabbix_agentd.pid # 啟動前刪除PID文件 ExecStart=/usr/local/zabbix_agent-3.4.15/sbin/zabbix_agentd # 啟動命令 ExecReload=/bin/kill -s HUP $MAINPID # 重載執行命令 KillSignal=SIGQUIT TimeoutStopSec=5 # 停止超時時間,如果不能在指定時間內停止,將通過SIGKILL強制終止 KillMode=mixed # systemd停止服務的方式 Restart=on-failure # 服務不正常退出后重啟 #PrivateTmp=true # 表示給服務分配獨立的臨時空間 [Install] WantedBy=multi-user.target # 多用戶模式
參考
————————————————
版權聲明:本文為CSDN博主「real向往」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/yuanfangpoet/article/details/89410312
