Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-26-2006, 12:35 AM   #1
Registered: Nov 2004
Location: Chicago, IL USA
Posts: 42

Rep: Reputation: 15
bash script: run multiple jobs in bg, & kill them separately?

The following code kills off a backgrounded process in a bash shell after 4 seconds:

dd if=/dev/zero of=/dev/null &
sleep 4
kill $!
I also want to write additional scripts that background many processes at the same time, and selectively kill different backgrounded processes/jobs selectively (based on some arbitrary criteria that I make in my script).

How can I do this?

The 'kill %N' construct as per does not appear to function inside the script.

mengland@matts-laptop 12:33am [/data/tmp] 25> cat
dd if=/dev/zero of=/dev/null &
sleep 1
kill %1
mengland@matts-laptop 12:33am [/data/tmp] 26> ./ 
kill: illegal pid: %1
mengland@matts-laptop 12:33am [/data/tmp] 27> ps -ef | grep dd
mengland    3316       1   0  00:33:35 /usr/bin/dd
mengland@matts-laptop 12:33am [/data/tmp] 28> killall dd
mengland@matts-laptop 12:33am [/data/tmp] 29> !ps
ps -ef | grep dd
mengland@matts-laptop 12:33am [/data/tmp] 30>
Notice the:

kill: illegal pid: %1
Why does that not work? If it could, I could (seemingly) selectively kill processes I put in background via the bash script.

Any thoughts?

fyi, the same thing happens for a plain 'kill %' in the script.

Old 03-26-2006, 05:07 PM   #2
Senior Member
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,124

Rep: Reputation: 164Reputation: 164
Is /bin/sh linked to /bin/bash on your system? Your syntax is correct for bash - if you search man bash for job control, it describes the behaviour you're looking for. It runs properly on my system - here is the script (the -x is for debug output):
#!/bin/sh -x
dd if=/dev/zero of=/dev/null &
sleep 2
kill %1
sleep 2
And here is the output:
bash-3.1$ ./ 
+ dd if=/dev/zero of=/dev/null
+ sleep 2
+ jobs
[1]+  Running                 dd if=/dev/zero of=/dev/null &
+ kill %1
+ sleep 2
./ line 6: 14766 Terminated              dd if=/dev/zero of=/dev/null
+ jobs


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to run script in multiple directories... seabass55 Linux - General 5 07-06-2004 12:44 AM
how do i kill a process from inside a bash script? mikaelo Programming 4 05-28-2004 08:51 AM
small bash script to kill a PID flosch Linux - General 3 05-06-2004 09:28 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM
BASH script inform user and kill process mounters Programming 3 02-11-2002 04:57 PM

All times are GMT -5. The time now is 07:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration