8
8

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.

ubuntuでcrontabの設定をする時はpathか絶対パスを使用すること。

Last updated at Posted at 2016-11-21

無駄なことに時間を使った。
動かないと思ったらパスが通ってないだけだった。

#使用上の注意
crontab -eは危ないので使わないほうがいい
そうなのですが、使ってます。間違って-rを使って削除してしまわないように。
http://qiita.com/furu-s/items/3aa9d55c9f3086cba711

cdで実行するディレクトリに移動する
あるいは絶対パスを指定して実行する必要がある。

実行するコマンドのほうもpathが通ってないと絶対パスにしないと実行されないので注意。

#crontabを開く

crontab -e 

#前準備
##環境変数の設定

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

##権限を変更
sudo chmod 755 実行するファイル

##ログが出るよう設定

edit or emacs or vi or...
emacs /etc/rsyslog.confを編集
cronのlogがコメントされているのでコメントアウト

/var/log/cron.logに出力されるようになる。

##ルール
分 時 日 月 曜日 ユーザ コマンド

#例

01 * * * * root ruby /home/ubuntu/test.rb
02 4 * * * root ruby /home/ubuntu/test.rb
22 4 * * 0 root ruby /home/ubuntu/test.rb
42 4 1 * * root ruby /home/ubuntu/test.rb
ユーザー名なしでも通った。(ラブパイのラズビアンで確認)
42 4 1 * * ruby /home/ubuntu/test.rb
**※上の例もrubyのpathが通ってないと通らない。絶対パスにするかpathに記載すること。**
0  11 * * * /bin/bash /home/ubuntu/bin/test.sh 

変数を使ってパスをまとめると良いとのこと。

LOG_DIR="/home/logs"
0   9 * * * /bin/bash $BIN_DIR/hoge.sh >> $LOG_DIR/hoge.log

参考
http://dqn.sakusakutto.jp/2012/06/cron_crontab9.html

8
8
0

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
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?