1833623 Members
3613 Online
110062 Solutions
New Discussion

nohup problem

 
SOLVED
Go to solution
Gary L. Paveza, Jr.
Trusted Contributor

nohup problem

I'm trying to capture the PID of a process started via nohup. According to the man page for nohup, I should be able to do the following:

nohup (command1; command2)

However, when I try the following:

nohup (sleep 10; echo $!) &

I get the following error:

ksh: syntax error: '(' unexpected

Anyone know how I can get the PID for the first command?
13 REPLIES 13
Steven E. Protter
Exalted Contributor

Re: nohup problem

PID=$$

echo "Process ID: $PID:

Try command witout the parenthesis.

sEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Gary L. Paveza, Jr.
Trusted Contributor

Re: nohup problem

without the parens it only nohups the first command, then executes the second. The variable $! returns the PID of the nohup command. I need the PID of the command started by nohup.
Ken Penland_1
Trusted Contributor

Re: nohup problem

how bout this:

nohup `sleep 10; echo $!` &
'
Uday_S_Ankolekar
Honored Contributor

Re: nohup problem

Try without brackets

nohup sleep; echo $!) &


USA..
Good Luck..
Ken Penland_1
Trusted Contributor

Re: nohup problem

naw, I see what his problem is, the echo $! returns the pid of the nohup, and not the sleep...hrm....I cant figure it out either.
'
Uday_S_Ankolekar
Honored Contributor

Re: nohup problem

Ignore my previous post!

Try this instead:

nohup `(sleep 10; echo $!)` &

Good Luck..
Geoff Wild
Honored Contributor

Re: nohup problem

nohup `(sleep 10; echo $!)` &

That won't work - as ; seperates cmmands - serially - that is, nohup will do first command, after done, the second...


Only way I see is:

nohup command &
ps -ef |grep command |awk '{print $2}`

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Ken Penland_1
Trusted Contributor

Re: nohup problem

why do you need to nohup it?

just typing:

sleep 10 &

then echo $! returns the correct number... when ya start including nohup it starts getting messy cause THAT is considered the last job that ran in the background
'
Tom Danzig
Honored Contributor
Solution

Re: nohup problem

If all you want is the pid of the nohup'ed process, it is stored in $!

i.e.

# nohup sleep 60 &
[1] 11187
# Sending output to nohup.out

# echo $!
11187
Ken Penland_1
Trusted Contributor

Re: nohup problem

thats not what he wants Tom, that gives you the pid of the nohup, not the pid of the SLEEP
'
Geoff Wild
Honored Contributor

Re: nohup problem

actually, it does:

sha1:root:/ # nohup sleep 20 &
[1] 19692
sha1:root:/ # Sending output to nohup.out

sha1:root:/ # echo $!
19692
sha1:root:/ # ps -ef |grep 19692
root 19692 23610 0 13:30:13 pts/8 00:00 sleep 20


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Gary L. Paveza, Jr.
Trusted Contributor

Re: nohup problem

Geoff,

That's exactly what I need. Thanks.
Gary L. Paveza, Jr.
Trusted Contributor

Re: nohup problem

Tom provided the solution and Geoff confirmed it.