Sponsored Content
Full Discussion: kill the script
Top Forums Shell Programming and Scripting kill the script Post 302710657 by gary_w on Thursday 4th of October 2012 04:09:58 PM
Old 10-04-2012
This example should get you started. Script x1 just sleeps 30 seconds to simulate a long-running program. timeout_example.ksh starts x1, then waits for it. After 10 seconds kills itself, letting x1 continue running.
Tested on Solaris.

Code:
$ cat x1
#!/bin/ksh

sleep 30
$
$
$ cat timeout_example.ksh
#!/bin/ksh

## Define variables.
integer DEBUG=0      # Set to 1 to see debug messages.
integer TIMEOUT=10   # Commit suicide if child processes I start are still
                     #  running after this many seconds.  The processes
                     #  will continue to run however.
typeset -f countdown # Declare a function.

integer mypid=$$    # The PID for this process.
(( DEBUG )) && print "$0: mypid is: $mypid"

##  Define a function to show a timeout countdown.
countdown() {
  integer ctr=$TIMEOUT
  while (( $ctr > 0 ));do
    printf "\r$0: Seconds until timeout: %d  " $ctr
    ((ctr=$ctr-1))
    sleep 1
  done
}

##
##  MAIN
##
## Commit suicide if the INT signal is received.
## The child processes I was monitoring will continue to run.
##
trap 'print "\n$0: Timed out while waiting for child processes";kill $mypid >/dev/null 2>&1' INT

##
## Start the timeout process in the background and save its PID.  If
##  the timeout is reached, it will tell me to commit suicide by sending me
##  the INT signal.
##
(sleep $TIMEOUT;kill -s INT $mypid >/dev/null 2>&1) &
timeout_pid=$!
(( DEBUG )) && print "$0: timeout_pid is: $timeout_pid"

print "$0: Starting a long child process..."

##
## Start a child process in a subshell, in the background.  x1 just sleeps for
##  30 seconds to simulate a long-running program. Save its PID too.
##
x1 >/dev/null 2>&1 &
child_pid=$!
(( DEBUG )) && print "$0: Waiting for x1 $child_pid...\n"

##
##  Show the background process
##
ps -fu$(logname)|grep "[x]1"

##
##  Show a timeout countdown for the fun of it.
##
countdown &

##
##  Wait for the child process to complete.
##
wait $child_pid

##
##  If we are here, then the child processes finished before the timeout
##   period was reached.  Kill the timeout process.
##
(( DEBUG )) && print "\n$0: x1 finished, killing timeout process.\n"
kill $timeout_pid >/dev/null 2>&1

print "$0: Child processes completed"
$

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

kill script

Hi all! I wirte a little Shell Script, that kill pids by programm names. For example, when i want to kill any pid of xmms i use this command: kill -9 `ps -A | awk ' ($4=="xmms") {print $1}'` To put this in a "killprg" script i use the following linkes: #!/bin/bash echo "" echo "Programm... (2 Replies)
Discussion started by: donald1111
2 Replies

2. Shell Programming and Scripting

Script to kill process

Hello guys, I have a process named monitoreo, with 'monitoreo start' my process start until i kill them, now i want to do 'monitoreo stop' to kill them. After 'monitoreo start' i have this process running: ps -af UID PID PPID C STIME TTY TIME CMD ati 10958 1495 ... (5 Replies)
Discussion started by: Lestat
5 Replies

3. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

Hi All, I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages. How can i terminate this daemon ? ? ? Regards, Vijay Hegde (3 Replies)
Discussion started by: VijayHegde
3 Replies

4. Shell Programming and Scripting

Script to kill process...

hello Bros, I need to write some script that i can put it on crontab which checks for a process X if running. If the process X is ruuning then take the PID and kill it or display message that says process X is not running. I am using AIX 5.3 Thanks guys.:b: (2 Replies)
Discussion started by: malcomex999
2 Replies

5. Shell Programming and Scripting

Kill script discussion

