CentOS6.9安裝daemontools工具管理服務進程
當服務進程因為某些原因(包括kill)掛掉了,daemontools會重新啟動這個進程
環境介紹:
CentOS 6.9 x86_64位最小化安裝
一、軟件包下載安裝過程
1.1下載軟件包解壓
[root@git-server ~]#wget http://cr.yp.to/daemontools/daemontools-0.76.tar.gz [root@git-server ~]# tar xf daemontools-0.76.tar.gz [root@git-server ~]# cd admin/daemontools-0.76/
編輯src/conf-cc文件在文件後面加入-include /usr/include/errno.h 防止在後續編譯安裝過程中報錯
[root@git-server daemontools-0.76]# vim src/conf-cc
gcc -O2 -Wimplicit -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow -Wcast-qual -Wcast-align -Wwrite-strings -include /usr/include/errno.h
註意:下面的最後一條命令的package表示daemontools-0.76目錄下的package目錄,而不是一個package命令
1.2sudo編譯安裝
[root@git-server daemontools-0.76]#sudo package/install Linking ./src/* into ./compile... Compiling everything in ./compile... sh find-systype.sh > systype rm -f compile sh print-cc.sh > compile chmod 555 compile ./compile byte_chr.c 。。。。。。。。 。。。。。。。。。 env - /bin/sh rts.tests 2>&1 | cat -v > rts rm -f sysdeps cat systype compile load >> sysdeps grep sysdep direntry.h >> sysdeps grep sysdep haswaitp.h >> sysdeps grep sysdep hassgact.h >> sysdeps grep sysdep hassgprm.h >> sysdeps grep sysdep select.h >> sysdeps grep sysdep uint64.h >> sysdeps grep sysdep iopause.h >> sysdeps grep sysdep hasmkffo.h >> sysdeps grep sysdep hasflock.h >> sysdeps grep sysdep hasshsgr.h >> sysdeps Copying commands into ./command... Creating symlink daemontools -> daemontools-0.76... Making command links in /command... Making compatibility links in /usr/local/bin... Creating /service... Adding svscanboot to inittab... init should start svscan now. 查看inittab,在文件末尾可以看到這一行: [root@git-server daemontools-0.76]# tail -1 /etc/inittab SV:123456:respawn:/command/svscanboot
之所以要使用sudo是因為,install腳本會在根目錄/下創建兩個目錄:/service和/command目錄。
其中/service目錄用來存放被daemontools管理的進程.
註意在/service目錄下存放的只能是連接。
/command命令下存放的是daemontools的一些常用命令。
二、配置daemontools
配置啟動項, 把 csh -cf ‘/command/svscanboot &‘ 加入到 /etc/rc.local=>/etc/rc.d/rc.local 末尾:開機自啟動
[root@git-server command]# sed -i ‘$acsh -cf \x27\/command\/svscanboot &\x27‘ /etc/rc.local
[root@git-server command]# tail -2 /etc/rc.local
csh -cf ‘/command/svscanboot &‘
[root@git-server command]# sh /etc/rc.local
/etc/rc.local: line 15: csh: command not found
報錯,於是安裝csh命令
[root@git-server command]# yum install tcsh -y
[root@git-server command]# which tcsh
/bin/tcsh
[root@git-server command]# which csh
/bin/csh
再次安裝成功:
[root@git-server command]# sh -x /etc/rc.local
+ bash /var/lib/subsys/local
+ csh -cf ‘/command/svscanboot &‘
[1] 13689
[root@git-server command]#
[root@git-server command]# ps -ef|grep svs
root 13689 1 0 22:55 pts/3 00:00:00 /bin/sh /command/svscanboot
root 13694 13689 0 22:55 pts/3 00:00:00 svscan /service
root 13768 9451 0 22:55 pts/3 00:00:00 grep --color svs
或者是直接後臺運行
[root@git-server service]# /command/svscanboot &
[root@git-server service]# ps -ef|grep svs
root 1044 7763 0 22:37 pts/2 00:00:00 grep --color svs
root 30451 9451 0 22:34 pts/3 00:00:00 /bin/sh /command/svscanboot
root 30458 30451 0 22:34 pts/3 00:00:00 svscan /service
從ps -ef | grep svs的輸出可以看到svscan作為svscanboot的子進程在運行。而且svscan監聽的目錄是/service目錄。
三、daemontools的簡單介紹
daemontools是一個守護進程工具,防止用戶進程意外退出。daemontools的工具有如下截圖:
3.1、常用的命令介紹:
(1)svscanboot
這個命令一般不用手動輸入執行,因為這個一般會隨Linux系統的啟動而自動啟動。於是加入開啟自啟動:/etc/rc.local就可以直到它會隨Linux的啟動而啟動。
svscanboot啟動之後會啟動svscan,svscan會啟動supervise,supervise會啟動並監聽/service中存放服務目錄下的run可執行文件。
(2)svscan
svscan會為/service目錄下所有服務啟動supervise進程。svscan會為每個服務啟動一對supervise服務:一個s子目錄(即服務進程的工程目錄),其實是個軟連接;一個是s子目錄下的log目錄(如果有的話)。supervise會執行s子目錄下的run腳本,並監聽run腳本啟動的進程,同時會啟動s/log目錄下的run腳本。這個s/log目錄下的run腳本用來輸出s目錄run腳本的日誌到指定目錄。svscan每隔5s檢查下/service目錄,如果有新的服務,則啟動新的supervise;如果有supervise意外退出會重新啟動supervise。
(3)supervise
supervise用來執行/service目錄下服務的run腳本。如果run腳本退出的話,會每隔幾秒執行一下。在/service下的服務目錄s下會有一個supervise目錄用來存放進程狀態的二進制格式信息。svstate就會讀取這個狀態信息
[root@git-server nginx]# svstat /service/nginx
/service/nginx: up (pid 19169) 0 seconds
[root@git-server nginx]#
(4)svc命令
svc opts services
opts是一系列的參數,services是指/service下的服務目錄。opts參數如下:
-u : up, 如果services沒有運行的話,啟動它,如果services停止了,重啟它。
-d : down, 如果services正在運行的話,給它發送一個TERM(terminate)信號,然後再發送一個CONT(continue)信號,在它停止後,不再啟動它。
-o : once, 如果services沒有運行,啟動它,但是在它停止後不再啟動了。就是只運行一次。
-p : pause, 給services發送一個停止信號。
-c : continue, 給services發送一個CONT信號。
-h : hang up, 給services發送一個HUP信號。
-a : alarm, 給services發送一個ALRM信號。
-i : interrupt, 給services發送一個INT信號。
-t : Terminate, 給services發送一個TERM信號。
-k : kill, 給services發送一個KILL信號。
-x : exit, supervise在services停止後會立刻退出, 但是值得註意的是,如果你在一個穩定的系統中使用了這個選項,你已經開始犯錯了:supervise被設計成為永遠運行的。
關掉一個服務進程通常使用-dk參數,此時supervise進程並沒有關閉,所以依然可以將這個服務進程重啟。即使,supervice進程掛掉了,svscan依然會重啟supervise,supervise會重新運行服務進程的run腳本。
Interface svc opts services
opts is a series of getopt-style options. services consists of any number of arguments, each argument naming a directory used by supervise.
svc applies all the options to each service in turn. Here are the options:
-u: Up. If the service is not running, start it. If the service stops, restart it.
-d: Down. If the service is running, send it a TERM signal and then a CONT signal. After it stops, do not restart it.
-o: Once. If the service is not running, start it. Do not restart it if it stops.
-p: Pause. Send the service a STOP signal.
-c: Continue. Send the service a CONT signal.
-h: Hangup. Send the service a HUP signal.
-a: Alarm. Send the service an ALRM signal.
-i: Interrupt. Send the service an INT signal.
-t: Terminate. Send the service a TERM signal.
-k: Kill. Send the service a KILL signal.
-x: Exit. supervise will exit as soon as the service is down. If you use this option on a stable system, you’re doing something wrong; supervise is designed to run forever.
(5)svstat
[root@git-server service]# ps -ef|grep svs
root 13689 1 0 22:55 pts/3 00:00:00 /bin/sh /command/svscanboot
root 13694 13689 0 22:55 pts/3 00:00:00 svscan /service
root 21765 7763 0 23:06 pts/2 00:00:00 grep --color svs
查看服務的狀態:
[root@git-server nginx]# svstat /service/nginx
/service/nginx: up (pid 19169) 0 seconds
四、舉例演示:
配置一個服務非常簡單:
1) 創建一個目錄,目錄下放一個run腳本
2) run腳本執行啟動服務的命令
3) 建立/service下一個鏈接
[root@git-server service]# mkdir /data/nginx -p
[root@git-server service]# cat /data/nginx/run
#!/bin/bash
exec service nginx start
停止nginx服務
[root@git-server nginx]# service nginx stop
[root@git-server service]# chmod +x /data/nginx/run
[root@git-server service]# ln -sv /data/nginx /service/nginx
`/service/nginx‘ -> `/data/nginx‘
神奇的就是最後一句,一旦創建了鏈接,daemontools就會自動幫你啟動nginx服務了。剛才停掉的nginx服務,然後服務是否又被啟動了:
如下圖:
停止對服務的監控:
[root@git-server service]# svc -d /service/nginx
[root@git-server service]# svstat /service/nginx
/service/nginx: down 213 seconds, normally up
停止nginx服務後,nginx服務不再被重啟
[root@git-server nginx]# service nginx stop
如果不再需要daemontools守護某服務,可完成上述操作後,將/service下的對應目錄刪除。
服務所在目錄/service
開啟對nginx服務的監控:
[root@git-server service]# svc -u /service/nginx
[root@git-server service]# svstat /service/nginx
/service/nginx: up (pid 23563) 1 seconds
nginx服務再次被啟動
[root@git-server nginx]# ps -ef|grep nginx
root 13695 13689 0 22:55 pts/3 00:00:00 readproctitle service errors: ...dy running.?Starting nginx... nginx (pid 23367 23366 23365 23364 23363) already running.?Starting nginx... nginx (pid 23367 23366 23365 23364 23363) already running.?Starting nginx... nginx (pid 23367 23366 23365 23364 23363) already running.?Starting nginx... nginx (pid 23367 23366 23365 23364 23363) already running.?Starting nginx... nginx (pid 23367 23366 23365 23364 23363) already running.?
root 13741 13694 0 22:55 pts/3 00:00:00 supervise nginx
root 23363 1 0 23:19 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www 23364 23363 0 23:19 ? 00:00:00 nginx: worker process
www 23365 23363 0 23:19 ? 00:00:00 nginx: worker process
www 23366 23363 0 23:19 ? 00:00:00 nginx: worker process
www 23367 23363 0 23:19 ? 00:00:00 nginx: worker process
root 23820 9451 0 23:20 pts/3 00:00:00 grep --color nginx
演示到此結束,很簡單吧,夥伴們一看就會。
CentOS6.9安裝daemontools工具管理服務進程