
私はキャプチャした写真をネットワーク上のFTPサーバーに送信するセキュリティカメラを持っており、受信ディレクトリにある古いファイルを削除するスクリプトを開発しました。コマンドラインで実行したときにスクリプトが正しく実行されたため、スクリプトを1日2回実行するようにcrontabに行を追加しました。
# Remove old security images / videos from directory
1 7,19 * * * /home/ftp/bin/secpurg
しかし、スクリプトは機能しません。ディレクトリがいっぱいで、何が起こっているのかを確認するために #!/bin/bash -x を実行することにしました。私のメールに次のメッセージが表示され始めました。
+ fileAge=10
+ SecDir=/home/ftp/_Security/
+ maxDirSize=3000000
++ du -s /home/ftp/_Security/
++ cut -f1
/home/ftp/bin/secpurg: line 11: cut: command not found
/home/ftp/bin/secpurg: line 11: du: command not found
+ secDirSize=
+ '[' -ge 3000000 ']'
/home/ftp/bin/secpurg: line 14: [: -ge: unary operator expected
ヒュー? CRONでスクリプトを実行すると、「cut」と「du」が見つかりませんか?端末でスクリプトを実行すると、このコマンドが正常に機能しますが、CRONで実行すると機能しない理由について誰かが教えてくれますか?
参考になる場合に備えて、参照用のスクリプトを含めました。
#!/bin/bash -x
# secpurg - find and remove older security image files.
# Variable decleration
fileAge=10
SecDir="/home/ftp/_Security/"
maxDirSize=3000000
# Determine the size of $SecDir
secDirSize=`du -s $SecDir | cut -f1`
# If the size of $SecDir is greater than $maxDirSize ...
while [ $secDirSize -ge $maxDirSize ]
do
# remove files of $fileAge days old or older ...
find $SecDir* -mtime +$fileAge -exec rm {} \;
# Generate some output to email a report when files are deleted.
# set -x
# Expanding $SecDir* makes for big emails, so we don't do that, but echo the command for reference ...
echo -e "\t\t[ $secDirSize -ge $maxDirSize ]
fileAge=$fileAge
SecDir=$SecDir
maxDirSize$maxDirSize
find $SecDir* -mtime +$fileAge -exec rm {} \;"
# decrement $fileAge ...
fileAge=$(( $fileAge - 1 ))
# and re-determine the size of $SecDir.
secDirSize=`du -s $SecDir | cut -f1`
# Just in case things are crazy, don't delete todays files.
if [ $fileAge -le 1 ]
then
secDirSize=0
fi
# Stop generating output for email.
# set +x
done
-
編集する:
echo "PATH -$PATH-"
スクリプトの上部に追加すると、電子メールの最初の行が表示されます+ echo 'PATH -~/bin:$PATH-'
。今私の質問は私のPATHで何が起こっているのか、そして便利なディレクトリを追加するための推奨される方法は何ですか?私はこれが私のすべてのCRON作業に影響を与えると思います。
-
答え1
対話型セッションで動作するのでcron
。
PATH=
crontab
明示的なディレクトリを含む行がないことを確認してください(cron
パス割り当てはシェルのように追加されません)。
PATH=/home/myhomedir/bin:/usr/local/bin:/bin:/usr/bin
答え2
$PATH
エコーは を提供するので、~/bin:$PATH
ファイルに~/.bashrc
このような内容がある可能性が高いです。
PATH='~/bin:$PATH'
これは、単一引用符のために割り当て前のパスに拡張されないPATH
リテラル文字列に設定されます。~/bin:$PATH
$PATH
一重引用符を二重引用符に変更します。