26
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Organization

systemdでtimerの作り方(最小限のサンプル)

実行するスクリプトを用意する

/usr/local/bin/myscript

#!/bin/sh

date >> /tmp/myscript.txt

実行権限を忘れずに

chmod 755 /usr/local/bin/myscript

サービスファイルを作る

/etc/systemd/system/myscript.service

[Unit]
Description=MyScript

[Service]
Type=simple
ExecStart=/usr/local/bin/myscript

[Install]
WantedBy=multi-user.target

サービスを有効にする

systemctl enable myscript

タイマーファイルを作る

/etc/systemd/system/myscript.timer

[Unit]
Description=Runs myscript

[Timer]
# ブート後、この時間後にこのタイマーを開始する
OnBootSec=1min
# 実行する時間の間隔
OnUnitActiveSec=10s
Unit=myscript.service

[Install]
WantedBy=multi-user.target

タイマーを有効にする

systemctl start myscript.timer
systemctl enable myscript.timer

動作確認

以上で10秒おきに/tmp/myscript.txtに時刻が書き込まれるはず。

tail -f /tmp/myscript.txt

タイマーを停止する

systemctl stop myscript.timer
systemctl disable myscript.timer
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
26
Help us understand the problem. What are the problem?