web-dev-qa-db-ja.com

cron.hourlyの構成の何が問題になっていますか?

1時間ごとに、このようなエラーのあるメールが届きます。

Subject: Cron <root@supa> root    cd / && run-parts --report /etc/cron.hourly

/bin/sh: root: not found

/ etc/crontabの内容は次のとおりです。ユーザー「root」を削除するかどうか(6列目)、同じエラーが表示されます。

Shell=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
11 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

Cron.hourlyディレクトリには2つのファイルがありますが、

$ ll /etc/cron.hourly/
total 0
lrwxrwxrwx 1 root root 25 2009-10-29 09:24 ntpsync -> /home/<user>/bin/ntpsync
lrwxrwxrwx 1 root root 28 2009-10-23 10:33 foo -> /home/<user>/bin/foo

最初のスクリプトは次のようになります。

$ cat ~/bin/ntpsync
#!/usr/bin/env bash
echo "user: $USER"
if [[ "$USER" == "root" ]] ; then
    ntpdate ntp.ubuntu.com
else
    Sudo ntpdate ntp.ubuntu.com
fi

/etc/cron.hourly/ディレクトリから両方のスクリプトを削除しても、1時間ごとに同じエラーメールが届きます。 cronを再起動しようとしましたが、まだ同じエラーメールが届きます。私が持っている次のアイデアは再起動することですが、私はそれを避けます。

$ Sudo /etc/init.d/cron restart

私のUbuntuバージョンは次のとおりです。

$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=8.04
DISTRIB_CODENAME=hardy
DISTRIB_DESCRIPTION="Ubuntu 8.04.1"

更新:オンラインで検索したときに誰かが問題を解決できると言ったため、/ etc/crontabファイルから6列目の「ルート」を削除しました。問題は、ルートの設定ではなくシステムのcrontab設定をいじっていたことが問題だったと思います。

$ Sudo crontab -l
# m h  dom mon dow   command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
6
grokus

Cronパッケージのデフォルトのcrontabファイル(3.0pl1-100ubuntu2.1、これはubuntu 8.04の最新バージョンです)は次のようになります。

Shell=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

これを取得してファイルに貼り付けることができるはずですが、パッケージの最新バージョンを使用していることを確認することもできます。これを行うには、次を実行します。

apt-get update
apt-get install cron

更新:

Crontabには2つの異なるタイプがあり、1つは/etc/crontabにあるシステムのcrontabです。このcrontabには次のものがあります。

minute hour dayOfMonth month dayOfWeek userToRunAs restOfLineIsCommand

もう1つのタイプはユーザーcrontabです。これはcrontabを使用して変更できます。実際の構成は/var/spool/cron/crontabs/USERNAMEにあり、それを所有するユーザーとして常に実行され、そのファイルの形式は次のとおりです。

minute hour dayOfMonth month dayOfWeek restOfLineIsCommand
6
LassePoulsen

6列目の「ルート」を削除した後もエラーが表示されると言っていましたが、実際には問題のように見えます。

たとえば、他の行を見てください。それらはすべて「テスト」で始まります。それはユーザーではなく、コマンドの始まりです。 「ルート」を削除すると、コマンドは「cd」で始まります。

特に、存在しないプログラムを実行しようとしたときに表示されるエラーである「ルート」が見つからないというエラーメッセージが表示されるためです。

そのため、もう一度削除してみてください。

1
Michael Terry

あなたの/etc/crontabは確かに面白そうです。実際には、すべての行に実際にユーザー列があり、これが最もおもしろい部分です。たとえば、私の読み取り:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

Shell=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

ちなみに、通常、このファイルに触れることはお勧めできません。より一般的なcrontabを追加する必要がある場合は、/etc/cron.dを使用してください。以下を使用して、cronパッケージのデフォルトの構成を復元することができます。

$ Sudo apt-get install --reinstall --yes -o DPkg::Options::=--force-confmiss -o DPkg::Options::=--force-confnew cron

問題が解決するかどうかを確認してください。

1
ℝaphink

ここには、実際に2つの問題があります。 1つ(より明白な)は、ルートの個人crontabの不適切な6番目の列です。 2番目のサイレント-/etc/crontabの1時間ごとのcron行の後のコマンドが正しく実行されていません。修正は以下のとおりです。


Sudo crontab -rを実行すると、偽のユーザーcrontabファイルを削除できます。


それが完了したら、1時間ごとのcron行の後の各行の/etc/crontabファイルにrootユーザーを追加する必要があります-次のように:

Shell=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
11 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

これにより、これらの電子メールの問題が解決するはずです。

1
Marco Ceppi

それをする:

# crontab -r

そしてそれをしないでください

# crontab /etc/crontab

代わりに、ファイル/ etc/crontabを手動で編集します。

0
user11267