I have scheduled to run the run.sh program like this:

16 09 07 * * root /opt/db_maintain/run.sh > /opt/db_maintain/temp-log

and here is the run.sh:

#********* Saman *********
TM=$(date --date='40 days ago' '+%F %T')
source $TARGET "$TM"

I have also granted the execute permission to the following files: run.sh main.sh

When I run the program manually, it redirects from run.sh to main.sh with no problems. However, after scheduling it, crontab runs successfully as I checked it with putting some echo statements in run.sh, but I have no idea why run.sh cannot redirect to main.sh, even when I give main.sh execute privileges.

Do you have any idea?

Update: I found out the problem, but I do not know why it behaves like this. In run.sh I was writing #!/bin/bash, but in main.sh, I was writing #!/usr/bin/env bash after I changed #!/bin/bash in run.sh to #!/usr/bin/env bash, it started working. why is this happening? what is the difference between them?

  • You can see this answer. – taliezin Apr 7 '15 at 6:10
  • 1
    This does not make any sense.  If #!/bin/bash worked when you ran run.sh manually, then it should work just fine under cron. – Scott Apr 7 '15 at 6:57
  • changing relative paths to absolute solved the problem. but, they should both act the same as all files are residing in the same directory. do you have any idea? – Saman Apr 8 '15 at 7:49

The #!-line in main.sh does not matter at all since you're using source to run it in the same execution environment as run.sh.

Your run.sh, as presented in the question, does not have a #!-line at all. It has a comment reading


as its first line.

This ought to mean that it will be executed by /bin/sh. The /bin/sh shell does not have a source command and you should therefore get some kind of "command not found" error from the script when cron runs it (check root's email).


| 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.