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?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Saman
  • 371

1 Answers1

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:

Kusalananda
  • 333,661