CentOS7 使用SystemD實現開機自啟動和服務管理
ofollow,noindex">CentOS 7 可以在SystemD管理服務Service方式實現服務的自啟動
SystemD即為System Daemon,是linux下的一種init軟體,開發目標是提供
更優秀的框架以表示系統服務間的依賴關係,並依此實現系統初始化時服務的並行啟動,
同時達到降低Shell的系統開銷的效果。
這裡假設需要啟動
# /usr/share/autostartup/foobar.sh
STEP1.建立SystemD管理服務使用的.service檔案
/usr/share/autostartup/foobar.service
以下是測試service檔案,.service的具體編寫,參考systemd相關文件
[Unit] Description=foobar Documentation=http://www.doc.com/doc.html After=network.target [Service] Type=simple WorkingDirectory=/home/foobar ExecStart=/usr/share/autostartup/foobar.sh ExecStop=/bin/kill -s QUIT $MAINPID Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=foobar User=foobar Group=foobar Environment= [Install] WantedBy=multi-user.target
STEP2.將編寫好的service檔案移動到`/usr/lib/systemd/`下的指定目錄下, 這裡我們需要隨系統啟動,所以移動到
`/usr/lib/systemd/system/`下
cp /usr/share/autostartup/foobar.service /usr/lib/systemd/system/foobar.service
STEP3.配置和啟動相應的Service
# 重新載入systemd的配置 systemctl daemon-reload # 配置服務為開機啟動 systemctl enable foobar.service # 開啟服務 systemctl start foobar.service #關閉服務 systemctl stop foobar.service