- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Getting the processid of a process started using n...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2003 02:03 AM
05-20-2003 02:03 AM
Getting the processid of a process started using nohup.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2003 02:09 AM
05-20-2003 02:09 AM
Re: Getting the processid of a process started using nohup.
Yes, the pid of a background process can be obatined in the shell with '$!':
# nohup ./process 1>>process.out 2>&1 &
# MYPID=$!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2003 02:20 AM
05-20-2003 02:20 AM
Re: Getting the processid of a process started using nohup.
receive after fork into a file using fopen()/creat/write etc.
And even regular
ps -ef | grep
Zeev
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2003 02:53 AM
05-20-2003 02:53 AM
Re: Getting the processid of a process started using nohup.
$! 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-20-2003 03:14 AM
05-20-2003 03:14 AM
Re: Getting the processid of a process started using nohup.
echo $!
or
echo $?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2003 08:46 AM
05-21-2003 08:46 AM
Re: Getting the processid of a process started using nohup.
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.