Cron doesn't run job in background

Hi,

First of all merry christmas and Happy holidays to all :smiley:

My situation is as below,
When a backup job runs on a mainframe server, it creates a 0byte file on a network drive which is accessible through linux

Linux server : Red Hat Enterprise Linux Server release 5.3 Beta (Tikanga)
File : /ntwrk/common/backup_in_progress.txt

Once the backup job is complete the file is removed.

To monitor the backup timings I made two shell scripts as below

#chk_job_start.sh <------ to catch start time

#/bin/bash
while [ 0 ]
do
     #if 0b file exists
     if [ -e /ntwrk/common/backup_in_progress.txt ]
        then
        #Record the start time
        tm=`date "%H%M"`
        echo "$tm" > /home/sam/Backup_Started
        #initiate job2
        /bin/bash chk_job_end.sh &
        #exit
        exit 0
else
        #loop every 10 secs unless the 0b file is found
        sleep 10
fi
#chk_job_end.sh
#!/bin/bash
while [ 0 ]
do
#if 0b file still exists

if [ -e /ntwrk/common/backup_in_progress.txt ]
then
       #loop every 10 secs unless the 0b file is not found
       sleep 3
else
       #Record the finish time
       tm=`date "+%Y%m%d %H%M"`
       echo "$tm" > /home/sam/Backup_Finished

       #exit
       exit 0
fi
done

I am running job start script through cron as

01 00 * * * /bin/bash /home/sam/chk_job_strt.sh

Problem is the 2nd script(chk_job_end.sh) never get's executed.

When I test this outside cron, the test results are proper i.e. both scripts are executed and time is recorded.

Plz help.

Thanks
Sam

#/bin/bash
while [ 0 ]
do
#if 0b file exists
     if [ -e /ntwrk/common/backup_in_progress.txt ]
     then
        #Record the start time
        tm=`date "+%Y%m%d %H%M"`
        echo "$tm" > /home/sam/Backup_Started
        while [ -e /ntwrk/common/backup_in_progress.txt ]; do
           echo "test" > /dev/null #do something till the file goes off :D
        done
        #Record the end time
        tm=`date "+%Y%m%d %H%M"`
        echo "$tm" > /home/sam/Backup_Finished
        exit 0
else
        #loop every 10 secs unless the 0b file is found
        sleep 5
fi

No need for the second script :slight_smile:

1 Like

Thanks Pikk45 :slight_smile:

PikK45's solution is much better, but you could also fix your problem by changing the line in chk_job_start.sh :

        /bin/bash chk_job_end.sh &

to:

        /bin/bash /home/sam/chk_job_end.sh &

When a job is started by cron , your PATH environment variable isn't set as it is when you login and run commands (so chk_job_start.sh probably isn't able to find chk_job_end.sh to run it).

Thanks Don for the fix :slight_smile:

Yeah... Don's right.. We could have set a working directory at beginning of the script. That would save all the confusions. Add some logging too :slight_smile: Helps in debugging

#!/bin/bash
EXEC_DIR=/home/sam
cd $EXEC_DIR

/bin/bash $EXEC_DIR/chk_job_end.sh &

I agree with Don.

Only add that should redirect the standard output and standard error:

01 00 * * * /bin/bash /home/sam/chk_job_strt.sh > /home/sam/chk_job_strt.out 2>&1

Greetings!