/ 前言 /
在部署過程中, 很多應用都被要求配置開機啓動的的服務, 例如MySQL、ElasticSearch等, 每次去寫一個啓動服務無疑是一鍵麻煩的事情, 今天我們來看下如何通過Shell腳本來一鍵生成自啓動服務
/ 1 / 腳本
#!/bin/bash
service_file=/etc/systemd/system/init.service
chmod +x $sh_file
touch $service_file
echo '#!/bin/bash' >> $service_file
echo [Unit] >> $service_file
echo Description=init >> $service_file
echo [Service] >> $service_file
echo Type=forking >> $service_file
echo User=root >> $service_file
echo ExecStart=/opt/start.sh >> $service_file
echo [Install] >> $service_file
echo WantedBy=multi-user.target >> $service_file
systemctl enable init
systemctl start init
/ 2 / Service服務參數
-
[Unit]
- Description : 描述
- Documentation : 文檔
- Before : 指定在什麼服務之前啓動, 格式爲xxx.service
- After : 指定在什麼服務之後啓動, 格式爲xxx.service
- Requires : 啓動依賴的單元, 當依賴的單元停止後, 服務也將被關閉
- Wants : 啓動以來的單元, 當依賴的單元停止後, 服務
不會
被關閉
-
[Service]
- User : 執行命令或腳本的用戶, 例如Elasticsearch需要獨立的用戶啓動
- ExecStart : 啓動服務時執行的命令或者腳本
- 服務 : 如/opt/elasticsearch/bin/elasticsearch
- 腳本 : 如/opt/start.sh
- ExecReload : 重啓服務時執行的命令或腳本
- ExecStop : 停止服務時啓動的命令或腳本
- ExecStartPre : 啓動服務之前執行的命令或者腳本
- ExecStartPost : 啓動服務之後執行的命令或者腳本
- Type : 啓動類型
- simple(默認值) : 啓動腳本或命令時的進程爲主進程, 如果還要啓動其餘服務慎用此服務
- forking : 啓動腳本或命令時會以fork方式啓動, 父進程會退出
- notify : 啓動後會發出信號通知systemd
- oneshot : 只執行一次然後退出, 如果你要執行多個任務請不用使用該類型
- idle : 當所有服務都執行完後纔會啓動該服務
-
[Install]
- WantBy : 服務運行所在的服務組, 服務組列表在/etc/systemd/system/下
- Alias : 別名
/ 3 / 常用命令
-
開啓服務
systemctl enable init.service Created symlink from /etc/systemd/system/multi-user.target.wants/init.service to /etc/systemd/system/init.service.
該命令會建立一條鏈接到服務組
-
啓動服務
systemctl start init.service
如果報錯則說明服務啓動失敗, 無輸出則說明啓動成功
-
停止服務
systemctl stop init.service
-
查看服務狀態
systemctl status init.service ● init.service - init Loaded: loaded (/etc/systemd/system/init.service; enabled; vendor preset: disabled) Active: inactive (dead) since Wed 2020-05-20 05:03:22 EDT; 4s ago Process: 7439 ExecStart=/opt/start.sh (code=exited, status=0/SUCCESS)
-
重啓服務
systemctl restart init.service
-
重新加載服務
systemctl reload init.service
-
報錯信息查看
我們除了可以在
systemctl status init.service
中看到報錯信息外, 如果想看更詳細的信息可以執行journalctl -xe
命令查看詳細信息Subject: Unit init.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit init.service has begun starting up. May 20 04:57:31 localhost.localdomain systemd[7351]: Failed at step EXEC spawning /opt/start.sh: No such file or directo -- Subject: Process /opt/start.py could not be executed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- - The process /opt/start.py could not be executed and failed. -- -- The error number returned by this process is 2. May 20 04:57:31 localhost.localdomain systemd[1]: init.service: control process exited, code=exited status=203 May 20 04:57:31 localhost.localdomain systemd[1]: Failed to start zstack a. -- Subject: Unit init.service has failed -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit i.service has failed. -- -- The result is failed. May 20 04:57:31 localhost.localdomain systemd[1]: Unit init.service entered failed state. May 20 04:57:31 localhost.localdomain systemd[1]: init.service failed. May 20 04:57:31 localhost.localdomain polkitd[6040]: Unregistered Authent
在第5行我們可以看到詳細的報錯原因, 沒有這個文件
-
報錯後重新啓動
- 修改init.service文件
- 執行
systemctl daemon-reload
命令 - 執行
systemctl start init.service
命令