0

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:

#/bin/bash
#********* Saman *********
TM=$(date --date='40 days ago' '+%F %T')
TARGET=/opt/db_maintain/main.sh
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?

3
  • You can see this answer.
    – taliezin
    Commented Apr 7, 2015 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. Commented Apr 7, 2015 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
    Commented Apr 8, 2015 at 7:49

1 Answer 1

1

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

#/bin/bash

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

Related:

You must log in to answer this question.

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