LoginSignup
6
5

More than 5 years have passed since last update.

cron.dに書いたcronの実行結果にgetpwnamがエラーとして載ってしまう場合

Posted at

前提

あるサーバーのあるディレクトリの使用量を定期的に取得しようとして/etc/cron.dにcronを設定した時に、メールが上手く飛んでこなかった時の作業メモ。

今回ぶつかった壁

/root/bin/check.sh
#!/bin/bash
du -h /home/user

というシェルスクリプトを

/etc/cron.d/check
MAILTO="user@example.com"
0 1 1 * * /bin/sh /root/bin/check.sh

毎月1日の1時に実行しよう

/var/log/cron
Oct 1 01:00:01 localhost CROND[990]: (root) CMD (/bin/sh /root/bin/check.sh)
Oct 1 01:00:01 localhost crond[990]: (/bin/sh) ERROR (getpwnam() failed)

ダメだった。

環境

OS
CentOS7

やったこと

どうやらcron.d/とcrontabでは微妙に書き方が違うらしい。
参考:各ユーザのcrontabファイルの場所について

大雑把にまとめるとこんなイメージ?

cron.d/ crontab
ユーザーを問わず利用できる ユーザー毎にファイルがある

なのでcron.dに書く内容を以下の様に修正してみる。

/etc/cron.d/check
MAILTO="user@example.com"
0 1 1 * * root /bin/sh /root/bin/check.sh

するとエラーも出ず、うまいことメールが飛んでくる様になりました。

結論

crontabはユーザー毎にファイルが存在しているため、ユーザー指定をしなくても問題ないが、cron.d/以下はユーザーを問わず(Apacheとかね)実行させるcronを置くことができる為、cronの中にuserで実行しますよ~という指示を与えてやる必要がある。

6
5
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
6
5