Does anybody got an idea to write a script to kill a long list of processes automatic? I got try this: ps -ef | grep 'my_name' After that, I used the "kill -9 PID". It seems like quite inefficient if I got a long list of processes need to terminate or kill it :( Thanks a lot for any... (2 Replies)
Discussion started by: patrick87
2 Replies

6. UNIX for Dummies Questions & Answers

kill script using alias name

Hi, I am using the below command to kill the firefox process i have opened in Redhat 5. ps -ef|grep fire|grep -v grep|awk '{print $2}'|xargs kill -9 If i execute the above command in terminal it works good and kills session. but when i use alias for that it is not working. alias... (2 Replies)
Discussion started by: nokiak810
2 Replies

7. Shell Programming and Scripting

timed kill within script?

I want to warn everyone, I am not a programmer lol. I'm an IT wanting to get a little insight of programming, and I like to play around so I can learn. Ok, so I'm going to school for IT Security and Forensics. I had a project to write a hack, and I chose to write a shell script to run dd to write... (8 Replies)
Discussion started by: joshbgosh10592
8 Replies

8. Shell Programming and Scripting

auto kill script

Hi, I have created a shell script which is used by many users to change their password/unlock, etc., via menu. There is possibility users just close the putty window without proper exit from menu. I want a solution so that if anybody forgets to stop that session, it should kill automatically... (9 Replies)
Discussion started by: prashant2507198
9 Replies

9. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

Good afternoon I need to KILL a process in a single command sentence, for example: kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'` That sentence Kills the process ID corresponding to the program CAL255.4ge. However it is possible that the same program... (6 Replies)
Discussion started by: enriquegm82
6 Replies

10. UNIX for Dummies Questions & Answers

How to kill a script and all its subprocesses?

I'd like to terminate scripts. These scripts are not by me, but other persons. These contain other programs such as ping, nmap, arp, etc. If such a script is running, how can I terminate this script AND all processes called by this script? These scripts are big so terminating all programs... (4 Replies)
Discussion started by: lordofazeroth
4 Replies
hatimerun(1M)						  System Administration Commands					     hatimerun(1M)

NAME
hatimerun - run child program under a timeout SYNOPSIS
/usr/cluster/bin/hatimerun -t timeOutSecs [-av] [-e exitcode] prog args /usr/cluster/bin/hatimerun -t timeOutSecs [-v] [-e exitcode] [-k signalname] prog args DESCRIPTION
The hatimerun utility provides a convenient facility for timing out the execution of another child, program. It is useful when programming in scripting languages, such as the Bourne shell. See sh(1). The hatimerun utility runs the program prog with arguments args as a child subprocess under a timeout, and as its own process group. The timeout is specified in seconds, by the -t timeOutSecs option. If the timeout expires, then hatimerun kills the child subprocess's process group with a SIGKILL signal, and then exits with exit code 99. You can run this command in the global zone or in a non-global zone. The command affects only the global or non-global zone in which you issue the command. OPTIONS
The following options are supported: -a Changes the meaning of hatimerun radically: instead of killing the child when the timeout expires, the hatimerun utility sim- ply exits, with exit code 99, leaving the child to run asynchronously. It is illegal to supply both the -a option and the -k option. -e Changes the exit code for the timeout case to some other value than 99. -k Specifies what signal is used to kill the child process group. The possible signal names are the same as those recognized by the kill(1) command. In particular, the signal name should be one of the symbolic names defined in the <signal.h> descrip- tion. The signal name is recognized in a case-independent fashion, without the SIG prefix. It is also legal to supply a numeric argument to the -k option, in which case that signal number is used. It is illegal to supply both the -a option and the -k option. -t Specifies the timeout period, in seconds. -v Verbose output, on stderr. EXIT STATUS
If the timeout occurs, then hatimerun exits with exit code 99 (which can be overridden to some other value using the -e option). If the timeout does not occur but some other error is detected by the hatimerun utility (as opposed to the error being detected by the child program), then hatimerunhatimerun exits with exit code 98. Otherwise, hatimerun exits with the child's exit status. The hatimerun utility catches the signal SIGTERM. It responds to the signal by killing the child as if a timeout had occurred, and then exiting with exit code 98. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscu | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
kill(1), sh(1), attributes(5) Sun Cluster 3.2 10 Apr 2006 hatimerun(1M)
All times are GMT -4. The time now is 10:41 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy