[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
# 8  
Old 09-20-2012
I don't see anything wrong there. You see that [1]+ Done ? The command is completing and returning before you run ps.

It's probably forking a copy of itself so it can put itself in the background automatically and return instead of hanging your shell. Daemons do that so often they call it 'daemonizing'. You might need another way to find its PID.

Look in /var/run/ for a file which might belong to it. Daemon processes often store their PID in a file somewhere under there for reference.

p.s. Here's a trick to stop grep from matching itself in a ps listing. ps | grep '[1]234'
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 09-20-2012
I would agree with Corona688; the behaviour you describe fits to a process "forking itself into background". This means, you don't need the "&" in your command; run it, see it finish, and still see the daemon (with a PID higher by one on a calm system or "random" on a busy system) show up in ps.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 09-20-2012
When you run a pipeline in the background with &, the value assigned to $! is the pid returned by the fork() system call that the shell used to create the pipeline's last command's process (nrpe in this simple, single-command case).

If that last command itself forks (as nrpe does during daemonization) the value of $! will be the pid of the parent resulting from this second fork.

From nrpe.c @ sourceforge
Code:
349 	/* else daemonize and start listening for requests... */
350 	else if(fork()==0){

After that fork(), the parent has nothing more to do and exits, while the child goes on to do the work of nrpe. This exiting parent process is the child process whose pid was saved in $!.

In the code there's a write_pid_file() function (line 1594) which refers to a "pid_file" config variable (line 581). You may want to have a look to see if you've set it to something. If not, do so and you can get the pid there.

I have never used nrpe. My pid config file suggestion is based solely on the code and not on experience.

Regards,
Alister

---------- Post updated at 04:49 PM ---------- Previous update was at 04:07 PM ----------

Quote:
Originally Posted by RudiC
... see the daemon (with a PID higher by one on a calm system or "random" on a busy system) show up in ps.
Some fork() implementations do not generate sequential pid values. I believe AIX is one of those. I am certain OpenBSD is.

Regards,
Alister

Last edited by alister; 09-20-2012 at 05:22 PM..
This User Gave Thanks to alister For This Post:
# 11  
Old 09-21-2012
Hey Corona, RudiC and alister, thanks for the replies!

Hey guys, great explanations!
Thanks alot for clarifying all this.... It makes much more sense to me now. I was kinda under the assumption that something
else was getting called from within the script (i.e. forking).

Alister,
Did you have to decompile nrpe in order to look at the code? I think I remember trying to look at it before and
it was definitely looking like compiled code (i.e. probably C, I think??)..?


Well anyway, thanks again guys for ALL your help with this..!!!


Thanks Again,
Matt

---------- Post updated at 10:35 AM ---------- Previous update was at 09:44 AM ----------

Ugghhh, Hey Corona, you were Correct!!!

I should have looked in here previously but guess what I found in "/var/run/..."?
Yup that's right lol, there is a PID file already in there for NRPE....
i.e. "/var/run/nrpe.pid"

Well, sorry for having to ask the question in the first place...
But I really appreciate ALL your guy's comments and help!


Thanks Again,
Matt
# 12  
Old 09-21-2012
It certainly wasn't a dumb question. Glad we could help.
# 13  
Old 09-21-2012
Yea... Thanks.

This is kinda how I feel right now --> Smilie
Haha....



Thanks Again,
Matt
# 14  
Old 09-21-2012
Quote:
Originally Posted by mrm5102
Alister,
Did you have to decompile nrpe in order to look at the code?
No. I didn't even download nrpe. The code is browsable on the web at sourceforge. I linked it in my previous post.

Regards,
Alister
This User Gave Thanks to alister For This Post:
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