Software engineering notes

Linux systemd

設定檔

/etc/systemd/system/test.service:

[Unit]
Description=test

[Service]
User=ec2-user
ExecStart=/bin/sh -c "/home/ec2-user/my_app/test >> /tmp/test.log 2>&1"
Restart=always

[Install]
WantedBy=multi-user.target

其他 [Service] 參數

WorkingDirectory=/home/ec2-user/my_app

其他參數

主機啟動時在 network 後才啟動

[Unit]
After=syslog.target network.target remote-fs.target nss-lookup.target

指令

重讀設定檔(修改後都要執行)

sudo systemctl daemon-reload

查看 systemd 執行 log

sudo systemctl status test

Enable it to run at boot:

sudo systemctl enable test

Start it

sudo systemctl start test

Stop it

sudo systemctl stop test

log

預設 systemd log 路徑

/var/log/syslog

將標準輸出指定到指定的路徑

解法是拿 shell 包一層, 程式裡面的輸出會輸出到你指定的位置, 但 syslog 就不會有了, 只會剩 systemd 的一些 log

User=ec2-user   (指定 log 的 user)
ExecStart=/bin/sh -c "/app_path/test >> /tmp/test.log 2>&1"

以下的方式是沒有用的

ExecStart=/app_path/test >> /tmp/test.log