LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

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


I've got a command in a shell script:
Code:
php ./script.php > output.txt & echo $! > script.pid
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 script.pid.

I'd like to make another shell script which will read the pid from script.pid and kill that process. I tried this but it didn't work:
Code:
Mac:server sneakyimp$ sudo kill -9 < cat script.pid
-bash: cat: No such file or directory
Mac:server sneakyimp$ sudo kill -9 < `cat script.pid`
-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
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

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

Last edited by smoker; 06-27-2010 at 04:08 PM.
 
Old 06-27-2010, 10:02 AM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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.

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

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


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
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


All times are GMT -5. The time now is 10:53 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration