轉:https://www.jianshu.com/p/1958878646bd
1. 創建pfly.service文件

2. 執行 systemctl daemon-reload
3. 執行 systemctl enable pfly.service
重啟ubuntu系統,就可以看到pfly程序已經開機自動啟動了。oh yeah!!!
pfly是由go build -o pfly p.go 編譯出來的。
package main
import (
"fmt"
"time"
"os"
)
func main() {
for {
f, err := os.OpenFile("/root/test.txt", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
newLine := "File handling is easy." + time.Now().Format(time.RFC3339)
fmt.Fprintln(f, newLine)
time.Sleep(2*time.Second)
}
}
-----------------------------------------------------------------------------------------------------------------------
Ubuntu 16.04 增加bash腳本為service,開機自啟服務腳本配置
------------------------------------------------------------------------------------------------
1. 首先在/lib/systemd/system/目錄下,創建服務腳本:nginx-1.13.0.service
[Unit]
Description=nginx-1.13.0
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx-1.13.0/sbin/nginx -c /usr/local/nginx-1.13.0/conf/nginx.conf
ExecStop=/usr/local/nginx-1.13.0/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2. 設置讓腳本開機自動啟動
sudo systemctl enable nginx-1.13.0.service
3. 常用命令
重新加載service文件:systemctl daemon-reload
啟動一個服務:systemctl start nginx-1.13.0.service
關閉一個服務:systemctl stop nginx-1.13.0.service
重啟一個服務:systemctl restart nginx-1.13.0.service
顯示一個服務的狀態:systemctl status nginx-1.13.0.service
在開機時啟用一個服務:systemctl enable nginx-1.13.0.service
在開機時禁用一個服務:systemctl disable nginx-1.13.0.service
查看服務是否開機啟動:systemctl is-enabled nginx-1.13.0.service
查看已啟動的服務列表:systemctl list-unit-files|grep enabled
查看啟動失敗的服務列表:systemctl --failed
作者:baymin_
鏈接:https://www.jianshu.com/p/1958878646bd
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
