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!

Hi, I've written a basic shell s

Status
Not open for further replies.

northcurlcurl

Programmer
Aug 9, 2002
6
AU
Hi,

I've written a basic shell script to stop a process (kill -TERM "$pid", where $pid is an variable that stores a processs id) on my Linux host. It seems like the script exits without an error and completes within a second. But when I issued ps -ef command after the script was completed, the process id I specified in the shell script was still there. I have even written another script to check if the process id I specified in the first script but it couldn’t catch the process. After while, I issued acctcom and the process was running for about 2 min 30 sec…I basically have two questions for this problem.

1. when a command is executed in a script, does a shell start executing next line of command without waiting for the result of the first command?

2. how come kill -TERM "$pid" was successfully executed but ps –ef command showed the process supposed to be terminated?

I guess my Unix knowledge is very limited and this question might be too basic to be asked in this forum. But I'm struggling with this problem for ages... Would you please help me out!!!

Thanks in advance,

Ryosuke
 
Ryosuke,

1. No, it shouldn't start until the previous command has completed (unless of course you have put the command in the background (with &) for some reason.

2. Rather than kill -TERM, I'd recommend kill -15 See if that works, and if not, nuke the process with an unforgiving kill -9

HTH.
 
1) Provided your line is not terminated by an ampersand (&), the shell execute them synchronously.
2) The kill command is successfully executed when it was able to send the desired signal to the designated process(es). It does'nt wait the termination of the interrupt catching handler of this process(es). When you send a TERM signal to a process and immediatly after a ps command, it's normal to see the killed process as it is in it's 'gracefull termination' context. However, the TERM signal is catchable and some processes ignore this signal.
The safe way to kill a process, say $pid is:
Code:
kill -TERM $pid
sleep 1
ps -p$pid >/dev/null && kill -KILL $pid
sleep 1
ps -p$pid && echo "Still alive !!!"

Hope This Help
PH.
 

Thanks so much for giving me some ideas. Since my shell script is incorporated into my colleague’s, I guess I have to have a look at his scripts in detail, in order to find out if he is running some commands in background…

For the mean time, I would like to ask one more question if I could. Would there be any difference between kill –TERM and kill –15? Coz, I couldn’t see any difference between two of them.

Cheers,

Ryosuke
 
You're absolutely right. Adapted from signal.h:
Code:
HUP    1  /* hangup */
INT    2  /* interrupt (rubout) */
QUIT   3  /* quit (ASCII FS) */
ILL    4  /* illegal instruction (not reset when caught) */
TRAP   5  /* trace trap (not reset when caught) */
ABRT   6  /* used by abort */
EMT    7  /* EMT instruction */
FPE    8  /* floating point exception */
KILL   9  /* kill (cannot be caught or ignored) */
BUS   10 /* bus error */
SEGV  11 /* segmentation violation */
SYS   12 /* bad argument to system call */
PIPE  13 /* write on a pipe with no one to read it */
ALRM  14 /* alarm clock */
TERM  15 /* software termination signal from kill */
USR1  16 /* user defined signal 1 */
USR2  17 /* user defined signal 2 */
CLD   18 /* death of a child */
PWR   19 /* power-fail restart */
WINCH 20 /* window change */
URG   21 /* urgent socket condition */
POLL  22 /* pollable event occurred */
STOP  23 /* sendable stop signal not from tty */
TSTP  24 /* stop signal from tty */
CONT  25 /* continue a stopped process */
TTIN  26 /* to readers pgrp upon background tty read */
TTOU  27 /* like TTIN for output if tp->t_local&TOSTOP */
VTALRM 28 /* virtual timer alarm */
PROF  29 /* profile alarm */
XCPU  30 /* CPU time limit exceeded */
XFSZ  31 /* File size limit exceeded */

Hope This Help
PH.
 
Replacing -TERM option with -15 was recommended by Ken, that's why I tried it. Thanks for your info, PH!

Ryosuke
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top