[SOLVED] Using "$!" to get the PID of the Last Ran Background Process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] Using "$!" to get the PID of the Last Ran Background Process
# 1  
Old 09-19-2012
[SOLVED] Using "$!" to get the PID of the Last Ran Background Process

Hello All,

I was looking into creating a script that would be used only to start a Daemon and create a lock file...
F.Y.I. It's for Nagios' NRPE Daemon Plugin...

Anyway when I run the command to start the Daemon (below):
Code:
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

And try to get the PID of that Command just ran by adding this to the end of that line above:
Code:
...-c nrpe.cfg & echo $!

The PID it prints out is NOT the PID of the NRPE Daemon. Am I not using the "echo $!" correctly...?

On one of the boxes im trying this on, which is SLES 11.1 (Bash), the PID it prints out is one number
less then what the actual PID of the Daemon is.

For Example:
Code:
 # /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d & echo $!
[1] 27329
27329



# ps -ef | grep [/]usr/local/nagios/bin/nrpe
nagios   27330     1  0 15:20 ?        00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

I thought maybe it was just a coincidence, so I tried it a few more times, and still it does the same thing.
So I figured I would just implement this one and just subtract 1 from the PID it gives me to
get the real PID, but I then tried this same thing on an AIX server and the actual PID was completely random. So
I decided not to do that (probably wouldn't have been a good idea anyway)...


Does anyone know what I'm doing wrong, or if there is a better way to get this PID (without doing the 'ps -ef | grep ...')..??

Any thoughts would be great!


Thanks in Advance,
Matt
# 2  
Old 09-19-2012
The "&" will background the task. The ";" will let you type another command on the same line.
Maybe try:
Code:
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d & ; echo $!



Btw. The output you were seeing did not come from the "echo" (which never executed), it came from the "&".

Last edited by methyl; 09-19-2012 at 05:12 PM..
# 3  
Old 09-19-2012
Hey Methyl, thanks for the reply.

Tried that on both different OS'es and still not getting it for that command just ran...
Code:
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d ; echo $!
598184
# ps -ef | grep -v grep | grep nrpe
  nagios 585858      1   0 16:07:57      -  0:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

Thanks Again,
Matt
# 4  
Old 09-19-2012
Are you backgrounding the task with a "&" or not? The command just posted does not contain "&".
If you are not backgrounding the process, the value of "$!" is irrelevant in this context.
# 5  
Old 09-19-2012
Hey Sorry must have had a typo when I entered it...
Now it gives an error. I guess you can't use "&" and ";" one right after the other?

Code:
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d & ; echo $!
-bash: syntax error near unexpected token `;'

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d & ; echo $!
ksh: 0403-057 Syntax error: `newline or ;' is not expected.


Thanks Again,
Matt
# 6  
Old 09-19-2012
When posting, please remember to post what Operating System and version your are running and what Shell you are using.

Though there is nothing obvious wrong with the original command, try:
Code:
nohup /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d &
echo $!

The "nohup" is to answer the next post in advance.
# 7  
Old 09-20-2012
Hey Methyl, thanks again for the reply.

In my original question I posted both Operating Systems that I have been testing on (i.e. SLES 11.1 (Bash v3.2.51) -and- AIX 6 (KSH))...
I guess I should have made it a little more noticeable, sorry about that...

Also, on the AIX one I have installed Bash version 3.2.16(1), which is what I've been using to write my scripts.

Thanks for your suggestion I'll give it a try and post back...
Also, before that, I wanted to mention that I'm ssh'ed into the SLES server and opened up my script using:
Code:
# gedit myScript &
# echo $!
30398
# ps -ef | grep [g]edit
root     30398 30317  0 09:42 pts/0    00:00:00 gedit checkNRPE

As you can see it actually worked this time!
This leads me to believe that possibly another "background process" (or something like that) is begin started when
I issue the command to start the Daemon...?

Does this sound like a possibility..?


Thanks Again,
Matt

---------- Post updated at 11:38 AM ---------- Previous update was at 09:59 AM ----------

Hello Again,

I just tried your command (with the 'nohup') and still getting the same results.

Here is the exact output when issuing those commands:
Code:
# nohup /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d &
[1] 2927
nohup: ignoring input and appending output to `nohup.out`
# echo $!
2927
[1]+  Done                    nohup /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# ps -ef | grep 2927
root      2937  2431  0 11:31 pts/4    00:00:00 grep 2927

So I'm guessing there is something weird going on within that command when I execute it...

I'm thinking of just starting the Daemon from within a Bash script, then within that script
just find the PID after it's started and write it to a file...


Thanks Again for Your Help,
Matt
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Why the nohup-ed background process always is "stopped" ?

I let a script A call script B. I used nohup a.sh &>/tmp/log & In script A it calls B directly, without any redirecting or nohup or background. However A is always "Stopped", while B is running correctly. Anybody knows why? thanks! -----Post Update----- BTW, if I don't use nohup... (4 Replies)
Discussion started by: meili100
4 Replies

5. UNIX for Advanced & Expert Users

Why the nohup-ed background process always is "stopped" ?

I let a script A call script B. I used nohup a.sh &>/tmp/log & In script A it calls B directly, without any redirecting or nohup or background. However A is always "Stopped", while B is running correctly. Anybody knows why? thanks! -----Post Update----- BTW, if I don't use nohup... (1 Reply)
Discussion started by: meili100
1 Replies

6. Red Hat

"service" , "process" and " daemon" ?

Friends , Anybody plz tell me what is the basic difference between "service" , "process" and " daemon" ? Waiting for kind reply .. .. (1 Reply)
Discussion started by: shipon_97
1 Replies

7. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. UNIX for Dummies Questions & Answers

Process seen in "ps aux" but not "top"

Hi, I have a process that can be seen after "ps aux" command. However when I do "top" command. This process cannot be seen. How can this happen? Is there anything wrong with my code? (3 Replies)
Discussion started by: monkfan
3 Replies
Login or Register to Ask a Question