linux服務(wù)管理的兩種方式為service和systemctl。systemd是Linux系統(tǒng)最新的初始化系統(tǒng)(init),作用是提高系統(tǒng)的啟動(dòng)速度,盡可能啟動(dòng)較少的進(jìn)程,盡可能并發(fā)啟動(dòng)更多進(jìn)程。systemd對應(yīng)的進(jìn)程管理命令是systemctl。
systemctl命令用法
1. 列出所用可用單元
systemctl list-unit-files
2. 列出所有運(yùn)行中的單元
systemctl list-units
3. 檢查某個(gè)單元(如 crond.service)是否啟用
systemctl is-enabled crond.service
4. 列出所有服務(wù)
systemctl list-unit-files ?Ctype=service
5. Linux中如何啟動(dòng)、重啟、停止、重載服務(wù)以及檢查服務(wù)(如 httpd.service)狀態(tài)
systemctl start httpd.service
systemctl restart httpd.service
systemctl stop httpd.service
systemctl reload httpd.service
systemctl status httpd.service
6. 如何激活服務(wù)并在開機(jī)時(shí)啟用或禁用服務(wù)(即系統(tǒng)啟動(dòng)時(shí)自動(dòng)啟動(dòng)mysql.service服務(wù))
systemctl is-active mysql.service
systemctl enable mysql.service
systemctl disable mysql.service
7. 如何屏蔽(讓它不能啟動(dòng))或顯示服務(wù)(如ntpdate.service)
systemctl mask ntpdate.service
ln -s '/dev/null' '/etc/systemd/system/ntpdate.service'
systemctl unmask ntpdate.service
rm '/etc/systemd/system/ntpdate.service'
8. 使用systemctl命令殺死服務(wù)
systemctl kill crond
編寫一個(gè)服務(wù)
1. 添加服務(wù)文件
在/lib/systemd/system/文件目錄下添加.service服務(wù)文件;
2. 編寫.service文件
[Unit]
ConditionFileIsExecutable=/etc/init.d/tst.sh
After=weston.service
[Service]
Type=forking
ExecStart=-/etc/init.d/tst.sh start
ExecStop=-/etc/init.d/tst.sh stop
[Install]
WantedBy=multi-user.target
從上面可以看出.serive文件包括三個(gè)部分:[Unit]、[Service]、[Install]。
“
[Unit]
Description:對當(dāng)前服務(wù)的簡單描述。
After:指定.serive在哪些服務(wù)之后進(jìn)行啟動(dòng);
Before:指定.serive在哪些服務(wù)之前進(jìn)行啟動(dòng);
除上述內(nèi)容,文件中還可能出現(xiàn)以下內(nèi)容:
Requires:指定服務(wù)依賴于哪些服務(wù)(強(qiáng)依賴關(guān)系,一旦所依賴服務(wù)異常,當(dāng)前服務(wù)也隨之停止);
Wants:指定服務(wù)依賴于哪些服務(wù)(弱依賴關(guān)系,所依賴服務(wù)異常不影響當(dāng)前服務(wù)正常運(yùn)行)。
“
[Service]
Type:定義啟動(dòng)類型??稍O(shè)置:simple,exec,forking,oneshot,dbus,notify,idle。
simple:ExecStart 字段啟動(dòng)的進(jìn)程為該服務(wù)的主進(jìn)程;
forking:ExecStart 字段的命令將以 fork() 方式啟動(dòng),此時(shí)父進(jìn)程將會(huì)退出,子進(jìn)程將成為主進(jìn)程;
ExecStart:定義啟動(dòng)進(jìn)程時(shí)執(zhí)行的命令;
ExecStop:停止服務(wù)時(shí)執(zhí)行的命令;
除上述內(nèi)容外,文件中還可能出現(xiàn):
EnvironmentFile:環(huán)境配置文件,用來指定當(dāng)前服務(wù)啟動(dòng)的環(huán)境變量;
ExecReload:重啟服務(wù)時(shí)執(zhí)行的命令;
ExecStartPre:啟動(dòng)服務(wù)之前執(zhí)行的命令;
ExecStartPost:啟動(dòng)服務(wù)之后執(zhí)行的命令;
ExecStopPost:停止服務(wù)之后執(zhí)行的命令;
RemainAfterExit:設(shè)為yes,表示進(jìn)程退出以后,服務(wù)仍然保持執(zhí)行;
RestartSec:重啟服務(wù)之前需要等待的秒數(shù)。
KillMode:定義 Systemd 如何停止服務(wù),可以設(shè)置的值如下:
control-group(默認(rèn)值):當(dāng)前控制組里面的所有子進(jìn)程,都會(huì)被殺掉;
process:只殺主進(jìn)程;
mixed:主進(jìn)程將收到 SIGTERM 信號(hào),子進(jìn)程收到 SIGKILL 信號(hào);
none:沒有進(jìn)程會(huì)被殺掉。
Restart:定義了退出后,Systemd 的重啟方式。
可以設(shè)置的值如下:
no(默認(rèn)值):退出后不會(huì)重啟;
on-success:當(dāng)進(jìn)程正常退出時(shí)(退出狀態(tài)碼為0),才會(huì)重啟;
on-failure:當(dāng)進(jìn)程非正常退出時(shí)(退出狀態(tài)碼非0),包括被信號(hào)終止和超時(shí),才會(huì)重啟;
on-abnormal:當(dāng)被信號(hào)終止和超時(shí),才會(huì)重啟;
on-abort:當(dāng)收到?jīng)]有捕捉到的信號(hào)終止時(shí),才會(huì)重啟;
on-watchdog:看門狗超時(shí)退出,才會(huì)重啟;
always:總是重啟。
“
[Install]
Install一般填為WantedBy=multi-user.target,表示多用戶環(huán)境下服務(wù)被啟用。
3. 設(shè)置開機(jī)自啟動(dòng)
systemctl enable (服務(wù)名)
4. 查詢服務(wù)狀態(tài)
systemctl status (服務(wù)名)
-
Linux
+關(guān)注
關(guān)注
87文章
11352瀏覽量
210551
發(fā)布評論請先 登錄
相關(guān)推薦
評論