Exit script after exactly 3 hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit script after exactly 3 hours
# 1  
Old 11-25-2011
Exit script after exactly 3 hours

Need to run a script for exactly 3 hours.
It will have while loop which is to keep running until its 3 hours since the script's invocation time and then exit the script.
Script could be invoked at any time.
May have to change the number of hours later as per need.

Are there any existing date function in aix version3 that could help me add specific number of hours to the time when script is invoked and store it in variable? If not, can you suggest me some other approach please?

date -d option does not exist on my aix system.
value of $TZ is :
Code:
echo $TZ
NFT-1DFT,M3.5.0/02:00:00,M10.5.0/03:00:00

# 2  
Old 11-25-2011
The following code will print "Hello" for 3 hrs!

Code:
#! /bin/bash
start=`date +"%s"`
while [ `date +"%s"` -ne `expr $start + 10800` ]
do
    echo "Hello"
    sleep 1
done

# 3  
Old 11-25-2011
bash and ksh have built-in variable SECONDS
# 4  
Old 11-25-2011
AIX 3 is about 20 years old by now! If the above solutions do not work in your ancient environment you can try a diffent approach: Start a script in the background that sleeps for the desired time and then creates a flag file that causes the main script to stop.
Script sleeperagent.ksh:
Code:
#!/bin/ksh
# usage: sleeperagent.ksh <flagfile> <timeinseconds>
[ ! -f $1 ] && sleep $2   # flagfile should not exist when we start sleeping
touch $1

main script:
Code:
...
flagfile=/tmp/$$.flag
/path/to/sleeperagent.ksh $flagfile 10800 &  # 10800 seconds = 3 hours
while [ ! -f $flagfile ]; do
...
done
rm $flagfile
...

# 5  
Old 11-25-2011
One way... Try this...

killer_script
Code:
#!/bin/bash

test $# -ne 2 && echo "Usage : killer_script <pid> <time_secs>" && exit 0                      
pid_to_kill=$1
time_to_die=$2
sleep $time_to_die
kill -9 $pid_to_kill

master_script
Code:
#!/bin/bash

./killer_script $$ 10800 &

#do your stuff and the killer will kill this script after the specified time

--ahamed
# 6  
Old 11-25-2011
Assuming you have permissions to use "cron", I'd start the script with an "at" job to create a flag file in 3 hours time. Then test for the presence of the file in the main "while" loop of the script. This has the added advantage that you can stop the script whenever you want. There may by subtle syntax differences for AIX.

Code:
echo "touch /tmp/exitflagfile" | at now +3 hours

while whatever
do
     [ if -f /tmp/exitflagfile ]
     then
            rm /tmp/exitflagfile
            exit
     fi
     # Rest of script
done

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Perl one liner in bash script not replacing hours and minutes [HH:MM]

Hi I want to replace time stamp in the following line PROCNAME.Merge.exchMon.CODE.T_QSTART 08:45 read assuming the new time stamp is 09:45 ; the line is getting replaced as below :45 read I'm trying to use the perl one liner in bash script perl -pi... (4 Replies)
Discussion started by: charlie87
4 Replies

2. Shell Programming and Scripting

Help 'speeding' up this 'parsing' script - taking 24+ hours to run

Hi, I've written a ksh script that read a file and parse/filter/format each line. The script runs as expected but it runs for 24+ hours for a file that has 2million lines. And sometimes, the input file has 10million lines which means it can be running for more than 2 days and still not finish.... (9 Replies)
Discussion started by: newbie_01
9 Replies

3. UNIX for Dummies Questions & Answers

Shell script to check the file sitting in the directory more than 10 hours

Hi, I require shell script to check for any pending files which are sitting in the particular directory for more than 10 hours. Please help me on this...Thank you. (5 Replies)
Discussion started by: kiruthiish
5 Replies

4. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

5. Shell Programming and Scripting

Script that will show output starting from 24-hours earlier to present

Hi Guys, Good day! I hope you could help me on this, I have a file that conatins output upon executing cat /var/log/messages, then what I want is to get the logs that has been generated only starting from 24-hours earlier at the time of actual execution of the script. Is this possible? Best... (9 Replies)
Discussion started by: rymnd_12345
9 Replies

6. Shell Programming and Scripting

PHP Mail Script Takes Hours to Send emails

guys, i have a php script that i wrote that takes hours to send emails to recipients. i can't post the content of this script in here because the script contains some very important confidential information. so my question is, why is it that when the php script runs, it runs successfully, but... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. Shell Programming and Scripting

Write a script to send alert for some particular hours in a day

Hi All, I have a have a script which checks for some processes whether they are running or not and if they are not running then it send a mail specifying that the processes are not running. This particular script example abc.ksh is runs in a cron like this 0,5,10,15,20,25,30,35,40,45,50,55 * * *... (5 Replies)
Discussion started by: usha rao
5 Replies

8. Shell Programming and Scripting

how to list files between last 6 hours to 3 hours

Hi Frens, I want to list some files from a directory, which contains "DONE" in their name, i am receiving files every minute. In this i want to list all the files which are newer than 6 hours but older than 3 hours, of current time i dont want my list to contain the latest files which are ... (4 Replies)
Discussion started by: Prat007
4 Replies

9. Shell Programming and Scripting

script for add and subtract two hours

i find a script to calculate hours of job i(nclude extraordinary) i make a script to calculate add and subtract two hours (format hh:mm:ss) (7 Replies)
Discussion started by: ZINGARO
7 Replies
Login or Register to Ask a Question