Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 06-26-2010, 08:53 PM   #1
Senior Member
Registered: Dec 2004
Posts: 1,055

Rep: Reputation: 78
how to read a PID from a text file and kill the corresponding process

I've got a command in a shell script:
php ./script.php > output.txt & echo $! >
This results in my script launching as a background process with output routed to output.txt and the process id of this script being written to

I'd like to make another shell script which will read the pid from and kill that process. I tried this but it didn't work:
Mac:server sneakyimp$ sudo kill -9 < cat
-bash: cat: No such file or directory
Mac:server sneakyimp$ sudo kill -9 < `cat`
-bash: 85412: No such file or directory
I read the man pages for kill and didn't see anything about reading the pid from stdin.
Old 06-27-2010, 02:44 AM   #2
Senior Member
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
pid=$(cat;kill -9 $pid
Actually, that can be condensed somewhat :
kill -9 $(cat

Last edited by smoker; 06-27-2010 at 04:08 PM.
Old 06-27-2010, 10:02 AM   #3
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,187

Rep: Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037Reputation: 1037
It is a bad idea to use kill -9 to terminate a process in a regular fashion. When a process receives a kill signal (SIGKILL) it cannot do any cleanup actions like freeing rescources. Running this on a server where many of these processes will be initiated and terminated and the server will not be reset for a long time, this will lead to consuming all available memory.

Only in situations where you want to terminate a process no matter what you may use kill -9. All other applications should use kill -15, SIGTERM which terminates a process in a clean and orderly way.

Old 06-30-2010, 02:52 PM   #4
Senior Member
Registered: Dec 2004
Posts: 1,055

Original Poster
Rep: Reputation: 78
Excellent, guys. Thank you both.


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
kill < file (Feeding kill from file), PID, <, awk, grep jaffd Programming 4 04-09-2010 05:06 PM
Kill child process problem. No matter what i try cant stop pid becoming a zombie?? ajb181 Linux - Software 1 11-15-2009 06:36 PM
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 04:10 AM
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 01:38 PM
Looking for a way to find a PID and kill the process it belongs to johngreenwood Programming 6 01-14-2007 06:45 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:50 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