When I run a /bin/bash script, it works fine and it logs into some log files inside the script. But, when I run it from cron, it does not log to the file! it only logs to /var/mail/root , saying that

Date: Fri, 12 Aug 2016 08:39:01 +0300 (MSK)

/bin/sh: 1: root: not found

this is the script:


echo "started testing cron" >> ${LOG_FILE}

pgrep tunnel
if [[ ${?} != 0 ]]; then
  echo "Tunnel process is not running..." | tee -a ${LOG_FILE}
  echo "initializing tunnel..." | tee -a ${LOG_FILE}
  /usr/local/bin/stunnel | tee -a ${LOG_FILE} 2>&1

echo `date` >> ${LOG_FILE}

and this is the cron:

45 8 * * *  /home/ubuntu/sam/scripts/sqlplus-scripts/accts-ct/test-crontab.sh > /dev/null
  • 1
    Two issues: (1) the script is running under sh but is using bash features, and (2) LOG_FILE should specify a complete path. (Neither of these issues, though, explains the error message.) – John1024 Aug 12 '16 at 6:01
  • Did you do chmod u+x /home/ubuntu/sam/scripts/sqlplus-scripts/accts-ct/test-crontab.sh? – Alex Lowe Aug 12 '16 at 6:24
  • @Alex - yes it has x permission for all. – Sam Aug 12 '16 at 6:40
  • @john- it creates the file in the same folder script runs. So, that is not the issue. Script runs as expected when executed manually. about the sh, I think cron uses sh instead of bash. I don't know how to make it compatible – Sam Aug 12 '16 at 6:41
  • "it creates the file in the same folder script runs" According to the question, it wasn't writing to the file. Are you saying that the cron job does succeed in creating a file but fails to write to it? – John1024 Aug 12 '16 at 7:13
  1. To get the log file where you expect it to be, replace:



  2. The command [[ ${?} != 0 ]] is bash-only. From the error message that you quote, the script appears to be running under /bin/sh. One way to fix that is to replace:

    if [[ ${?} != 0 ]]; then


    if [ ${?} != 0 ]; then

    Another approach is to run crontab -e and add the following line to your crontab file:

  3. Also, for simplicity and style, consider replacing:

    echo `date` >> ${LOG_FILE}


    date >> "${LOG_FILE}"

    This eliminates a useless use of echo.

  4. Lastly, consider putting double-quotes around all shell variables, particularly $LOG_FILE. Because the current value of LOG_FILE contains no spaces or shell-active characters, this is not needed now. But, putting double-quotes around them will will prevent unpleasant surprises in the future.


I think that your crontab entry is wrong.

The format of the ubuntu crontab file is "m h dom mon dow user command", where the first 5 entries specfy the time the job is run. The 6th entry is the user who runs the job (I think that this is missing in your entry) and the 7th entry is the command you want to run.

I think that you should correct your crontab line to

45 8 * * * root /home/ubuntu/sam/scripts/sqlplus-scripts/accts-ct/test-crontab.sh > /dev/null

assuming that you want the command to run as root

  • The user field is only present in crontab files saved in /etc/cron.d. In user-specific crontab files, the user field isn't present: The file's owner is the user. – FKEinternet Jan 24 '20 at 23:03

The working directory in my case was /home/me so a relative path like test-crontab.log is mostly going to be resolved to /home/me/test-crontab.log

I don't like the idea of changing the paths in the files and make it less portable, instead change the directory on the cronjob line

cd /path/to/directory && ./bin/myapp

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.