I've some scripts that run perfect if you run them from a terminal, but once you add them to crontab it's like the bash doesn't execute properly and doesn't continue with the first IF statement and the script looks to stop.


## File: opt_files_backup.sh
## Author: Raul Sanchez
## Last update: 09/12/2015
## Description:

## Includes the configuration file for the FTP connection
## Available variables (FTP): $ftp_user, $ftp_pass, $ftp_server, $ftp_source_directory, $ftp_target_directory
## Available variables (mail): $mail_recipients
## Available variables (Logging): $log_files
source config.sh

## Creates a tar.gz backup with the format opt_files_YYYYmmdd.tar.gz
if tar czfP /opt/backup-scripts/src/files/opt_files-`date +%Y%m%d%H%M`.tar.gz /opt --exclude='/opt/backup-scripts';
echo -e "`date` - Files Backup OK" >> $log_files
## Remove files older than 7 days
if find /opt/backup-scripts/src/files/*.tar.gz -mtime +7 -exec rm {} \;
    ##------ Connect to remote server and synchronize ------##
            ## ftp::ssl-allow is false by default on /root/.lftprc
    if lftp -e "mirror --reverse --delete --verbose $ftp_source_directory $ftp_target_directory; exit" -u $ftp_user,$ftp_pass $ftp_server;
        echo -e "`date` - Files Backup Mirror Transfer OK" >> $log_files
                ##------ Send email notification to Sys Admin ------##
            echo "The task completed successfully at `date`" | mail -e -s "CRONTAB task #2 OPT Files Mirror" $mail_recipients
        echo -e "`date` - Files Backup Mirror Transfer KO" >> $log_files
        exit 1
echo -e "`date` - Files Backup Mirror Backup KO" >> $log_files
##------ Send email notification failure to Sys Admin ------##
echo "The task didn't completed at `date`, something went wrong. Please check $log_files for more information." | mail -e -s "CRONTAB task #2 OPT Files Mirror Failed!"  $mail_recipients
exit 1

As I've said, this script works perfectly if you run it from a terminal, but it doesn't with crontab. The cronjob for this script is:

0   7,10,13,16  *   *   1-5 /opt/backup-scripts/scripts/opt_files_backup.sh

I've changed the SHELL in /etc/crontab from SHELL=/bin/sh to /bin/bash and still doesn't work.

Could anyone help me to find what's going wrong with my script and crontab?


1 Answer 1


When writing a scrpit for cron there are certain things you must remember:

  1. Never assume it is running from any particular place. Either manually change to a pre-defined location (cd) with one of your first operations, or always use absolute pathnames for everything. That includes your "source config.sh" line - where is that config.sh file...?
  2. It may not have the same PATH environment variable as when you run it manually. If you reference anything that is not in the standard system binary locations (/bin /usr/bin) then you will either have to manually specify a different PATH variable or again use full absolute path names for your commands. That even includes /sbin and /usr/sbin - some systems don't have that in the default PATH for non-root users.

For debugging make sure that the MAILTO variable is set in the crontab so that it knows where to send any error reports to. You can also add "-x" to your hashbang line to show all the commands on stdout as they are executed - which cron should then mail you.

  • Thanks for your help, I solved the problem as you suggested: 1. Adding the PATH environment variable, checking first where the invoked applications where located. 2. Adding the absolute pathname for the config.sh After that, I added to the crontab the MAILTO just to know if it was executed and check the syslog to know if there was something going wrong. Everything worked as a charm. Thanks so much! Dec 11, 2015 at 12:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .