Run job for a period of time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Run job for a period of time
# 1  
Old 03-04-2006
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.
# 2  
Old 03-04-2006
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 10:02 PM..
# 3  
Old 03-05-2006
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?
# 4  
Old 03-05-2006
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
# 5  
Old 03-05-2006
That's cool, too. Thanks!
# 6  
Old 03-06-2006
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.
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 - 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

2. Shell Programming and Scripting

[Solved] Terminate TCPDUMP in a certain time period

Hello All, As I stated in subject, I need a command to terminate my tcpdump command in a certain time period. (using HP-UX) I am using below one to terminate when number of captured packages reach 3 limit. But what if there will no packet come in 5 min for instance? Please help me to find a... (2 Replies)
Discussion started by: mrcrowley
2 Replies

3. Shell Programming and Scripting

To get the Files between Time Period

All, How to get the list of files through a unix command which exists / created / updated between 8 PM to 11:59 PM from a particular location. Regards Oracle User (3 Replies)
Discussion started by: Oracle_User
3 Replies

4. Shell Programming and Scripting

Get connection count over a period of time

I used this script to get the connection to a domain in two specific minutes. I need to extend to give result over a range of minutes. The below gives total number of connections in the minutes 00:40 and 01:13 on 22nd March. egrep "22/Mar/2013:00:40|22/Mar/2013:01:13"... (1 Reply)
Discussion started by: anil510
1 Replies

5. HP-UX

memory consumption over a time period

Hi, Can some one please tell me how do I generate a report of the Memory Consumption over a time period: HP-UX B.11.31 U ia64 0440531406 unlimited-user license I normally use glance to monitor memory in run time. Note: I do not have root privileges. Thanks Danish ... (5 Replies)
Discussion started by: danish0909
5 Replies

6. UNIX for Dummies Questions & Answers

Start Time and period of a PID

Hi, Below is my OS details. uname -an SunOS mymachine 5.10 Generic_144488-07 sun4v sparc SUNW,SPARC-Enterprise-T5220 I need to know when was my Apache server last started. Whats is the best and most reliable way to find out not just for Apache but for any PID per say? I am... (16 Replies)
Discussion started by: mohtashims
16 Replies

7. Shell Programming and Scripting

grep /target greater than time period??

Hey guys, I'm fairly new at unix shell scripting and I have a quick question. Quick overview I devolped a script where I generate a file ..and I want to grep any time greater than 30 minutes. What i do is runa command to generates the below and puts it into a file: I run ./ggsci << endit... (4 Replies)
Discussion started by: nomiezvr4
4 Replies

8. Shell Programming and Scripting

Calculate Time Period in Scripting

Hi all, now i am writting one bash script. in that my requirement is i need to create one directory and that the directory details to be stored in one file Ex. date/time and all in one file. after that i need to delete the folder automatically exactly after 3months. between these time... (5 Replies)
Discussion started by: anishkumarv
5 Replies

9. Shell Programming and Scripting

To run a shell script for a definite period

Hi, I would like to invoke a shell script with period (in seconds) as a parameter and want it to run olny for that period. The script should come out after that period even some work is going inside the script. Regards (2 Replies)
Discussion started by: sanjay1979
2 Replies

10. 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
Login or Register to Ask a Question