LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-01-2010, 06:51 PM   #1
halfpower
Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 241

Rep: Reputation: 31
Started programs with one script. Want to kill them with another


I started several programs with the following script:
Code:
front_end1 input1_a &
sleep 4
program2 input2_a &
program2 input2_b &
program2 input2_c &
program5 input5_a &

program6 &
I would like to be able to shut them all down as well. Is there a way to do this with a second script? I don't know how to do this because the second script would not know the PIDs. Is there a way I can do all of this with just one script?
 
Old 11-01-2010, 11:41 PM   #2
vamped
LQ Newbie
 
Registered: Oct 2006
Posts: 14

Rep: Reputation: 0
killall

You can kill programs by name as well as by pids. See man killall.
 
Old 11-01-2010, 11:47 PM   #3
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
You can also use the following:

kill `ps aux | grep PROGRAM | awk "${2}"`

Most likely, I jacked up the quotation marks because I am at work and am using a *gag* windows machine.....so just fix the awk command up and there you go!
 
Old 11-02-2010, 12:45 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Assuming that you're using bash, you're looking for the "jobs" builtin command. For instance:
Code:
#!/bin/bash

AWK="/usr/bin/awk"
DU="/usr/bin/du"

${DU} -hs /home/username/temp &
${DU} -hs /home/username/Documents &
${DU} -hs /home/username/Downloads &

job_pids=$( jobs -l | ${AWK} '{print $2}' )
for job_pid in ${job_pids} ; do
  echo "job_pid = ${job_pid}"
done

exit 0
The jobs builtin will display information about each process spawned by the environment the shell script was using. Use the '-l' option to have the PID listed for each job. For more info, type man bash and search for "SHELL BUILTIN COMMANDS" (e.g., in the man page, press the forward slash '/', type 'SHELL BUILTIN COMMANDS', and press Enter). You will need to continue scrolling down for a while, but you'll get there.

When using the '-l' option, the PID is displayed in the second column (hence the awk command).

You can then save the PIDs to a file. Your other script can read the PIDs from that file, kill the PIDs listed, and then delete the PID file.
 
  


Reply



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
Bash: how to get the PID of programs started? J_Szucs Programming 11 01-16-2012 02:08 AM
Problems with programs started in background. MheAd Linux - Desktop 6 05-04-2010 11:49 PM
How to kill remote processes started with SSH? gjb1002 Linux - General 15 08-26-2009 03:35 PM
order of auto-started programs yosato_uk Linux - Desktop 1 07-06-2008 09:59 PM
catch event or kill application when started.. timepassman Programming 2 05-29-2008 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration