Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to get the process ID on Linux [duplicate]

I want to write a shell script (.sh file) to get a given process id. What I'm trying to do here is once I get the process ID, I want to kill that process. I'm running on Ubuntu (Linux).

I was able to do it with a command like

ps -aux|grep ruby kill -9 <pid> 

but I'm not sure how to do it through a shell script.

like image 247
sameera207 Avatar asked Jun 22 '11 09:06

sameera207


People also ask

How do I find the process ID of a script in Linux?

To get the PID of the last executed command type: echo "$!" Store the pid of the last command in a variable named foo: foo=$! Print it, run: echo "$foo"

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.

How do you find the PID of a specific process?

How to get PID using Task Manager. Press Ctrl+Shift+Esc on the keyboard. Go to the Processes tab. Right-click the header of the table and select PID in the context menu.

Is process ID unique in Linux?

In Linux, when an executable stored on disk is called a program, and a program loaded into memory and running is called a process. A process is given a unique number called process ID (PID) that identifies that process to the system, when it is started.


2 Answers

Using grep on the results of ps is a bad idea in a script, since some proportion of the time it will also match the grep process you've just invoked. The command pgrep avoids this problem, so if you need to know the process ID, that's a better option. (Note that, of course, there may be many processes matched.)

However, in your example, you could just use the similar command pkill to kill all matching processes:

pkill ruby 

Incidentally, you should be aware that using -9 is overkill (ho ho) in almost every case - there's some useful advice about that in the text of the "Useless Use of kill -9 form letter ":

No no no. Don't use kill -9.

It doesn't give the process a chance to cleanly:

  1. shut down socket connections
  2. clean up temp files
  3. inform its children that it is going away
  4. reset its terminal characteristics

and so on and so on and so on.

Generally, send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved!

Don't use kill -9. Don't bring out the combine harvester just to tidy up the flower pot.

like image 69
Mark Longair Avatar answered Oct 02 '22 20:10

Mark Longair


If you are going to use ps and grep then you should do it this way:

ps aux|grep r[u]by 

Those square brackets will cause grep to skip the line for the grep command itself. So to use this in a script do:

output=`ps aux|grep r\[u\]by` set -- $output pid=$2 kill $pid sleep 2 kill -9 $pid >/dev/null 2>&1 

The backticks allow you to capture the output of a comand in a shell variable. The set -- parses the ps output into words, and $2 is the second word on the line which happens to be the pid. Then you send a TERM signal, wait a couple of seconds for ruby to to shut itself down, then kill it mercilessly if it still exists, but throw away any output because most of the time kill -9 will complain that the process is already dead.

I know that I have used this without the backslashes before the square brackets but just now I checked it on Ubuntu 12 and it seems to require them. This probably has something to do with bash's many options and the default config on different Linux distros. Hopefully the [ and ] will work anywhere but I no longer have access to the servers where I know that it worked without backslash so I cannot be sure.

One comment suggests grep-v and that is what I used to do, but then when I learned of the [] variant, I decided it was better to spawn one fewer process in the pipeline.

like image 43
Michael Dillon Avatar answered Oct 02 '22 21:10

Michael Dillon