CentOS6/7 配置守護進程

CentOS6.x

CentOS6中轉用Upstrat代替以前的init.d/rcX.d的線性啟動方式。

一、相關命令

通過initctl help可以查看相關命令

[root@localhost ~]# initctl help
Job commands:
  start                       Start job.
  stop                        Stop job.
  restart                     Restart job.
  reload                      Send HUP signal to job.
  status                      Query status of job.
  list                        List known jobs.

Event commands:
  emit                        Emit an event.

Other commands:
  reload-configuration        Reload the configuration of the init daemon.
  version                     Request the version of the init daemon.
  log-priority                Change the minimum priority of log messages from the init daemon
  usage                       Show job usage message if available.
  help                        display list of commands

For more information on a command, try `initctl COMMAND --help'.

 

二、自己配置一個

在/etc/init/文件夾中新建一個testserver.conf配置文件。

通過exec執行發布出來的程式可執行文件。通過設置respawn讓程式反覆啟動。

start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /usr/local/src/testserver/testserver

然後啟動

initctl reload-configuration 
initctl list 
initctl start testserver

通過initctl list即可看程式是不是處於running啟動狀態。

[root@localhost ~]# initctl list
vmware-tools start/running
rc stop/waiting
tty (/dev/tty3) start/running, process 3024
tty (/dev/tty2) start/running, process 3022
tty (/dev/tty6) start/running, process 3033
tty (/dev/tty5) start/running, process 3028
tty (/dev/tty4) start/running, process 3026
plymouth-shutdown stop/waiting
testserver start/running, process 4157
control-alt-delete stop/waiting
rcS-emergency stop/waiting
readahead-collector stop/waiting
kexec-disable stop/waiting
quit-plymouth stop/waiting
rcS stop/waiting
prefdm start/running, process 3017
init-system-dbus stop/waiting
ck-log-system-restart stop/waiting
readahead stop/waiting
ck-log-system-start stop/waiting
splash-manager stop/waiting
start-ttys stop/waiting
readahead-disable-services stop/waiting
ck-log-system-stop stop/waiting
rcS-sulogin stop/waiting
serial stop/waiting

可以看到其處於啟動狀態,現在守護進程已經設置成功。

另外,配置文件中可以通過script … end script執行腳本。舉個例子

start on runlevel [2345]
stop on runlevel [!2345]
script 
echo 「test~~~~~」 >>/tmp/test.txt 
end script

CentOS7.x

Centos7中可以通過systemd配置守護進程。

一、Unit的含義

systemd可以管理所有系統資源,不同資源統稱為 Unit,一共分為12種:

Service unit:   系統服務
Target unit:    多個unit構成一個組
Device unit:    硬體設備
Mount unit:     文件系統的掛載點
Automount unit: 自動掛載點
Path unit:      文件或路徑
Scope unit:     不是由Systemd啟動的外部進程
Slice unit:     進程組
Snapshot unit:  Systemd快照,可以切回某個快照
Socket unit:    進程間通訊的socket
Swap unit:      swap文件
Timer unit:     定時器

二、Unit管理常用命令(主要針對service)

# 開機自啟動
systemctl enable nginx

# 關閉自啟動
systemctl disable nginx

# 服務狀態
systemctl status nginx

# 服務重啟
systemctl restart nginx

# 殺死一個服務
systemctl kill nginx

# 顯示已啟動的服務
systemctl list-units --type=service

 

三、Unit配置文件

每一個Unit都有一個配置文件,用於告訴系統如何啟動Unit,systemd默認從 /etc/systemd/system/ 目錄讀取配置文件,

Unit配置文件目錄主要有三個:

/lib/systemd/system
/run/systemd/system
/etc/systemd/system

 

四、Unit服務配置

每個服務以.service後綴,一般會分為3部分:[Unit],[Service],[Install],具體以nginx服務為例:

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
 
#Restart配置可以在進程被kill掉之後,讓systemctl產生新的進程,避免服務掛掉
Restart=on-failure
RestartSec=30
 
[Install]
WantedBy=multi-user.target

1、[Unit]區塊

[Unit]區塊通常是配置文件的第一個區塊,用來定義Unit的元數據,以及配置與其他Unit的關係。

Description: 簡短描述
Documentation: 文檔地址
After:依賴,僅當依賴的服務啟動之後再啟動自定義的服務單元

2、[Service]區塊

[Service]區塊用來Service的配置,只有service類型的unit才有本區塊。

Type: 定義啟動時的進程行為。它有以下幾種值:

Type=simple  :(默認值) systemd認為該服務將立即啟動。服務進程不會 fork 。如果該服務要啟動其他服務,不要使用此類型啟動,除非該服務是 socket 激活型。
Type=forking :systemd認為當該服務進程 fork,且父進程退出後服務啟動成功。對於常規的守護進程(daemon),除非你確定此啟動方式無法滿足需求,使用此類型啟動即可。
               使用此啟動類型應同時指定 PIDFile=,以便 systemd 能夠跟蹤服務的主進程。
Type=oneshot :這一選項適用於只執行一項任務、隨後立即退出的服務。可能需要同時設置 RemainAfterExit=yes 使得 systemd 在服務進程退出之後仍然認為服務處於激活狀態。
Type=notify  :與 Type=simple 相同,但約定服務會在就緒後向 systemd 發送一個訊號。這一通知的實現由 libsystemd-daemon.so 提供。
Type=dbus    :若以此方式啟動,當指定的 BusName 出現在DBus系統匯流排上時,systemd 認為服務就緒。
Type=idle    :systemd 會等待所有任務處理完成後,才開始執行 idle 類型的單元。其他行為與 Type=simple 類似。

其他選項:

ExecStart:     啟動服務的命令
ExecStartPre:  啟動服務之前執行的命令
ExecStartPost: 啟動服務之後執行的命令
ExecReload:    重啟服務執行時的命令
ExecStop:      停止服務時執行的命令
ExecStopPost:  停止服務之後執行的命令
RestartSec:    自動重啟服務間隔的秒數
Restart:       定義何種情況下會自動重啟服務,可能的值包括always(總是重啟)、on-success、on-failure、on-abnormal、on-abort、on-watchdog
TimeoutSec:    定義Systemd停止服務之前等待的秒數
Environment:   指定環境變數
PIDFile:      pid文件路徑
PrivateTmp:   true表示給服務分配獨立的臨時空間
User:         執行命令的用戶
Group:        執行命令的組

3、[Install]區塊

[Install]區塊用來定義如何啟動,以及是否開機啟動。

WantedBy:   它的值是一個或多個Target,當前Unit激活時(enable)符號鏈接會放入/etc/systemd/system目錄下面以Target名 + .wants後綴構成的子目錄中
RequiredBy: 它的值是一個或多個Target,當前Unit激活時(enable)符號鏈接會放入/etc/systemd/system目錄下面以Target名 + .required後綴構成的子目錄中
Alias:      當前Unit可用於啟動的別名
Also:       當前Unit激活時(enable),會被同時激活的其他Unit

 

五、Target的概念

Target就是一個Unit組,包含許多相關Unit。啟動某個Target的時候,Systemd就會啟動裡面所有的Unit。

傳統init啟動模式裡面,有RunLevel的概念,跟Target的作用很類似。不同的是,RunLevel是互斥的,不可能多個RunLevel同時啟動,但是多個Target可以同時啟動。

Target的常用命令:

查看所有target下的unit
systemctl list-unit-files --type=target
 
查看默認target,即默認的運行級別。對應於舊的`runlevel`命令
systemctl get-default
 
設置默認的target
systemctl set-default multi-user.target
 
查看target下的unit
systemctl list-dependencies multi-user.target
 
切換target,不屬於新target的unit都會被停止
systemctl isolate multi-user.target

 

六、自己配置一個

/lib/systemd/system/目錄中定義一個自己的testserver.service文件

[Unit]
Description=testserver

[Service]
# 應用程式所在的文件目錄
WorkingDirectory=/usr/local/src/testserver/
ExecStart=/usr/local/src/testserver/testserver
Restart=always

# 如果服務崩潰,10秒後重新啟動服務
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=testserver
User=root

# Production:生產環境 Development:開發環境
# Environment=ASPNETCORE_ENVIRONMENT=Development


[Install]
WantedBy=multi-user.target  # 由此target觸發自啟動

 

通過以下命令實現啟動

systemctl daemon-reload

// 自動啟動 systemctl enable testserver.service

//立即啟動 systemctl start testserver.service
//狀態查看 systemctl status testserver.service

 

部分摘自

//t.zoukankan.com/solohac-p-4154181.html

//www.cnblogs.com/jkko123/p/12171572.html

Tags: