Operating System - HP-UX
1829447 Members
1600 Online
109992 Solutions
New Discussion

Getting the processid of a process started using nohup.

 
Shiju Abraham
New Member

Getting the processid of a process started using nohup.

Hi ,
I have a problem of getting the processid of a process started using nohup.

A sample code for process.c is attached below.
#include
#include
#include
#include

void sig_usr1()
{
printf("\nSignal SIGUSR1 received. Stopping");
exit(0);
}

int main(void)
{
pid_t pid;

/* Signal Trapping */
sigset(SIGTERM, sig_term);
sigset(SIGINT, sig_int);
sigset(SIGSTOP, sig_stop);
sigset(SIGUSR1, sig_usr1);

pid = fork();
if (0 > pid)
{
printf("\nFailed to Become a Deamon - Aborting. pid = %d\n", pid);
exit(1);
}

if (pid > 0)
{
exit(0);
}

setsid();

while(1)
{
};

return 0;
}

I am starting process in shell script using the following command.
nohup ./process 1>>process.out 2>&1 &
Is there any way to get the pid of process inside the shell script.

Regards,
Shiju.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Getting the processid of a process started using nohup.

Hi Shiju:

Yes, the pid of a background process can be obatined in the shell with '$!':

# nohup ./process 1>>process.out 2>&1 &
# MYPID=$!

Regards!

...JRF...

Zeev Schultz
Honored Contributor

Re: Getting the processid of a process started using nohup.

I think many ways.For example to save pid you
receive after fork into a file using fopen()/creat/write etc.
And even regular
ps -ef | grep should show child (PPID would appear).

Zeev
So computers don't think yet. At least not chess computers. - Seymour Cray
Shiju Abraham
New Member

Re: Getting the processid of a process started using nohup.

Hi James,
$! gives the pid of a process beginning in background, but since I am starting the process using nohup $! returns the nohup pid not the pid of process.

#nohup ./process 1>>process.out 2>&1 &
[1] 5320
#echo $!
5320
[1] + Done nohup ./process 1>>process.out 2>&1 &
#ps -ef|grep process
shiju 5347 766 3 16:18:53 ttyj6 0:00 grep process
shiju 5321 1 178 16:18:37 ? 0:03 ./process
#

here 5320 id the pid of nohup. pid of process is 5321.

Also the process is forked as shown in the code attached and the parent is exited so as to make it a daemon. so the ppid of process will be init process.
If an existing process is running then both the process instances will have ppid as 1(init).

Thanks and regards,
Shiju.
John Meissner
Esteemed Contributor

Re: Getting the processid of a process started using nohup.

you can use

echo $!
or
echo $?
All paths lead to destiny
Peter Nikitka
Honored Contributor

Re: Getting the processid of a process started using nohup.

Hi,
I use code fragments of this type in such situations:
Add something like this after your setsid();:
...
pidfno = open(pidfile, O_WRONLY|O_CREAT, 0775);
if (pidfno == -1)
{
perror(pidfile);
return(-1);
}

sprintf(pidbuf,"%d\n", getpid());
write(pidfno, pidbuf, strlen(pidbuf));
close(pidfno);
...

Set 'pidfile' to a value, you supply as an argument to your C-program, so the calling script has control over that or you a name extractable of your env -
/tmp/program.pid for example.

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"