Debian – Task Not Running by Crontab: Troubleshooting Guide

crondebianshell-script

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')
TARGET=/opt/db_maintain/main.sh
TIMESTAMP=$(echo ${TM} | tr --delete ': -')
export TIMESTAMP
source $TARGET "$TM"

This is the beginning of main.sh:

#!/bin/bash
##!/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

Best Answer

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.

Related Question