Visit Our UNIX and Linux User Community

Killing a shell script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing a shell script
# 1  
Old 08-26-2009
Killing a shell script


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?

# 2  
Old 08-26-2009
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.

Last edited by methyl; 08-26-2009 at 08:02 PM..
# 3  
Old 08-26-2009

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

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.

# 4  
Old 08-26-2009
I'm backing off because the OP cannot provide basic details about the Operating System and shell. This is fundamental to killing processes.
# 5  
Old 08-26-2009
try pressing ctrl + c
# 6  
Old 08-27-2009
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.

# 7  
Old 08-27-2009
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #267
Difficulty: Easy
In 1935, at the age of 22, Alan Turning was elected a fellow of King's on the strength of a dissertation in which he proved the central limit theorem.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing a program in shell script

I followed the directions here Free Twitter Source Code ? Twitter Database Server: Install and created a php script that enters twitter keyword searches into a MySQL DB. When you execute the files outlined in the above link, a script starts indefinitely. I've noticed that the scripts... (6 Replies)
Discussion started by: phpchick
6 Replies

2. UNIX for Dummies Questions & Answers

Killing the shell Process

I was just playing with the processes and suddenly a question striked my mind: What will happen if we kill directly the shell process?? :rolleyes: Do anyone know? Will the system shutdown? Or the system wont let it be killed? (5 Replies)
Discussion started by: paras.oriental
5 Replies

3. UNIX for Dummies Questions & Answers

Shell Scripts - Killing a job....

Hello all, I need to write a shell script that does the following; Allows you to kill a job,(1) listing only the jobs you own, (2) asks for which job to kill, (3) kills the job and (4) confirms kill... I am not sure if I need to first run the job command and pipe it with kill? Which options... (6 Replies)
Discussion started by: citizencro
6 Replies

4. Shell Programming and Scripting

**need help for killing a process through script**

Hello All, i hope you are fine. I need a little help from you people-- inside a script i want to kill a parent process by checking it with the child process.. p_pid=`ps -e | awk '/ra_cmd_d/ {print$1}'` here i am selecting the child process id in p_pid. next-- sleep_pid=`ps -af |... (3 Replies)
Discussion started by: onlyniladri
3 Replies

5. Shell Programming and Scripting

Newbie Question: Killing a process using a supplied name to a shell script

Hi, I am trying to automate the killing of named processes of which I found a good solution here on the forums but as I am pretty much a begginer to linux I am having an issue. The code I found is: kill $(ps -ef | nawk '/monitoreo start/ { print $2}'} but what I want to do is replace... (3 Replies)
Discussion started by: TylrRssl1
3 Replies

6. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

7. Shell Programming and Scripting

script not killing process!

i am using script to connect remotly to server and run some commands , one of these commands is to kill some process but tried different ways with no hope sshpass -p 'pass' ssh -o StrictHostKeyChecking=no server kill -9 `pgrep procs` getting error message "kill: bad argument count" ... (2 Replies)
Discussion started by: mogabr
2 Replies

8. AIX

killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog I use this for each volume group that we have. We run this everyday but the problem is, we... (4 Replies)
Discussion started by: jalge2
4 Replies

9. Shell Programming and Scripting

killing a child process within a shell

Hi All, I have a written a script in korn shell for importing data into a oracle database. The shell invokes the import within the script. I want to kill this import (child process) . I tries using trap, but this does not kill the import even if i press cnt c. i have to login into other terminal... (2 Replies)
Discussion started by: yerics
2 Replies

10. Shell Programming and Scripting

killing process using a script

can I do ps -ef | grep <process_name> and kill the process is it exists? and send a mail to me that the process was found and killed Thanks much... KS (4 Replies)
Discussion started by: skotapal
4 Replies

Featured Tech Videos