LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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
 
LinkBack Search this Thread
Old 06-14-2007, 02:51 PM   #1
pelikan81
LQ Newbie
 
Registered: Apr 2006
Distribution: Fedora
Posts: 4

Rep: Reputation: 0
Question how to kill processes connected with pipe


hi,
i want kill processes which is connected with pipe
for example i have running these processes:

a. process_1 | process_2
b. process_1 | process_3
c. process_1 | process_4
d. process_1 | process_5

i have a pid of process which i want kill:
PID3=`pidof -x process_3`
and
kill -9 $PID3 -> kill process_3 but process "process_1" from case b. is still running.
How can i kill them also?

I appreciate any ideas.
thx a lot

mario
 
Old 06-14-2007, 06:50 PM   #2
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,506

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
You will have to lookup the Parent Process ID (PPID) of the child process. In Linux, you can get this info by parsing the output of ps, or by finding it in /proc/$PID3/status. Sending a signal to the PPID should kill it.
--- rod.
 
Old 06-15-2007, 02:04 AM   #3
pelikan81
LQ Newbie
 
Registered: Apr 2006
Distribution: Fedora
Posts: 4

Original Poster
Rep: Reputation: 0
But process_1 and process_3 don't have relation parent and child, their PPID is 1.

They have connected with pipe for exmaple pipe:[1275140]:

cd /proc/<pid_process_1>/fd/ ; ls -la
lrwx------ 1 user users 64 2007-06-15 08:54 0 -> /dev/pts/4
l-wx------ 1 user users 64 2007-06-15 08:54 1 -> pipe:[1275140]
l-wx------ 1 user users 64 2007-06-15 08:54 2 -> pipe:[1275140]
lr-x------ 1 user users 64 2007-06-15 08:54 3 -> /var/log/test.log

cd /proc/<pid_process_3>/fd/ ; ls -la
lr-x------ 1 user users 64 2007-06-15 08:54 0 -> pipe:[1275140]
l-wx------ 1 user users 64 2007-06-15 08:54 1 -> /dev/null
l-wx------ 1 user users 64 2007-06-15 08:54 2 -> /dev/null
lrwx------ 1 user users 64 2007-06-15 08:54 3 -> socket:[1275143]
 
Old 06-15-2007, 03:50 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,434

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
Recently did something similar with trap:
#!/bin/bash
# /usr/bin/process_1
MY_PID=$$

trap_int() {
sleep .5
process_2 &> /dev/null

}

while [ -d /proc/$MY_PID ] ; do
trap trap_int 2
# note that leaving out this initial start of the program means that the loop waits for
# an INT signal before starting the program above.
process_2 &> /dev/null


This code is meant to keep re-starting process_2 whenever it gets stopped or interrupted (kill-2 process_2.).
In order to kill both processes you can send kill to process_1 first then to process_2. Play around with this code by sending kill 2, 5 and 9 to both process in both orders, then commenting out the last line and repeating. Some very interesting behaviour is available...

My program uses the above to create a non-polling wrapper process which keeps another program up-to-date by allowing other programs to interrupt it(process_2) process_1 keeps restarting it. process_1 acts very similar to a daemon which keeps process_2 running, but without constantly using CPU cycles(sleeps) to poll the process -kind of a one-way pipe. The third programs which 'communicate' with process_2 do so by rewriting its' config file. When process_1 restarts process_2 the config file gets re-read.
This setup will give you a stable process id for process_1, but process_2 will have a new pid each time it gets restarted. I use pgrep to help sort out the process id's.
I think what you want is slightly different, but playing around with this you can make it behave other ways. As mentioned, commenting out the last line causes process_2 to be started if you send kill -2 to process_1.
Note that the sleeps in my code are not needed, but make the response of the program smoother. These are not getting looped over and over.

Last edited by gnashley; 06-15-2007 at 04:02 AM.
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to kill processes tradnbillies Linux - Software 2 04-28-2006 03:00 PM
kill command in a pipe bujecas Linux - General 1 02-02-2006 07:55 AM
pipe to many processes? Thinking Programming 4 09-20-2005 11:22 AM
how to use kill to kill a batch of processes with same name? dr_zayus69 Linux - Software 2 09-03-2005 06:35 PM
pipe commands into processes acren Linux - Newbie 1 07-04-2001 10:08 AM


All times are GMT -5. The time now is 03:28 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
Open Source Consulting | Domain Registration