LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-26-2006, 12:35 AM   #1
mattengland
Member
 
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:

Code:
#!/bin/sh
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 http://www.tldp.org/LDP/abs/html/x6659.html#JOBIDTABLE does not appear to function inside the script.

Code:
mengland@matts-laptop 12:33am [/data/tmp] 25> cat ks2.sh
#!/bin/sh
dd if=/dev/zero of=/dev/null &
sleep 1
kill %1
mengland@matts-laptop 12:33am [/data/tmp] 26> ./ks2.sh 
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:

Code:
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.

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

Rep: Reputation: 162Reputation: 162
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):
Code:
#!/bin/sh -x
dd if=/dev/zero of=/dev/null &
sleep 2
jobs
kill %1
sleep 2
jobs
And here is the output:
Code:
bash-3.1$ ./killer.sh 
+ dd if=/dev/zero of=/dev/null
+ sleep 2
+ jobs
[1]+  Running                 dd if=/dev/zero of=/dev/null &
+ kill %1
+ sleep 2
./killer.sh: line 6: 14766 Terminated              dd if=/dev/zero of=/dev/null
+ jobs
bash-3.1$
 
  


Reply


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 04:42 PM.

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