systemctl

systemctl 背景執行

用於設定服務開機、關機自動執行的應用程式,ubuntu 內建

設定

編輯路徑/etc/systemd/system/echo_server.service


[Unit]
Description=logstash

[Service]
Type=simple
ExecStart=/opt/module/logstash/bin/logstash -f config/test3.conf
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

# 要求 systemd 重新載入設定
sudo systemctl daemon-reload

# 啟用開機自動執行
sudo systemctl enable beeapp.service

# 關閉開機自動執行
sudo systemctl disable beeapp.service

# 啟動
sudo systemctl start beeapp.service

# 停止
sudo systemctl stop beeapp.service

# 查詢
systemctl list-units --type=service --state=running

300-Resource/Linux/resource/systemctl 1.png

[Unit]
# 服務名稱
Description=Your Server

# 服務相關文件
# Documentation=https://example.com
# Documentation=man:nginx(8)

# 設定服務啟動的先後相依姓,例如在網路啟動之後:
# After=network.target

[Service]
# 行程類型
Type=simple

# 啟動服務指令
ExecStart=/opt/your_command

# 服務行程 PID(通常配合 forking 的服務使用)
# PIDFile=/run/your_server.pid

# 啟動服務前,執行的指令
# ExecStartPre=/opt/your_command

# 啟動服務後,執行的指令
# ExecStartPost=/opt/your_command

# 停止服務指令
# ExecStop=/opt/your_command

# 停止服務後,執行的指令
# ExecStopPost=/opt/your_command

# 重新載入服務指令
# ExecReload=/opt/your_command

# 服務終止時自動重新啟動
Restart=always

# 重新啟動時間格時間(預設為 100ms)
# RestartSec=3s

# 啟動服務逾時秒數
# TimeoutStartSec=3s

# 停止服務逾時秒數
# TimeoutStopSec=3s

# 執行時的工作目錄
# WorkingDirectory=/opt/your_folder

# 執行服務的使用者(名稱或 ID 皆可)
# User=myuser

# 執行服務的群組(名稱或 ID 皆可)
# User=mygroup

# 環境變數設定
# Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"

# 服務輸出訊息導向設定
# StandardOutput=syslog

# 服務錯誤訊息導向設定
# StandardError=syslog

# 設定服務在 Syslog 中的名稱
# SyslogIdentifier=your-server

[Install]
WantedBy=multi-user.target