crontab strangely does not execute my script, although the script is running just fine from the shell terminal. I already made sure that scripts are separated from each other by a new line space. However, I am suspicious about the contents of my script. crontab executes run.sh which would later run main.sh.

This is the /etc/crontab:

* * */3 * *        root  source /opt/db_maintain/run.sh

This is the contents of run.sh which would call main.sh from inside:

#!/usr/bin/env bash

#********* Saman *********
TM=$(date --date='40 days ago' '+%F %T')
TIMESTAMP=$(echo ${TM} | tr --delete ': -')
source $TARGET "$TM"

This is the beginning of main.sh:

##!/usr/bin/env bash
# main program entry point

source /opt/db_maintain/functions.sh
source /opt/db_maintain/constants.sh
source /opt/db_maintain/settings.sh
source /root/PASSWD_PGRS.sh

#read -s -t 0 -n 9999
#read -s -p "Enter password for ${USERNAME}: " PASSWORD

Use this syntax in the cronjob:

* * */3 * *        root  /bin/bash /opt/db_maintain/run.sh

You used source. That is a shell builtin command from bash. Therefore it can only be executed inside a bash shell or via a bash shell. The crontab just executes binaries, not shell commands. So you have to call a binary (/bin/bash), preferably by its abolute path.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.