Sponsored Content
Full Discussion: Cascaded Cron Jobs
Top Forums Shell Programming and Scripting Cascaded Cron Jobs Post 302307585 by jeffm on Wednesday 15th of April 2009 09:01:56 PM
Old 04-15-2009
As others have stated, use a file to tag to see if the process completed or is still running. If you can modify your java code, you can have the java code write the little status files. If that's not possible, just wrap it all up in a shell script. Something like;

For the 1st java process
Code:
#!/bin/sh

echo "running" > /var/tmp/process-status.txt

## Insert the method to launch your java code here.
## e.g. $JAVA_HOME/bin/java -jar /path/to/jarfile.jar

echo "complete" > /var/tmp/process-status.txt

And for the 2nd process, something like;
Code:
#!/bin/sh

COUNTER=0
# Do this 10 times. Keeps the process from running forever...
while [ $COUNTER -lt 11 ]; do
     if [ -f /var/tmp/process-status.txt ]; then

        # Nothing fancy, just snag the return code from grep.
        # send the output to /dev/null, because we don't really 
        # care about it.
        grep complete /var/tmp/process-status.txt &> /dev/null
    
        # if grep found complete, $? will be 0, otherwise 1.
        if [ $? -eq 0 ]; then
             # Insert the code to run your 2nd java app.
             # e.g. $JAVA_HOME/bin/java -jar /path/to/jar.file
          
              # remove the status file, so we don't get confused 
              # the next time we run.
              rm /var/tmp/process-status.txt
              
              # the break here will "break out" of the loop, regardless of the
              # status of the $COUNTER variable.
              break
        fi
    fi
    
    # the file either didn't exist, or the file doesn't have "complete" in it, so
    # we'll wait for 60 seconds and try again.
    sleep 60

    # increase our counter variable by 1.
    let COUNTER=COUNTER+1
done

if [ $COUNTER -eq "11" ]; then
   # Our counter variable got too big, which means that the 
   # loop would stopped. With the sleep in there, we waited 10 minutes 
   # for the earlier code to populate the file with "complete". We should
   # Probably generate an error of some type.
   #
   # If cron is setup properly, it'll email all output to root@localhost, so 
   # we'll just let cron handle notifying the operator.
   #
   echo "WARNING: $0 failed to run. Something is broken."
fi

So the 1st script creates a /var/tmp/process-status.txt file, with the word "running" in it. It then launches your java process. When the java process exits, it writes "complete" into the file.

The 2nd script will only start the 2nd java process if it can find the word "complete" in the /var/tmp/process-status.txt file. If it finds the word "complete" in the file, it will start up the 2nd java process. It'll do this check 10 times, waiting 60 seconds between checks. If all 10 checks fail, then it'll spit out an error. If cron is configured properly, cron will automatically generate an email to root@locahost with any output. You could probably run "mailx" or something to generate a nicer email message or something.

Note I didn't test any of this, so there are probably quite a few bugs, or better ways to do it (e.g. inside a for loop perhaps). I'm sure other posters will point those out. Smilie But it should give you a good idea on how to do it.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

CRON Jobs

Hi, I am a total newbie to all things Unix. I've worked out I need to set up something that will allow me to automatically backup a DB for me, the DB is for a foum system I run. Now, I've only found out I need to use telnet for this, and worked out hwo to log into telnet today. From here... (4 Replies)
Discussion started by: eludlow
4 Replies

2. UNIX for Advanced & Expert Users

cron jobs

I need to monitor my cron jobs with another unix machine since occasionally the cron will go down on the main server but there are no errors. Can anyone help with a script to write to use the cron on the back up machine to monitor the main server? I am using SCO and the cron jobs have been... (3 Replies)
Discussion started by: rmarral
3 Replies

3. Solaris

Cron Jobs

I'm trying to run cron jobs to start any inhibited processes after a system reboot. I can schedule th cron, but i'm confused as to how to incorporated the reboot, since reboot is scheduled at different times, once every month. How can I write this to start every 15 min after after a reboot ... (2 Replies)
Discussion started by: Remi
2 Replies

4. Solaris

cron jobs

how to Put a cron entry which should be same script triggered on every Saturday and 1st of every month at 01.00 GMT. 0 2 1 * 6 --( At 2.00 GMT every sat & on 1st of every month) the above syntax is correct? Thanks (1 Reply)
Discussion started by: kurva
1 Replies

5. UNIX for Dummies Questions & Answers

cron jobs

