Terminate shell script after a specified time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Terminate shell script after a specified time
# 1  
Old 10-13-2014
Terminate shell script after a specified time

I have a shell script that checks a file state.txt, deletes fit.bin if state.txt is empty. I cron this at 2am, I will want the script to stop by 8am irrespective of the value of state.txt, any ideas?

Code:
#!/usr/bin/ksh


while true; do


if [[ -s ~/state.txt ]] ; then

echo

else

rm ~/fit.bin

exit

fi ;


sleep 961

done

exit

# 2  
Old 10-13-2014
Quote:
Originally Posted by aydj
I have a shell script that checks a file state.txt, deletes fit.bin if state.txt is empty. I cron this at 2am, I will want the script to stop by 8am irrespective of the value of state.txt, any ideas?
Maybe i oversee something, but i think you are doing it more complicated than necessary: put one script in cron at 2 am, put a second one which kills the first one (if it is running at all) in cron at 8 am. You can create a "PID file" at the start of the first script which you remove at program end:

Code:
#! /bin/ksh
typeset fPID="/var/run/${0##*/}"

trap "rm -f $fPID" 0

if [ ! -e "$fPID" ] ; then
     print - "$$" > "$fPID"
else
     print -u2 "Error: Program already running"
     exit 2
fi

[.... your script code here ....]

exit 0

This is your work script, which you will start at 2 am. It cannot be started twice (the second instance would terminate). Trap 0 is a "virtual trap" (for lack of a better word), which is not executed upon getting a certain signal but on exiting the shell process (^= terminating the script in any way).

The content of the PID file is only the PID of the running script instance.

Your termination script looks like this:

Code:
#! /bin/ksh

typeset fPID="/var/run/yourscript"

if [ -e "$fPID" ] ; then
     kill -15 $(cat "$fPID")
     sleep 5
     kill -9 $(cat "$fPID")         # in case the script doesn't end itself
     rm -f "$fPID"
else
     print -u2 "Warning: yourscript already terminated"
fi

[.....put any cleanup code here.....]

exit 0

Note that both scripts are only rough sketches. You might want to add all sorts of checks, ifs and whens.

I hope this helps.

bakunin
# 3  
Old 10-13-2014
Thanks bakunin. For example, if script terminates successfully at 2:30am, and the PID is reassigned to another process before 8am, won't that cause a process to be terminated wrongfully when the second script is run at 8am??
# 4  
Old 10-13-2014
You can cobble something up like...check the current time and break out of the infinite loop and exit if it's 8 am...
# 5  
Old 10-13-2014
Quote:
Originally Posted by aydj
Thanks bakunin. For example, if script terminates successfully at 2:30am, and the PID is reassigned to another process before 8am, won't that cause a process to be terminated wrongfully when the second script is run at 8am??
First: it is highly unlikely that the PID is recycled so fastly. PIDs are - for a reason - supposed to be almost random.

Second: even this small chance is further reduced to absolute zero because the terminating script will delete its PID file upon ending. "trap 0" is executed every time the script ends, regardless of how it ends. The action part of the cleanup script will only run when the PID file exists, not when the PID is in use. This means: when the script terminates at 2:30 it will remove the PID file, at 8:00 the cleanup script will start, fail to find a PID script (the "if [ - e file..." will evaluate to "false") and the cleanup script will do nothing (in fact it will issue the warning, see the else-part).

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 6  
Old 10-13-2014
Consider the following:

Code:
PID=$$
# The self killer
(sleep 30;kill $PID)&

# The program
while [ T ]; do
   date
   sleep 2
done

These 2 Users Gave Thanks to cjcox For This Post:
# 7  
Old 10-13-2014
Code:
#!/usr/bin/ksh
loop=24
#24x900 = 6 hours
while [ $loop -gt 0 ]; do
 if [[ -s ~/state.txt ]] ; then
  echo
 else
  rm ~/fit.bin
  exit
 fi
 sleep 900
 loop=$((loop - 1))
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture run time of python script executed inside shell script

I have bash shell script which is internally calling python script.I would like to know how long python is taking to execute.I am not allowed to do changes in python script.Please note i need to know execution time of python script which is getting executed inside shell .I need to store execution... (2 Replies)
Discussion started by: Adfire
2 Replies

2. Shell Programming and Scripting

Run script every minute and terminate after specified number of executions

when it runs and look at my acron.log file it generates an error as below /tmp/prog.sh: line 4: (12 Replies)
Discussion started by: azherkn3
12 Replies

3. 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

4. Shell Programming and Scripting

Terminate a process using shell script

Hi, I am writing a shell script to run a process and write the output of the process to a file. Inside program.sh: ./process.sh > tempfile .. open tempfile do the following But the problem is that process.sh is running indefinitely and program.sh is not executed completely. Can... (4 Replies)
Discussion started by: dreamgirl314
4 Replies

5. Shell Programming and Scripting

Shell script to convert epoch time to real time

Dear experts, I have an epoch time input file such as : - 1302451209564 1302483698948 1302485231072 1302490805383 1302519244700 1302492787481 1302505299145 1302506557022 1302532112140 1302501033105 1302511536485 1302512669550 I need the epoch time above to be converted into real... (4 Replies)
Discussion started by: aismann
4 Replies

6. Shell Programming and Scripting

strange behaviour script terminate before complete execution

I'm working on a script to make backup of various folder located on various host using different OS. I got a strange behaviour because the script donět process all lines of a configuration file, the script execute only one loop even the input file have 6 lines: This is the script: #!/bin/bash... (4 Replies)
Discussion started by: |UVI|
4 Replies

7. Shell Programming and Scripting

Time Diff in shell script

Hi all , i am trying to calculate time difference btw the script execution I am using solaris start_time=`date +%s` sleep 2 end_time=`date +%s` duration=`expr $end_time - $start_time` when i try to subtract i get the error line 13: %s - -time : syntax error: operand expected... (3 Replies)
Discussion started by: posner
3 Replies

8. Shell Programming and Scripting

Get the time with shell script

when I get time some times it like $hwclock -r Mon 07 Jul 2008 20:01:48 PM GMT -0.486264 seconds some times it like $hwclock -r Mon Jul 7 20:01:48 2008 -0.547393 seconds the sequence is not the same all the time and I just want to use "hwclock" not "date" to get time I... (8 Replies)
Discussion started by: yanglei_fage
8 Replies

9. Shell Programming and Scripting

how to terminate ssh connection without terminating script

Hi all, I connect with SSH connection to remote machine in the script and ı want to logout at half of the script then continue to script. If ı write exit in the script it terminates script not SSH connection. How can i do that please help me (1 Reply)
Discussion started by: fozay
1 Replies

10. Shell Programming and Scripting

Terminate session on completing script

Hai all.. How do i terminate my telnet session automatically when my java applicatiion exits. i have a file run which executes my java application and takes care of all class and library path settings prior to the execution. I would like to terminate my session when my application exits. The... (4 Replies)
Discussion started by: deepsteptom
4 Replies
Login or Register to Ask a Question