Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script is not terminating as expected 1

Status
Not open for further replies.

lnbnatera

Technical User
Feb 14, 2006
54
HK
hi,

my aim is to run a script in the background, and in the event that i need to cancel its execution, i'll just put a file name cancel to the execution directory of the script. if the cancel file exists, the whole script is expected to end and seize execution.

the problem is i cannot achieve that kind of effect...

#!/bin/sh

check_cancel() {
# this routine will check whether the file cancel exists
# if it exists, "this script" is expected to end execution
if [ -f `dirname ${0}`"/cancel" ]
then
rm `dirname ${0}`"/cancel"
exit
fi
}

while true
do
# check if a cancel file exists in the
# directory where the script is executed
check_cancel
while read LINE
do
check_cancel
sleep 1
echo ${LINE} > /dev/null
done < /var/adm/messages
done

i just used /var/adm/messages and echo to /dev/null to simulate some kind of processing.

thanks,
 
Hi

Works for me.

I would add some [tt]echo[/tt]s to tell which part is executing when it does not exits.

If this is a debug version and the original is not secret, please post that too.

Feherke.
 
try running it with

sh -x scriptname and post the output

Mike

"Whenever I dwell for any length of time on my own shortcomings, they gradually begin to seem mild, harmless, rather engaging little things, not at all like the staring defects in other people's characters."
 
The problem is that in 'proper' Korn shells (i.e. not pdksh) exit in a function will behave the same way as return. So ideally you should return a result from the function and in the calling code use something like check_cancel || exit (or && depending whether you decide to return 0 or 1 when the cancel file exists).

Annihilannic.
 
You may try to replace this:
exit
with this:
exec kill -9 $$ 2>/dev/null

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
ok, i tried the script in korn shell. it worked as expected.

however, is there a way for it to work in bourne shell (#!/bin/sh)?
 
Have you tried my suggestion ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Why -9 PHV?
To prevent ANY trap !

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top