简体   繁体   English

如何调试这个 shell 脚本在 cron 上不起作用?

[英]How to debug this shell script not working on cron?

I have a shell script below.我在下面有一个 shell 脚本。 This already works by doing sh start_script.sh这已经可以通过 sh start_script.sh

But I want this to work automatically using cron jobs.但我希望它能够使用 cron 作业自动工作。 Already made it executable using chmod +x已经使用chmod +x使其可执行

Below is shell script and the cron job:下面是 shell 脚本和 cron 作业:

2 * * * * /home/glenn/projects/database_dump/backup/script_backupMongoDB.sh
#!/bin/sh
TIMESTAMP=`date +%F-%H%M%S`
FILENAME=configurator_`date +%m%d%y%H`_$TIMESTAMP.tar.gz
DEST=/home/glenn/projects/database_dump/backup
SERVER=127.0.0.1:27017
DBNAME=siaphaji-cms


DUMPFOLDER=$DEST/$DBNAME
DAYSTORETAINBACKUP="7"

# DUMP TO DEST FOLDER
mongodump -h $SERVER -d $DBNAME --out $DEST;

# COMPRESS THE DUMPFOLDER AND NAME IT FILENAME
tar -zcvf $FILENAME $DUMPFOLDER

# DELETE DUMP FOLDER
rm -rf $DUMPFOLDER

find $DEST -type f -name '*.tar.gz' -mtime +$DAYSTORETAINBACKUP -exec rm {} +

Check the environment differences, especially the value of the PATH variable in each case (when it works, and when not).检查环境差异,尤其是每种情况下 PATH 变量的值(何时有效,何时无效)。

Processes launched via cron only have a default environment.通过 cron 启动的进程只有一个默认环境。 You should use a full path for all non standard commands, and add all the required environment variables (at least for mongodump ) in the script itself.您应该为所有非标准命令使用完整路径,并在脚本本身中添加所有必需的环境变量(至少对于mongodump )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM