CentOS7.6自定義系統啟動項的方法(類似busybox裡面的inittab)
1.編寫屬於自己的unit服務檔案,命令為my.service
[Unit]
Description=My-demo Service                                                                                   
[Service]
Type=oneshot
ExecStart=/bin/bash /home/book/workspace/test.sh #自己的指令碼檔案
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target

2.將上述的檔案拷貝到 /usr/lib/systemd/system/目錄下

3.編寫unit檔案中ExecStart=/bin/bash /home/book/workspace/test.sh希望之星的的test.sh檔案,將其放在定義的目錄當中,檔案中可實現我們的自定義操作。
#!/bin/bash                                                                                                                                       
echo "Hello world"

4.將my.service註冊到系統當中執行命令:
systemctl enable my.service
輸出:ln -s'/usr/lib/systemd/system/my.service' '/etc/systemd/system/multi-user.target.wants/my.service'
註冊的過程實際上就是將服務連結到/etc/systemd/system/目錄下
重新啟動系統,會發現cat /var/log/messages 有Hello woeld輸出,表明服務在開機時啟動成功。
當然本例當中的test.sh檔案可以換成任意的可執行檔案作為服務的主體,這樣就可以實現各種各樣的功能。

5.其他指令:
啟動:systemctl start my.service
結束:systemctl stop my.service
重啟:systemctl restart my.service
狀態:systemctl status my.service
檢視網路服務狀態: systemctl status network.service
列出所有可用單元:systemctl list-unit-files
列出所有執行中單元:systemctl list-units
列出所有失敗單元:systemctl --failed
使用systemctl命令殺死網路服務:systemctl kill network.service