Linux中添加自定義服務Systemd

進入ARM板用戶目錄:

cd /lib/systemd/system/

1:編寫屬於自己的unit文件,命令爲my-demo.service,整個文件如下

[Unit]
Description=My-demo Service
[Service]
Type=oneshot
ExecStart=/bin/bash /root/test.sh #自己的腳本文件
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target

2:編寫unit文件中ExecStart=/bin/bash /root/test.sh所定義的test.sh文件,將其放在定義的目錄當中,此文件是服務的執行主體。文件內容如下:

#!/bin/bash 
date >> /tmp/date

3:將my-demo.service註冊到系統當中執行命令:

#systemctl enable my-demo.service

【注】如果是製作文件系統,進入etc/systemd/system/multi-user.target.wants目錄,再用以下命令:

#ln -s …/…/…/…/lib/systemd/system/systemd-config.service systemd-config.service

註冊的過程實際上就是將服務鏈接到/etc/systemd/system/目錄下
至此服務已經創建完成。重新啓動系統,會發現/tmp/date文件已經生成,服務在開機時啓動成功。當然本例當中的test.sh文件可以換成任意的可執行文件作爲服務的主體,這樣就可以實現各種各樣的功能。

【提示】

啓動:systemctl start my-demo.service
結束:systemctl stop my-demo.service
重啓:systemctl restart my-demo.service
狀態:systemctl status my-demo.service
查看服務狀態: systemctl status network.service
列出所有可用單元:systemctl list-unit-files
列出所有運行中單元:systemctl list-units
列出所有失敗單元:systemctl --failed
使用systemctl命令殺死服務:systemctl kill network.service

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章