38
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-05-20

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

/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
38
27
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?