The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grepping for period . in file name ? ie grep . bobk544 UNIX for Dummies Questions & Answers 5 11-30-2007 11:31 PM
output result (period -1) happyv Shell Programming and Scripting 4 05-18-2007 03:11 AM
record wc -l over 24 hour period csaunders Shell Programming and Scripting 5 12-09-2005 11:49 AM
Timeout period Nadeem Mistry UNIX for Dummies Questions & Answers 4 08-15-2001 06:55 AM
Deleting a File with an Erroneous Period PLF UNIX for Dummies Questions & Answers 3 08-08-2001 11:04 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-04-2006
Registered User
 

Join Date: Mar 2006
Posts: 41
Run job for a period of time

I have a job that runs for an unspecified amount of time. I want to run this as a cron job for a specified amount of time, say 2 hours. Once the time is up, the program should be killed in the middle of execution. How can I do this?

Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 03-04-2006
Registered User
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 130
If your process is written in C and you have access to the code, you can use the alarm(2) function which will generate a SIGALARM signal after a specified number of seconds. You can capture the signal for a graceful exit or let it abort the program.

You can do the same with a script by sending a HUP signal to your script via at(1). At the beginning of your script, run the following command:

echo "/bin/kill -HUP $$" | at now + 120 minutes

With a trap, the HUP signal could be intercepted for a graceful termination or, as in the alarm(2) example, let it abort the script. Also, the at-job number could be saved used to remove it from the queue if your script completes early.

A third option would be to create a pid-file from within your process (giving it a shell wrapper if necessary). Then you could cron another job to check for the existence of the pid file after a specified period of time and send a signal to it if the PID is still active.

Here's kind of a not-too-useful example:

Code:
#!/bin/ksh

    trap 'echo terminated counts: $I : $J : $K ; exit 3' HUP

    echo "kill -HUP $$" | at now + 1 minute

    I=0

    while [ $I -lt 1000 ]
    do
        J=0

        while [ $J -lt 1000 ]
        do
            K=0

            while [ $K -lt $J ]
            do
                (( K = K + 1 ))
            done

            (( J = J + 1 ))
        done

        (( I = I + 1 ))
    done

     echo final counts $I : $J : $K
    exit 0

Last edited by hegemaro; 03-04-2006 at 07:02 PM.
Reply With Quote
  #3  
Old 03-04-2006
Registered User
 

Join Date: Mar 2006
Posts: 41
Wow, that's just what I needed. I appreciate you going through with the effort with this.
I have some questions though:
- why do I get the following message each time when I run the above script in succession:
Job 2 will be executed using /bin/sh
Job 3 will be executed using /bin/sh
...

- in general, is there a preference of using SIGHUP over SIGINT in these situations?
Reply With Quote
  #4  
Old 03-05-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
this is one more way of implementing;

Code:
./yourscript.ksh & sleep 7200; kill $!
run your script as a background process; then sleep for specified number of seconds later send a signal to the process put in background using the last background identifier from ksh builtin variables
Reply With Quote
  #5  
Old 03-05-2006
Registered User
 

Join Date: Mar 2006
Posts: 41
That's cool, too. Thanks!
Reply With Quote
  #6  
Old 03-06-2006
Registered User
 

Join Date: Feb 2006
Location: Schenectady, NY
Posts: 130
By default, the at command uses the Bourne shell. What you're seeing with "Job 2 will be executed using /bin/sh" message is a warning. You can call at with a -k option to tell it to use the Korn shell.

As far as SIGHUP vs SIGINT, it depends on the process you're trying to interrupt. Many processes handle SIGHUPs gracefully -- for example syslogd will reread its configuration file. Other process, if not coded specifically for processing SIGHUP will terminate. SIGINTs, on the other hand, are almost universally "process-terminating". Experiment.

I'm glad I could be of help.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 10:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0