systemdで1分毎にプログラムを動かす設定

例えば、my-programというサービスを定義して、1分毎に動かす場合の設定

1. /etc/systemd/systemの下に設定ファイルを2つ(my-program.service, my-program.timer)配置する

   1. 1. my-program.service

[Unit]
Description=My Program Service (Oneshot)
After=network.target

[Service]
Type=oneshot
ExecStart=/path/to/my_program
WorkingDirectory=/path/to
User=worker-user
Group=worker-group

   1. 2. my-program.timer

[Unit]
Description=Run My Program every minute

[Timer]
OnCalendar=*:0/1
Unit=my-program.service

[Install]
WantedBy=timers.target

2. systemctlでtimer設定を有効化

sudo systemctl enable --now my-program.timer

3. 設定確認

sudo systemctl list-timers my-program.timer

4. 標準出力ログ確認( -f は tail -fと同じ監視するオプション)

sudo journalctl -u my-program.service -f