Linux:使用systemd管理進程

systemd是目前Linux系統(tǒng)上主要的系統(tǒng)守護進程管理工具,由于init一方面對于進程的管理是串行化的,容易出現(xiàn)阻塞情況,另一方面init也僅僅是執(zhí)行啟動腳本,并不能對服務本身進行更多的管理。所以從CentOS 7 開始也由systemd取代了init作為默認的系統(tǒng)進程管理工具。

systemd所管理的所有系統(tǒng)資源都稱作Unit,通過systemd命令集可以方便的對這些Unit進行管理。比如systemctl、hostnamectl、timedatectl、localctl等命令,這些命令雖然改寫了init時代用戶的命令使用習慣(不再使用chkconfig、service等命令),但確實也提供了很大的便捷性。

特點

CentOS 7 支持開機并行啟動服務,顯著提高開機啟動效率

CentOS7使用systemd解決原有模式缺陷,比如原有service不會關(guān)閉程序產(chǎn)生的子進程

語法

systemctl [OPTIONS...] {COMMAND} ...

command:

start:啟動指定的unit,例如systemctl start nginx

stop:關(guān)閉指定的unit,例如systemctl stop nginx

restart:重啟指定unit,例如systemctl restart nginx

reload:重載指定unit,例如systemctl reload nginx

enable:系統(tǒng)開機時自動啟動指定unit,前提是配置文件中有相關(guān)配置,例如systemctl enable nginx

disable:開機時不自動運行指定unit,例如systemctl disable nginx

status:查看指定unit當前運行狀態(tài),例如systemctl status nginx

配置說明

每一個Unit都需要有一個配置文件用于告知systemd對于服務的管理方式

配置文件存放于/usr/lib/systemd/system/,設(shè)置開機啟動后會在/etc/systemd/system目錄建立軟鏈接文件

每個Unit的配置文件配置默認后綴名為.service

在/usr/lib/systemd/system/目錄中分為system和user兩個目錄,一般將開機不登陸就能運行的程序存在系統(tǒng)服務里,也就是/usr/lib/systemd/system

配置文件使用方括號分成了多個部分,并且區(qū)分大小寫

相關(guān)文件說明:

相關(guān)文件 CentOS6 CentOS7

服務啟動的腳本啟動路徑 /etc/init.d /usr/lib/systemd/system

開機自啟服務存放路徑 /etc/rcN.d /etc/systemd/system/multi-user.target.wants/

默認運行級別配置文件 /etc/inittab /etc/systemd/system/default.target

常見中間件配置

Nginx

[Unit]

Description=nginx

After=network.target


[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true


[Install]

WantedBy=multi-user.target

MySQL

[Unit]

Description=MySQL Server

Documentation=man:mysqld(8)

Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target

After=syslog.target

[Install]

WantedBy=multi-user.target

[Service]

User=mysql

Group=mysql

Type=forking

PIDFile=/var/run/mysqld/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.

TimeoutSec=0

# Execute pre and post scripts as root

PermissionsStartOnly=true

# Needed to create system tables

ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service

ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation

EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit

LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

亞馬遜測評 www.yisuping.cn

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容