為了要讓 NetCore 背景執行利用了 Systemd 將服務掛在背景執行,應用程式的執行紀錄可以利用 journal 來觀看。
- 查詢版本
1systemctl --version - 已註冊清單
12systemctl list-unit-filessystemctl list-units --type=service - 設定檔
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647[Unit]# 應用描述Description=A test program# 應用文檔地址Documentation=https://docs.example.com# 必須在某一個或多個單元之後啟動After=network.target ssh.service# 依賴於某一個或多個單元Requires=ssh.service[Service]# 進程啟動方式,有:simple(默認), forking, oneshot, notify, dbus, idle這幾種方式Type=simple# 啟動服務之前執行的命令ExecStartPre=/usr/bin/which hello# 啟動程序ExecStart=/user/local/bin/hello# 啟動服務之後執行的命令ExecStartPost=/bin/echo "start hello success."# 停止服務時執行的命令ExecStop=/bin/echo "stop hello"# 重啟服務時執行的命令ExecReload=/bin/kill -s HUP $MAINPID# 進程結束後重啟方式,可選值有:always, on-success, on-failure, on-abnormal, on-abort, on-watchdogRestart=on-failure# 重啟間隔秒數RestartSec=30# 標準輸出到StandardOutput=syslog# 標準錯誤輸出到StandardError=syslog# 設置syslog中log的程序名稱SyslogIdentifier=helloexample# 設置syslog中log類型SyslogFacility=local0# 設置syslog中log級別,此處為infoSyslogLevel=info# 程序運行時的用戶User=root# 程序運行時分配的組Group=root# 程序的環境變量Environment=NODE_ENV=production# 依賴於[Install]WantedBy=multi-user.target - 目前 Log 大小
1journalctl --disk-usage - 僅顯示指定服務產生的訊息
1journalctl _SYSTEMD_UNIT=avahi-daemon.service
來源: