1849871 Members
3387 Online
104045 Solutions
New Discussion

Question abt nohup

 
SOLVED
Go to solution
Allanm
Super Advisor

Question abt nohup

Here is a snippet of the code -

start)
> if [ $running -eq 1 ] ; then
> > echo "JBoss already running (PID $pid)"
> > exit 1
> fi
> exec > /dev/null 2>&1
> rm -f $PID_FILE
> nohup $START_CMD $START_OPT &





The application is crashing quite frequently and I suggested that we capture the output of nohup in a file instead of sending it to /dev/null. But here folks are suggesting that nohup.out should be existing on the system which I couldnt locate after an exhaustive find. My argument is that its already going to /dev/null but they are saying that the wrapper scripts output is going to /dev/null and the commands output is going to nohup.out.

I have tried to look for nohup.out but couldnt find. Can u answer why?

Thanks,
Allan
3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Question abt nohup

>I have tried to look for nohup.out but couldn't find. Can you answer why?

Because it is already redirected to /dev/null with that exec. nohup(1) says:
If output is not redirected by the user, both standard output and standard error are sent to nohup.out.
nohup uses isatty(3) to check.
James R. Ferguson
Acclaimed Contributor
Solution

Re: Question abt nohup

Hi Allan:

> My argument is that its already going to /dev/null but they are saying that the wrapper scripts output is going to /dev/null and the commands output is going to nohup.out.

No, the STDOUT and STDERR file descriptors have already been redirected to /dev/null by the 'exec'. Everything written to STDOUT and/or STDERR from here on goes there unless you manipulate the file descriptors again.

Consider this script:

# cat ./mysh
#!/usr/bin/sh
START_CMD=date
START_OPT=-u
exec > /tmp/mylog1.$$ 2>&1
rm -f $PID_FILE
nohup $START_CMD $START_OPT &
1>&-
exec > /tmp/mylog2.$$
echo "ok from STDOUT"
print -u2 "ok from STDERR"

...run as:

# ./mysh

# cat /tmp/mylog1*
cat /tmp/mylog1*
ok from STDERR
Fri Apr 24 22:18:59 UTC 2009

# cat /tmp/mylog2*
ok from STDOUT

The 'sh-posix' manpages will show you that '1>&-' closes file descriptor-1 (STDOUT) which is why we can then redirect it again.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Question abt nohup

>JRF: '1>&-' closes file descriptor-1 (STDOUT) which is why we can then redirect it again.

Not "can" but probably "must", at least for stdin, stdout, stderr. When you redirect, it closes the file first.

So typically there isn't any need to do #>&-, unless you know you don't need it.