Hi, We have a group of hosts using which the cron jobs are submitted... Few days ago i had submitted a cron job in of these hosts, but unfortunately forgot the host name :( Can anyone please help me out in finding this host name from which the cron s submitting the job, i dont want the... (2 Replies)
Discussion started by: bhavanisree
2 Replies

6. Linux

Cron jobs

Hi, I am a Linux administrator (newbie) in my company. The distro being used in the servers here is Centos 5.3 Just need to know, as a Linux administrator is it better for me to use /etc/crontab to set my cron jobs. I do not want to use the crontab -e to schedule my cron jobs. That means... (1 Reply)
Discussion started by: anaigini45
1 Replies

7. Shell Programming and Scripting

Help with cron jobs

Hi Frenz, How do we get a cron job running in background to foreground ? (3 Replies)
Discussion started by: mkalase
3 Replies

8. Shell Programming and Scripting

cron jobs

Hi, please help on this am trying to exec the below mentioned cron jobs but its getting failed fro the past two days ###but when am trying to execte the cron by the times 23,29 18 * * * /export/home/inrvgo/thelak/China.sh its getting exec properly please help on this #... (8 Replies)
Discussion started by: thelakbe
8 Replies

9. Solaris

Cron jobs and at jobs

There are two jobs in Solaris , Cron and at jobs.. I know how to disable or enable cron jobs. How can I enable at jobs and disable it. Kindly help. Rj (2 Replies)
Discussion started by: jegaraman
2 Replies

10. Red Hat

Cron jobs

I'm running cronjobs on a redhat 5.X. Cronjobs are getting failed frequently so how to find the root cause (2 Replies)
Discussion started by: karthik9358
2 Replies
cron(1M)						  System Administration Commands						  cron(1M)

NAME
cron - clock daemon SYNOPSIS
/usr/sbin/cron DESCRIPTION
cron starts a process that executes commands at specified dates and times. You can specify regularly scheduled commands to cron according to instructions found in crontab files in the directory /var/spool/cron/crontabs. Users can submit their own crontab file using the crontab(1) command. Commands which are to be executed only once can be submitted using the at(1) command. cron only examines crontab or at command files during its own process initialization phase and when the crontab or at command is run. This reduces the overhead of checking for new or changed files at regularly scheduled intervals. As cron never exits, it should be executed only once. This is done routinely by way of the svc:/system/cron:default service. The file /etc/cron.d/FIFO file is used as a lock file to prevent the execution of more than one instance of cron. cron captures the output of the job's stdout and stderr streams, and, if it is not empty, mails the output to the user. If the job does not produce output, no mail is sent to the user. An exception is if the job is an at(1) job and the -m option was specified when the job was submitted. cron and at jobs are not executed if your account is locked. Jobs and processses execute. The shadow(4) file defines which accounts are not locked and will have their jobs and processes executed. Setting cron Jobs Across Timezones The timezone of the cron daemon sets the system-wide timezone for cron entries. This, in turn, is by set by default system-wide using /etc/default/init. If some form of daylight savings or summer/winter time is in effect, then jobs scheduled during the switchover period could be executed once, twice, or not at all. Setting cron Defaults To keep a log of all actions taken by cron, you must specify CRONLOG=YES in the /etc/default/cron file. If you specify CRONLOG=NO, no log- ging is done. Keeping the log is a user configurable option since cron usually creates huge log files. You can specify the PATH for user cron jobs by using PATH= in /etc/default/cron. You can set the PATH for root cron jobs using SUPATH= in /etc/default/cron. Carefully consider the security implications of setting PATH and SUPATH. Example /etc/default/cron file: CRONLOG=YES PATH=/usr/bin:/usr/ucb: This example enables logging and sets the default PATH used by non-root jobs to /usr/bin:/usr/ucb:. Root jobs continue to use /usr/sbin:/usr/bin. The cron log file is periodically rotated by logadm(1M). FILES
/etc/cron.d Main cron directory /etc/cron.d/FIFO Lock file /etc/default/cron cron default settings file /var/cron/log cron history information /var/spool/cron Spool area /etc/cron.d/queuedefs Queue description file for at, batch, and cron /etc/logadm.conf Configuration file for logadm ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
svcs(1), at(1), crontab(1), sh(1), logadm(1M), svcadm(1M), queuedefs(4), shadow(4), attributes(5), smf(5) NOTES
The cron service is managed by the service management facility, smf(5), under the service identifier: svc:/system/cron:default Administrative actions on this service, such as enabling, disabling, or requesting restart, can be performed using svcadm(1M). The ser- vice's status can be queried using the svcs(1) command. DIAGNOSTICS
A history of all actions taken by cron is stored in /var/cron/log and possibly in /var/cron/olog. SunOS 5.10 5 Aug 2004 cron(1M)
All times are GMT -4. The time now is 05:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy