LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to save the pid of a process at the time of process startup (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-save-the-pid-of-a-process-at-the-time-of-process-startup-913576/)

jhonnappier2007 11-15-2011 12:10 AM

How to save the pid of a process at the time of process startup
 
Hi,

I am starting some processes from a script placed under /etc/init.d/. (Red Hat Enterprise Linux Server release 5.2 (Tikanga))
The process are meant to be run at system startup.
So a restart is scheduled in the crontab.
I need to save the pid of the process at the time of process startup itself.
Is that possible?

I Have tried this
Code:

ps -ef | grep java | grep -v grep | awk '{print $2}'
But this will be difficult in some cases, like if more than one process is running.

Any other ideas?
Any information would be helpful.
Thanks in advance.

suicidaleggroll 11-15-2011 12:16 AM

$! will give you the PID of the previously executed process, or you can use $$ from within the script itself to grab its own PID and dump it to a file (assuming it's a shell script you're launching). Many other programming languages have commands to grab their own PID as well.

jhonnappier2007 11-15-2011 12:27 AM

Thank you for your information
is it possible to start a process with a specific pid?

grail 11-15-2011 12:36 AM

If you have pidof installed it may help too assuming you want the pidof what is being launched as opposed to what is doing the launching (ie the script will be the launcher)

devUnix 11-15-2011 12:55 AM

Quote:

Originally Posted by jhonnappier2007 (Post 4524230)
Thank you for your information
is it possible to start a process with a specific pid?

That is the job of Mr. Kernel! ;)

The system itself assigns a PID to every job being executed. We cannot specify by any means as to what PID we want a job to have when it is executed.

Let's say:

Code:

ls myfile 1909
"1909" will be treated as an input to "ls" and not as a PID specifier.

Code:

echo $!
gives you the PID of the recent last/latest background job.

You can save it to a file, name it myJob.PID, for example.

Let me show you how it works:

Code:

$ vi hope &
[1] 15634
$ echo $!
15634

[1]+  Stopped                vim hope
$ kill 15634
$ jobs
[1]+  Stopped                vim hope
$ kill -9 15634
$ jobs
[1]+  Killed                  vim hope
$ jobs
$


Cheers!

jhonnappier2007 11-15-2011 01:22 AM

Thank you devUnix!!
That was a good explanation. Really helpful.


And for grail,

Actually the system is a remote server and couldn't install pidof on that.
I need the pid of the process that is started from the script.
Say, i am starting tomcat from startup.sh , Here i need the pid of the tomcat process not the startup.sh script

grail 11-15-2011 02:10 AM

So something like:
Code:

ps -C tomcat -o pid=

jhonnappier2007 11-15-2011 02:17 AM

Quote:

Originally Posted by grail (Post 4524291)
So something like:
Code:

ps -C tomcat -o pid=

Can you please explain it a little more?
does the inclusion of this statement in the startup script, save the process id to the variable pid ?

grail 11-15-2011 02:38 AM

You would output the data to a pid file of your choosing and then call file back when pid needs to be checked / used.

jhonnappier2007 11-15-2011 03:17 AM

Yes. But this gives the same output as the command i posted earlier.
Quote:

ps -ef | grep java | grep -v grep | awk '{print $2}'
The problem is if more than one process is running how can we get the pid of the process we just started.
for example

Code:

pgrep ssh
5263
8970
9222
9508

 ps -C sshd -o pid=
 5263
 8970
 9222
 9508

ps -ef | grep ssh | grep -v grep | awk '{print $2}'
5263
8970
9222
9508

this is the pids for SSH services running. The command pgrep, ps -ef and ps -C returns all the matches for the search criteria.
From this i am getting lot of pids. couldn't figure out which one is the required
That is why asked for saving the pid right after starting the process.


As suicidaleggroll posted earlier
Quote:

$! will give you the PID of the previously executed process, or you can use $$ from within the script itself to grab its own PID and dump it to a file (assuming it's a shell script you're launching). Many other programming languages have commands to grab their own PID as well.
Any ideas on how to implement this?

devUnix 11-15-2011 09:21 AM

Quote:

Originally Posted by jhonnappier2007 (Post 4524259)
Say, i am starting tomcat from startup.sh , Here i need the pid of the tomcat process not the startup.sh script

Welcome!

Here you go:

I have a job/script/process whatever you call it: myJob.sh
It does its work just as tomcat or httpd or sshd would do their work. Okay this far?

But let me tell you what it does:

[root@localhost ~]# cat /tmp/myJob.sh

Code:


