Shell Script to Get the Process Id on Linux

Getting PID of process in Shell Script

Just grep away grep itself!

process_id=`/bin/ps -fu $USER| grep "ABCD" | grep -v "grep" | awk '{print $2}'`

Shell script to get the process ID on Linux

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.

Shell script to capture Process ID and kill it if exist

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

Hope it helps.

Shell Script:How to get all the Process Id which are using a file-system/directory

If you only want id instead of id(user) then don't use the -u option. Documentation of fuser -u:

-u, --user

Append the user name of the process owner to each PID.

For me, fuser -c / has a different format than your sample. Each id is followed by letters denoting the type of access. The letters are printed to stderr, therefore I will use 2>&- to hide them.

$ fuser -c /
/: 1717rce 1754rce 1765rce 1785rce ...
$ fuser -c / 2>&-
1717 1754 1765 1785 ...

You can use grep to print one id per line:

$ fuser -c / 2>&- | grep -o '[0-9]*'
1717
1754
1765
1785
...

However, to run a loop you don't need one id per line. Ids separated by spaces work as well:

for id in $(fuser -c / 2>&-); do
echo "id = $id"
done

Get the process ID in a Shell script when a process is launched in foreground

As I commented above since your command is still running in foreground you cannot enter a new command in the same shell and goto the next line.

However while this command is running and you want to get the process id of this program from a different shell tab/window process then use pgrep like this:

pgrep -f "myprogram"
17113 # this # will be different for you :P

EDIT: Base on your comment or is it possible to launch the program in background and get the process ID and then wait the script till that process gets exited ?

Yes that can be done using wait pid command as follows:

myprogram &
mypid=$!
# do some other stuff and then
wait $mypid

Get the pid of the shell script and save it into a lockfile

The PID is stored in $$

Like

echo $$ > thisscriptpidfile


Related Topics



Leave a reply



Submit