Killing a shell script

Hi,

If I have a large shell script running as root, say for example like one that copies a ton of files, how would I kill the shell script and any processes that it created?

Thanks

Please post the script making it clear what Operating System and shell is in use.

In general your scripts need to trap a "kill -15" (hangup) and exit cleanly.
There is rarely any reason to issue a "kill -9".

It would help to know the Operating System so we can determine the best way to find the parent and child of the runaway process. This would determine the syntax of the "ps" comand with view to finding out the process tree and thence the process we need to kill.

Hi,

My script is just a simple shell script that uses cp to copy some files:

#!/bin/sh
cp -R /somefile /newlocation/
cp -R /anotherfile /newlocation
# And so on for about 600 files

The script does exit cleanly when the process is done, but I need to know how to kill it in the middle of its operation.

Thanks

I'm backing off because the OP cannot provide basic details about the Operating System and shell. This is fundamental to killing processes.

try pressing ctrl + c

I'm running Mac OS X, Darwin/BSD shell. I've written a native Mac OS X Cocoa application that launches a shell script. I need some sort of command that would be able to immediately kill the shell script and any other processes that it spawned.

Thanks

I'm not sure if it's available in OS X, but the "kill" utility - not the shell built-in command - has the ability to kill entire process groups:

/usr/bin/kill -15 -- -1234

That example would send the SIGTERM signal to all processes in the process group that process 1234 is in. The double-dash argument is necessary to indicate that the end of options has been reached and allow the PID argument to be interpreted as a negative PID instead of an erroneous numeric option.

Note that the shell built-in kill command in my experience does not have this capability, so you can't just type "kill ...." and get this effect - that will invoke the shell's built-in command.

The problem with using the kill utility in this manner is that a shell script is usually part of the same process group as the login shell that started it. So if you kill the shell script this way, you'll kill your login script, too.

Generally, the "setsid()" call is used in compiled programs to create a new process group. I'm not aware of any ways to do that in a shell script, but there may be. I'm certainly no expert on OS X particulars.

Hi,

I used the kill utility and it seems to work fine for my purposes :slight_smile:

Thanks