#!/bin/bash
for count in `seq 1 3600`; do
        echo $count
        sleep 1
done
exit 0

Okay, let us run it in the background from the command prompt:

Code:

[root@localhost ~]# /bin/bash /tmp/myJob.sh &> /dev/null  &
[1] 3612

So, we already have one instance of the job/script we just executed above.

Now, let's create more PIDs of it by executing the same from within a script - that is what you really want to do. ;)

So, here is the script to run the above script in the background:

[root@localhost ~]# cat procTrack.sh

Code:

#!/bin/bash
/bin/bash /tmp/myJob.sh &> /dev/null  &
echo $! >> my_pids.log
echo "PID of myJob.sh is: $!  - Saved to my_pids.log"
exit 0

Now let's run the above script:

Code:

[root@localhost ~]# ./procTrack.sh
PID of myJob.sh is: 3641  - Saved to my_pids.log

Okay, so how many PIDs have we got by now?

Code:

[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root      3612  0.0  0.2  5152  1312 pts/1    S    09:12  0:00 /bin/bash /tmp/myJob.sh
root      3641  0.0  0.2  5152  1308 pts/1    S    09:12  0:00 /bin/bash /tmp/myJob.sh

Okay, I know you (I too) forgot what the previous PID was. So, let's not worry because we saved the second one in a file:

Code:

[root@localhost ~]# cat my_pids.log
3641

Can you see your loved one above? ;)

You can edit "procTrack.sh" and say something like this:

Code:

echo "Process myJob.sh Executed at `date` with the PID: $!" >> myPIDs.log
That way you will also have a record of process execution and associated PID. Later on you can simply say:

Code:

grep 'myJob.sh' myPIDs.log
and you will have a good track of your jobs/PIDs.

Here it is:

Code:

[root@localhost ~]# cat procTrack.sh
#!/bin/bash
/bin/bash /tmp/myJob.sh &> /dev/null  &
echo "MyJob.sh Executed at `date` with the PID of: $!" >> my_PIDs.log
exit 0
[root@localhost ~]#

Code:

[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root      5138  0.0  0.2  5152  1308 pts/1    S    09:32  0:00 /bin/bash /tmp/myJob.sh
[root@localhost ~]# ./procTrack.sh
[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root      5138  0.0  0.2  5152  1308 pts/1    S    09:32  0:00 /bin/bash /tmp/myJob.sh
root      5452  0.0  0.2  5152  1308 pts/1    S    09:33  0:00 /bin/bash /tmp/myJob.sh
[root@localhost ~]# ./procTrack.sh
[root@localhost ~]# ps aux | grep myJob.sh | grep -v grep
root      5138  0.0  0.2  5152  1308 pts/1    S    09:32  0:00 /bin/bash /tmp/myJob.sh
root      5452  0.0  0.2  5152  1308 pts/1    S    09:33  0:00 /bin/bash /tmp/myJob.sh
root      5470  0.0  0.2  5152  1308 pts/1    S    09:34  0:00 /bin/bash /tmp/myJob.sh
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat my_PIDs.log
MyJob.sh Executed at Tue Nov 15 09:32:48 EST 2011 with the PID of: 5138
MyJob.sh Executed at Tue Nov 15 09:33:58 EST 2011 with the PID of: 5452
MyJob.sh Executed at Tue Nov 15 09:34:03 EST 2011 with the PID of: 5470

I hope, I have done my best to help you.

Anyways, cheers!

suicidaleggroll 11-15-2011 11:34 AM

I'm pretty sure devUnix covered it above, but just to reiterate...

When you start a program in the background, you can immediately issue a call to $! to grab the PID of that process, which you can then do whatever you wish with. I routinely run large ionospheric weather models, and to keep track of them I do exactly that. Each run gets a "runner.sh" script, which looks something like:

Code:

[user@localhost]$ cat runner.sh
nohup mpirun -np 6 aspen96mpi.exe &> run.out &
echo $! > run.pid

On execution, the model is started up in the background using nohup, model output is written to "run.out", and the script writes the pid to the "run.pid" file. Later on, I can just cat run.pid to get the PID of that model, and then check on the status using or kill it if necessary.

jhonnappier2007 11-20-2011 11:13 PM

That is really helpful...
This is what i wanted exactly...
Thank you for the help...
Great explanation by devUnix...
:)

devUnix 11-21-2011 12:54 AM

Quote:

Originally Posted by jhonnappier2007 (Post 4529359)
That is really helpful...
This is what i wanted exactly...
Thank you for the help...
Great explanation by devUnix...
:)

You're welcome! :)


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