CentOS6/7 配置守護進程

CentOS6.xCentOS6中轉用Upstrat代替以前的init.d/rcX.d的線性啟動方式 。
一、相關命令
通過initctl help可以查看相關命令
[root@localhost ~]# initctl helpJob commands:startStart job.stopStop job.restartRestart job.reloadSend HUP signal to job.statusQuery status of job.listList known jobs.Event commands:emitEmit an event.Other commands:reload-configurationReload the configuration of the init daemon.versionRequest the version of the init daemon.log-priorityChange the minimum priority of log messages from the init daemonusageShow job usage message if available.helpdisplay list of commandsFor more information on a command, try `initctl COMMAND --help'.二、自己配置一個
在/etc/init/文件夾中新建一個testserver.conf配置文件 。
通過exec執行發布出來的程序可執行文件 。通過設置respawn讓程序反復啟動 。
start on runlevel [2345]stop on runlevel [!2345]respawnexec /usr/local/src/testserver/testserver然后啟動
initctl reload-configurationinitctl listinitctl start testserver通過initctl list即可看程序是不是處于running啟動狀態 。
[root@localhost ~]# initctl listvmware-tools start/runningrc stop/waitingtty (/dev/tty3) start/running, process 3024tty (/dev/tty2) start/running, process 3022tty (/dev/tty6) start/running, process 3033tty (/dev/tty5) start/running, process 3028tty (/dev/tty4) start/running, process 3026plymouth-shutdown stop/waitingtestserver start/running, process 4157control-alt-delete stop/waitingrcS-emergency stop/waitingreadahead-collector stop/waitingkexec-disable stop/waitingquit-plymouth stop/waitingrcS stop/waitingprefdm start/running, process 3017init-system-dbus stop/waitingck-log-system-restart stop/waitingreadahead stop/waitingck-log-system-start stop/waitingsplash-manager stop/waitingstart-ttys stop/waitingreadahead-disable-services stop/waitingck-log-system-stop stop/waitingrcS-sulogin stop/waitingserial stop/waiting【CentOS6/7 配置守護進程】可以看到其處于啟動狀態,現在守護進程已經設置成功 。
另外,配置文件中可以通過script ... end script執行腳本 。舉個例子
start on runlevel [2345]stop on runlevel [!2345]scriptecho “test~~~~~” >>/tmp/test.txtend scriptCentOS7.xCentos7中可以通過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:進程間通信的socketSwap 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 serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/usr/local/nginx/logs/nginx.pidExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stop#Restart配置可以在進程被kill掉之后,讓systemctl產生新的進程,避免服務掛掉Restart=on-failureRestartSec=30[Install]WantedBy=multi-user.target1、[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 :這一選項適用于只執行一項任務、隨后立即退出的服務 ??赡苄枰瑫r設置 RemainAfterExit=yes 使得 systemd 在服務進程退出之后仍然認為服務處于激活狀態 。Type=notify:與 Type=simple 相同 , 但約定服務會在就緒后向 systemd 發送一個信號 。這一通知的實現由 libsystemd-daemon.so 提供 。Type=dbus:若以此方式啟動,當指定的 BusName 出現在DBus系統總線上時,systemd 認為服務就緒 。Type=idle:systemd 會等待所有任務處理完成后,才開始執行 idle 類型的單元 。其他行為與 Type=simple 類似 。

推薦閱讀