1. service方式
service其實管理的是腳本,有一些需要我們自己編寫,腳本位置在/etc/inin.d/
root@ubuntu:/# cd /etc/init.d/ root@ubuntu:/etc/init.d# ls acpid checkfs.sh docker kmod networking rc.local single umountroot alsa-utils checkroot-bootclean.sh grub-common lightdm network-manager rcS skeleton unattended-upgrades anacron checkroot.sh halt mountall-bootclean.sh ondemand README speech-dispatcher urandom apparmor console-setup hostname.sh mountall.sh open-vm-tools reboot supervisor uuidd apport cron hwclock.sh mountdevsubfs.sh plymouth resolvconf thermald whoopsie avahi-daemon cups irqbalance mountkernfs.sh plymouth-log rsync udev x11-common bluetooth cups-browsed kerneloops mountnfs-bootclean.sh pppd-dns rsyslog ufw bootmisc.sh dbus keyboard-setup mountnfs.sh procps saned umountfs brltty dns-clean killprocs mysql rc sendsigs umountnfs.sh
例如:我們想查看 docker的狀態,service docker status

需求:在桌面寫一個test_service.py,讓 service test_service start /service test_service stop / service test_service restart / service test_service status 都可以執行
2. systemctl方式
這種方式管理的是服務xxx.service,讀取服務配置,/lib/systemd/system
練習一:自己寫一個xx.py,被systemctl管理
test.py
import random import time a = random.randint(1,10) while 1: time.sleep(1) print(a) if __name__ == "__main__": pass
test.sh
#! /bin/bash # Provides: Python_Test # Required-Start: $all # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: Python Test file_path="/home/yanyanzhang/Desktop" echo $file_path python3.7 $file_path/test.py
#####
上面的Default-Start必須寫,要不這一步就會報如下的錯。
root@ubuntu:/lib/systemd/system# systemctl enable python_test
Synchronizing state of python_test.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable python_test
insserv: warning: script 'K01python_test' missing LSB tags and overrides
insserv: warning: script 'python_test' missing LSB tags and overrides
update-rc.d: error: python_test Default-Start contains no runlevels, aborting. # 運行等級
python_test.service
[Unit] Description=PythonTest [Service] Type=simple Restart=on-failure ExecStart=/bin/sh /home/yanyanzhang/Desktop/test.sh KillSignal=SIGIN [Install] WantedBy=multi-user.target
systemctl enable python_test
systemctl start python_test
systemctl status python_test
root@ubuntu:/lib/systemd/system# systemctl status python_test ● python_test.service - PythonTest Loaded: loaded (/lib/systemd/system/python_test.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-05-01 19:25:05 CST; 2s ago Main PID: 128376 (sh) Tasks: 2 Memory: 4.3M CPU: 26ms CGroup: /system.slice/python_test.service ├─128376 /bin/sh /home/yanyanzhang/Desktop/test.sh └─128377 python3.7 /home/yanyanzhang/Desktop/test.py May 01 19:25:05 ubuntu systemd[1]: Started PythonTest. May 01 19:25:05 ubuntu sh[128376]: /home/yanyanzhang/Desktop
停止 systemctl stop python_test
root@ubuntu:/lib/systemd/system# systemctl stop python_test root@ubuntu:/lib/systemd/system# systemctl status python_test ● python_test.service - PythonTest Loaded: loaded (/lib/systemd/system/python_test.service; enabled; vendor preset: enabled) Active: inactive (dead) since Sat 2021-05-01 19:38:21 CST; 15s ago Process: 128376 ExecStart=/bin/sh /home/yanyanzhang/Desktop/test.sh (code=killed, signal=TERM) Main PID: 128376 (code=killed, signal=TERM) May 01 19:25:05 ubuntu systemd[1]: Started PythonTest. May 01 19:25:05 ubuntu sh[128376]: /home/yanyanzhang/Desktop May 01 19:38:21 ubuntu systemd[1]: Stopping PythonTest... May 01 19:38:21 ubuntu systemd[1]: Stopped PythonTest.
練習二:不使用test.sh,直接運行test.py
python_test_two.service /lib/systemd/system/
root@ubuntu:/lib/systemd/system# cat python_test_two.service [Unit] Description=PythonTestTwo [Service] Type=simple Restart=on-failure ExecStart=/usr/local/bin/python3.7 /home/yanyanzhang/Desktop/test.py KillSignal=SIGN [Install] WantedBy=multi-user.target
加載服務,啟動服務
root@ubuntu:/lib/systemd/system# systemctl enable python_test_two.service Created symlink from /etc/systemd/system/multi-user.target.wants/python_test_two.service to /lib/systemd/system/python_test_two.service. root@ubuntu:/lib/systemd/system# systemctl start python_test_two root@ubuntu:/lib/systemd/system# systemctl status python_test_two ● python_test_two.service - PythonTestTwo Loaded: loaded (/lib/systemd/system/python_test_two.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2021-05-01 21:00:27 CST; 7s ago Main PID: 129073 (python3.7) Tasks: 1 Memory: 3.1M CPU: 17ms CGroup: /system.slice/python_test_two.service └─129073 /usr/local/bin/python3.7 /home/yanyanzhang/Desktop/test.py May 01 21:00:27 ubuntu systemd[1]: Started PythonTestTwo.
systemctl有一個缺點,而且 systemctl start xxx.service/systemctl stop xxx.service 啟動和停止服務,都沒有任何提示,還非得要 systemctl status xxx.service 來查看服務的運行狀態,非常不方便。
systemctl 不加.service也可以啟動,重啟,停止
systemctl enable name.service 啟用開機自啟動服務
systemctl disable name.service 停止開啟自啟動服務
systemctl is-enabled name.service 查看服務是否為開機自啟動
root@ubuntu:/# systemctl is-enabled python_test_two.service enabled
systemctl try-restart name.service 只重啟運行中的服務
systemctl --failed 查看啟動失敗的服務列表
systemctl --all 查看所以服務 空格翻頁,q退出
systemctl list-dependencies --after name.service 列出指定服務之前啟動的服務,依賴,空格翻頁,q退出
systemctl list-dependencies --before name.service 列出指定服務之后啟動的服務,被依賴,空格翻頁,q退出
下面來看看,.service各部分配置的含義
1. Unit : 啟動順序和依賴關系 Description字段:給出當前服務的簡單描述。 Description=Python_Test_Two Documentation字段:給出文檔位置。 After字段:表示本服務應該在某服務之后啟動。 Before字段:表示本服務應該在某服務之前啟動。 After和Before字段只涉及啟動順序,不涉及依賴關系。設置依賴關系,需要使用Wants字段和Requires字段。 Wants字段:表示本服務與某服務之間存在“依賴”系,如果被依賴的服務啟動失敗或停止運行,不影響本服務的繼續運行。 Requires字段,表示本服務與某服務之間存在“強依賴”系,如果被依賴的服務啟動失敗或停止運行,本服務也必須退出。 2. Service : 定義如何啟動/重啟/停止服務。 2.1 Type字段定義啟動類型。它可以設置的值如下。 simple(默認值):ExecStart字段啟動的進程為主進程。 forking:ExecStart字段將以fork()方式啟動,此時父進程將會退出,子進程將成為主進程。 oneshot:類似於simple,但只執行一次,Systemd會等它執行完,才啟動其他服務。 dbus:類似於simple,但會等待D-Bus信號后啟動。 notify:類似於simple,啟動結束后會發出通知信號,然后Systemd再啟動其他服務。 idle:類似於simple,但是要等到其他任務都執行完,才會啟動該服務。 2.2 ExecStart字段-啟動服務 啟動服務時執行的命令,可以是可執行程序、系統命令或shell腳本。 2.3 ExecReload字段-重啟服務 重啟服務時執行的命令,可以是可執行程序、系統命令或shell腳本。 2.4 ExecStop字段-停止服務 停止服務時執行的命令,可以是可執行程序、系統命令或shell腳本。 服務配置文件還可以讀取環境變量參數文件 3. Install :定義如何安裝這個配置文件,即怎樣做到開機啟動。 WantedBy=multi-user.target WantedBy字段:表示該服務所在的Target。 Target的含義是服務組,表示一組服務。WantedBy=multi-user.target指的是,Python_Test_Two所在的Target是multi-user.target(多用戶模式)。 這個設置非常重要,因為執行systemctl enable python_test_two.service命令時,python_test_two.service會被鏈接到/etc/systemd/system/multi-user.target.wants目錄之中,實現開機啟動的功能。
# TODO
# TODO
