Sponsored Content
Top Forums Shell Programming and Scripting Why don't kill the script after 5s? Post 302926327 by derekludwig on Sunday 23rd of November 2014 11:12:23 AM
Old 11-23-2014
Shells (bash, ksh, etc) do not propagate signals to non-builtin commands. To illustrate this a bit better, I replaced the sleep in your script with a script tasker
Code:
#! /usr/bin/perl

$\ = ' ';
$| = 1;
$s = 10;

sub handler {printf "\ncaught SIG\%s\n", shift @_; $s = 0; }

$SIG{INT}  = \&handler;
$SIG{TERM} = \&handler;

while ($s--) { print $s; sleep(1); }
printf "\nexit\n";

Nothing fancy, it counts down from 9 with one second delays. So your script is now (with some echos):
Code:
# watchdog process 
mainpid=$$ 
echo mainpid $$
trap : TERM
( sleep 5; echo killing $mainpid; kill $mainpid ) & 
watchdogpid=$! 
echo watcher $!
tasker
echo killing $watchdogpid
kill $watchdogpid

And when run:
Code:
$ yangleiold
mainpid 22277
watcher 22278
9 8 7 6 5 4 killing 22277
3 2 1 0 exit
killing 22278
yangleiold: line 10: kill: (22278) - No such process

What you need to do is have the watchdog kill the subprocess directly:
Code:
# watchdog process 

tasker &
pid=$!

sleep ${1:-5}
echo Killing $pid
kill $pid

Which runs as:
Code:
$ yangleinew
9 8 7 6 5 Killing 32765
caught SIGTERM
exit

Now if the task finishes first, the script will still sleep for the full interval:
Code:
$ yangleinew 20
9 8 7 6 5 4 3 2 1 0 exit
Killing 22361
x: line 8: kill: (22361) - No such process

Hope this helps.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

I don't think this would be a problem to script but ...

:confused: I have not written any code in about 15 years. The company I work for has Unix servers and they utilize KSH. The scriptors say that what I want can only be scripted in PERL which on my server they say they cannot get to work. They also tell me that what I want done cannot be... (0 Replies)
Discussion started by: CapnJuan
0 Replies

2. Programming

kill(0,-9) don't kill the process

Hi all i have simple c program , when i wish to kill the app im using kill(0,-9) , but it seams this command don't do any thing and the program. just ignore it . what im doing wrong here ? im using HP-UX ia64 Thanks (9 Replies)
Discussion started by: umen
9 Replies

3. Shell Programming and Scripting

don't know how to implentment as unix sh script

for each file if file name like xx* for each line in a file if substring(3,6) found in another txt file output to file-a( filename = orginal file + _a) else output to file-b( filename = orginal file + _a) end Next Line (4 Replies)
Discussion started by: ttivanwan@yahoo
4 Replies

4. Shell Programming and Scripting

script don't stop

Hello everybody! I am new to this and I am trying to change a script in an open source program that plots some offset vectors and then calls a postscript viewer. I have commented away the call for the postscript viewer but somehow the script doesn't return to the shell prompt. I cant figure out... (3 Replies)
Discussion started by: larne
3 Replies

5. UNIX for Advanced & Expert Users

don't understand the unix script

if {"$my_ext_type" = MAIN]; then cd $v_sc_dir Filex.SH $v_so_dir\/$v_fr_file Can somebody tell me what does this suggest. I am pretty new to unix and I am getting confused. What i understood from here is If we have a file extension name as MAIN which we have then we change the directory to... (1 Reply)
Discussion started by: pochaman
1 Replies

6. Solaris

I don`t understand how It work (about startup script)?

Hi all. The startup script in /usr/local/bin. After user login the script run an application. Iwould in the same way run the another application. How to make It similar? Where I must to look? Regards. (3 Replies)
Discussion started by: wolfgang
3 Replies

7. UNIX for Dummies Questions & Answers

HELP Script don't work selecting lowest value!!!

Hy again guys, Last week i resolve a question here but now i need your help again :rolleyes: I have about 3000 files that i need to choose based on the lowest value, so i make temp files like this: The files can have lines from 1-10 but only 2 columns, the point is to grep the name os the... (2 Replies)
Discussion started by: MetaBolic0
2 Replies

8. UNIX for Dummies Questions & Answers

Revision of script from don

Don, I revised script but when I ran it I did not receive any log. I am not sure what you mean to run it in code tags. I am using a putty session and ssh but I did not get a trace log? Barb ---------- Post updated at 01:33 PM ---------- Previous update was at 01:27 PM... (2 Replies)
Discussion started by: bcarosi
2 Replies

9. Shell Programming and Scripting

A shell script to run a script which don't get terminated and send a pattern from the output by mail

Hi Guys, I am very new to shell script and I need your help here to write a script. Actually, I have a script abc.sh which don't get terminated itself. So I need to design a script to run this script, save the output to a file, search for a given string in the output and if it exists send those... (11 Replies)
Discussion started by: Sambit Sahu
11 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 11:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy