Cron doesn't run job in background


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron doesn't run job in background
# 1  
Old 12-26-2013
Question Cron doesn't run job in background

Hi,

First of all merry christmas and Happy holidays to all Smilie

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
Code:
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
Code:
#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




Code:
#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
Code:
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
# 2  
Old 12-26-2013
Code:
#/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 Smilie
This User Gave Thanks to PikK45 For This Post:
# 3  
Old 12-27-2013
Thanks Pikk45 Smilie
# 4  
Old 12-27-2013
PikK45's solution is much better, but you could also fix your problem by changing the line in chk_job_start.sh:
Code:
        /bin/bash chk_job_end.sh &

to:
Code:
        /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).
# 5  
Old 12-27-2013
Thanks Don for the fix Smilie
# 6  
Old 12-27-2013
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 Smilie Helps in debugging

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

/bin/bash $EXEC_DIR/chk_job_end.sh &

# 7  
Old 01-07-2014
Cron output is set to syslog, if not redirect

I agree with Don.




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

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

Greetings!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cron job cannot run the command

Hi, I created this cron job for asterisk to send sms daily to a number #!/bin/sh #custom mod - send sms once a day, at 07:00. CRON_PATH="/etc/asterisk/gw/crontabs_root"; if ! grep 'gsm send sms' $CRON_PATH > /dev/null 2>&1 ;then echo "* 7 * * * asterisk -rx 'gsm send sms 1 7666... (4 Replies)
Discussion started by: jazzyzha
4 Replies

2. Shell Programming and Scripting

Cron Job to Run every 2 minutes

Hello Gurus, I want to schedule a cron job which will run every 2 minutes starts at 11:25 AM and 3:25 AM daily. Can you please suggest as how to schedule the job. Thanks- Pokhraj Das (2 Replies)
Discussion started by: pokhraj_d
2 Replies

3. Shell Programming and Scripting

Cron Job to Run for 30 minutes

Hello Gurus, I have a requirement to run a job from cron only for 30 minutes duration daily twice at 8.35 am and 20.35 pm Can you please suggest how to schedule the job? Thanks- Pokhraj (5 Replies)
Discussion started by: pokhraj_d
5 Replies

4. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

5. Solaris

Is user cron job running in background?

Hi, Should the user jobs specified in crontab be running in background? Cron daemon is already running in background. So I am not sure whether should the jobs (output and error messages are redirected to file) ran by it be explicitly stated to be run in background (& at end of command) if one... (1 Reply)
Discussion started by: joe_x
1 Replies

6. UNIX for Advanced & Expert Users

Need help with a script run by a cron job.

Hi, new to this forum and not sure if this is the right place to post. I'm new to cron jobs and scripts, and I need some help with a script to be used with a cron job. I already have a bot set up at a certain website address and need a script that the cron job will load every day that tells it to... (1 Reply)
Discussion started by: klawless
1 Replies

7. Shell Programming and Scripting

Script doesn't work as expected when run on cron

The script checks for free space stats on Oracle. If there are any tablespaces with more than 85% usage it prints the details of the tablespace. If all the tablespaces have more than 15% free space, then "All tablespaces have more than 15 pct free space" must be printed on the screen. When I run... (2 Replies)
Discussion started by: RoshniMehta
2 Replies

8. Shell Programming and Scripting

Does not run via cron job

I have a perl script, when I ran manually it works perfect. I check the permissions which are fine. How can I find out why it is not running? how can I setup a log to check. I am running it on solaris 9. It compares multiple files, SCP and then send out an e-mail. As I said when I ran it... (2 Replies)
Discussion started by: amir07
2 Replies

9. Shell Programming and Scripting

Run a job in background for infinte time

Hello All, I would like to know if it is possible to launch a job in background for infinite time. example: myScript.ksh while true do ( echo "(`date`)"; top -U user | grep 'Memory:' ) >> log & sleep 1800 done and when i do... (5 Replies)
Discussion started by: midhun_u
5 Replies

10. UNIX for Advanced & Expert Users

Run cron job problem

I have the below crontab job that it will run at every 7:00am - 10:00am , it work fine , now if I want to skip to run the crontab job at a specific time , eg. I want the script not to run at next Monday 8:00am ( only skip this time , other time is normal ) , how can I make it ? is it possible ?... (3 Replies)
Discussion started by: ust
3 Replies
Login or Register to Ask a Question