Search code examples
linuxbashshellcron

How to debug this shell script not working on cron?


I have a shell script below. This already works by doing sh start_script.sh

But I want this to work automatically using cron jobs. Already made it executable using chmod +x

Below is shell script and the cron job:

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 {} +

Solution

  • Processes launched via cron only have a default environment. 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.