LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   kill a running process (http://www.linuxquestions.org/questions/linux-server-73/kill-a-running-process-892299/)

carlosOFF 07-18-2011 07:22 AM

kill a running process
 
Hello everybody,

I trying to kill one process but only if the process is running, what I tried to do was that:

list=`ps x | grep crttool | grep -v grep | awk '{print $1,$3}'`

With that I can retrieve the pid and the status, but for every process in one string.

Who could I see if the status is running (R) and kill it.

There's a way to send the awk exit to one array? or a better way?

thank's
Carlos

MTK358 07-18-2011 09:21 AM

What does "exit to one array" mean?

Ayway, what's wrong with "killall"?

Code:

killall crttool
Also, it's better to use $(command) instead of backticks. It nests easily and can't be confused with single quotes.

carlosOFF 07-19-2011 07:33 AM

Quote:

Originally Posted by MTK358 (Post 4418127)
What does "exit to one array" mean?

Ayway, what's wrong with "killall"?

Code:

killall crttool
Also, it's better to use $(command) instead of backticks. It nests easily and can't be confused with single quotes.


I can't do that, because the problem is if the process is running, i could figure out the solution, by doing this:

toKill=`ps x | grep process | grep -v grep | awk '/R/ {print $1}'`

the problem is that process is running like a ghost the application is closed but still running.
It's a TCL application

Thanks!
Carlos

colucix 07-19-2011 08:00 AM

This lists the processes and kill them:
Code:

ps -C command -o pid,state --no-headers | awk '/S/{print "kill -9", $1 | "/bin/bash"}'

Reuti 07-19-2011 11:37 AM

Quote:

Originally Posted by colucix (Post 4418938)
This lists the processes and kill them:
Code:

ps -C command -o pid,state --no-headers | awk '/S/{print "kill -9", $1 | "/bin/bash"}'

I think it can be shortened by a system calll:
Code:

system("kill -9 " $1)
(note the space behing -9). Any reason why you chose /S/ and not /R/?


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