LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to read a PID from a text file and kill the corresponding process (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-read-a-pid-from-a-text-file-and-kill-the-corresponding-process-816562/)

sneakyimp 06-26-2010 07:53 PM

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.

smoker 06-27-2010 01:44 AM

Code:

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

kill -9 $(cat script.pid)

jlinkels 06-27-2010 09:02 AM

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

sneakyimp 06-30-2010 01:52 PM

Excellent, guys. Thank you both.


All times are GMT -5. The time now is 05:11 AM.