株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

コマンドやシェルを自動実行させるときに使うcrontab。
サーバ単位での設定とユーザ単位の設定ができると思えばいいかも。

・/etc/crontab
書き方は以下の通りで、コマンドを実行させるユーザを記載する必要がある
分 時 日 月 曜日 ユーザ コマンド

[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
  2  *  *  *  * root sleep.sh

・crontab -e
書き方は以下の通りで、コマンドを実行させるユーザを記載はない。ユーザ単位に設定するからね
分 時 日 月 曜日 コマンド

#crontab -eで編集
[root@localhost ~]# crontab -e
0 * * * * sleep.sh

#/var/spool/cron/root配下にユーザ名のファイルが生成
[root@localhost ~]# ls -l /var/spool/cron/root
-rw-------. 1 root root 20 10月 19 13:32 /var/spool/cron/root

#内容は編集した内容
[root@localhost ~]# cat /var/spool/cron/root
0 * * * * sleep 100

#crontabの内容確認
[root@localhost ~]# crontab -l
0 * * * * sleep 100

#crontab削除 ※確認なく消されちゃうのでかなり危険かも
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
[root@localhost ~]#

 

もちろん、両方設定しとけば両方実行されるよ。
/etc/crontabにまとめたほうがいいんじゃね。と思った。

<スポンサーリンク>

2 Comments

2 Records

  1. on 2020年1月28日 at 9:24 PM
    たに wrote:

    キーボードの「E」と「R」がとなりなので、「crontab -e」と入力すべきところを「crontab -r」と誤入力し、本番機のcrontabを吹っ飛ばした先輩がいました…。

    返信
    • on 2020年3月31日 at 1:27 AM
      yuta wrote:

      アツいっす!

      返信

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)