LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2011, 11:10 PM   #1
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Rep: Reputation: Disabled
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.
 
Old 11-14-2011, 11:16 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
$! 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.

Last edited by suicidaleggroll; 11-14-2011 at 11:18 PM.
 
1 members found this post helpful.
Old 11-14-2011, 11:27 PM   #3
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thank you for your information
is it possible to start a process with a specific pid?
 
Old 11-14-2011, 11:36 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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)
 
Old 11-14-2011, 11:55 PM   #5
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by jhonnappier2007 View Post
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!

Last edited by devUnix; 11-14-2011 at 11:59 PM.
 
1 members found this post helpful.
Old 11-15-2011, 12:22 AM   #6
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-15-2011, 01:10 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
So something like:
Code:
ps -C tomcat -o pid=
 
Old 11-15-2011, 01:17 AM   #8
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
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 ?
 
Old 11-15-2011, 01:38 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
You would output the data to a pid file of your choosing and then call file back when pid needs to be checked / used.
 
Old 11-15-2011, 02:17 AM   #10
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
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?

Last edited by jhonnappier2007; 11-15-2011 at 02:31 AM.
 
Old 11-15-2011, 08:21 AM   #11
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by jhonnappier2007 View Post
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!

Last edited by devUnix; 11-15-2011 at 08:37 AM.
 
1 members found this post helpful.
Old 11-15-2011, 10:34 AM   #12
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.

Last edited by suicidaleggroll; 11-15-2011 at 10:36 AM.
 
2 members found this post helpful.
Old 11-20-2011, 10:13 PM   #13
jhonnappier2007
LQ Newbie
 
Registered: Nov 2011
Posts: 17

Original Poster
Rep: Reputation: Disabled
That is really helpful...
This is what i wanted exactly...
Thank you for the help...
Great explanation by devUnix...
 
Old 11-20-2011, 11:54 PM   #14
devUnix
Member
 
Registered: Oct 2010
Posts: 606

Rep: Reputation: 59
Quote:
Originally Posted by jhonnappier2007 View Post
That is really helpful...
This is what i wanted exactly...
Thank you for the help...
Great explanation by devUnix...
You're welcome!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get the creation time of a process via pid in C++ Cracker-Barrel Programming 4 03-23-2010 03:33 PM
pid of a process Marin_ Linux - Software 3 03-07-2010 10:22 PM
how to get the name of process from PID in C/C++? kulandaivelu Programming 3 02-03-2009 09:41 AM
How to get the PID of the process giving kill signal to a process? hariprd Programming 2 11-27-2008 03:10 AM
pid of a process kushkothari Programming 8 11-19-2008 05:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration