[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):
And try to get the PID of that Command just ran by adding this to the end of that line above:
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:
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 ...')..??
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.
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:
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:
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...
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)
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)
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)
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)
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)
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)
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)
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)