天天看點

Systemd 如何設定 開機啟動 守護程序

權限:systemd有系統和使用者區分;系統(/user/lib/systemd/system/)、使用者(/etc/lib/systemd/user/).一般系統管理者手工建立的單元檔案建議存放在/etc/systemd/system/目錄下面。

1、在/usr/lib/systemd/system/ 目錄下 建立對應service(例如我要設定 gogs 為開機啟動和守護程序,那麼就建立 gogs.service )

 切換目錄

cd /lib/systemd/system
           

建立對應的service

vi gogs.service
           

編輯 service

[Unit]
Description=Gogs
After=network.target

[Service]
# Modify these two values and uncomment them if you have
# repos with lots of files and get an HTTP error 500 because
# of that
###
#LimitMEMLOCK=infinity
#LimitNOFILE=65535
Type=forking
User=gogs
Group=gogs
WorkingDirectory=/usr/local/git/gogs
ExecStart=/bin/sh /usr/local/git/gogs/gogs.sh
Restart=always
Environment=USER=gogs HOME=/home/gogs

# Some distributions may not support these hardening directives. If you cannot start the service due
# to an unknown option, comment out the ones not supported by your version of systemd.
#ProtectSystem=full
#PrivateDevices=yes
#PrivateTmp=yes
#NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
           

wq 儲存退出

2、加載配置,執行配置。

systemctl daemon-reload
           
systemctl enable gogs.service
           
systemctl start gogs.service
           

  檢視 sercvice 狀态

systemctl status gogs.service
           

  如果輸出如下,那麼恭喜你就完成了

Systemd 如何設定 開機啟動 守護程式

附上nexus和sonarqube的配置

[Unit]
After=nertwork.target

[Service]
User=sonarqube
Type=forking
#EnvironmentFile=-/etc/profile
ExecStart=/bin/sh -c '. /etc/profile ; /usr/local/java/sonarqube/sonarqube-7.7/bin/linux-x86-64/sonar.sh start'
ExecStop=/bin/sh -c '. /etc/profile ; /usr/local/java/sonarqube/sonarqube-7.7/bin/linux-x86-64/sonar.sh stop'
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=sonarqube.service
           
[Unit]
After=nertwork.target

[Service]
User=nexus
Type=forking
ExecStart=/bin/sh -c '. /etc/profile ; /usr/local/java/nexus/nexus-3.16.2-01/bin/nexus start'
ExecStop=/bin/sh -c '. /etc/profile ; /usr/local/java/nexus/nexus-3.16.2-01/bin/nexus stop'
Restart=on-failure

[Install]
WantedBy=multi-user.target
Alias=nexus
           

具體service 各種配置項 請參考 http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

繼續閱讀