Operating System - HP-UX
1753808 Members
7788 Online
108805 Solutions
New Discussion юеВ

nohup - capturing the return value from the command

 
SOLVED
Go to solution
Paul Ammirata
Occasional Contributor

nohup - capturing the return value from the command

Is there a way to execute a command in the background using nohup and capture the return value of the command. I've tried the following:

nohup (command; echo $? >rv.out) &

but I receive a syntax error:

sh: Syntax error: '(' is not expected
6 REPLIES 6
Sanjay_6
Honored Contributor

Re: nohup - capturing the return value from the command

Hi Paul,

Try $? after the nohup command. This will return the exit code for the command run using nohup,

nohup command;echo $?

do a "man nohup" for more info.

Hope this helps.

Regds
Paula J Frazer-Campbell
Honored Contributor

Re: nohup - capturing the return value from the command

Hi

From man nohup

nohup executes command with hangups and quits ignored. If output is
not redirected by the user, both standard output and standard error
are sent to nohup.out. If nohup.out is not writable in the current
directory, output is redirected to $HOME/nohup.out; otherwise, nohup
fails. If a file is created, the file's permission bits will be set to
S_IRUSR | S_IWUSR.

If output from nohup is redirected to a terminal, or is not redirected
at all, the output is sent to nohup.out.



So:-

nohup {filename] >log.file &


HTH

Paula

If you can spell SysAdmin then you is one - anon
steven Burgess_2
Honored Contributor
Solution

Re: nohup - capturing the return value from the command

Hi

You want to put your command in a script

#!/usr/bin/sh!

nohup

if [ $? = 0 ]
then
echo "echo command succesful" > /tmp/log
else
echo "command unsuccesful" > /tmp/errors
fi

Steve
take your time and think things through
John Palmer
Honored Contributor

Re: nohup - capturing the return value from the command

Hi Paul,

Some more information on what you are trying to achieve would be useful...

However, as per 'man nohup', nohup will not accept a list of commands. You have to write a shell script to run the commands and nohup the script.

Regards,
John
Paul Ammirata
Occasional Contributor

Re: nohup - capturing the return value from the command

The following:

nohup command;echo $?

works for commands executed in the foreground, but I want to execute the command in the background.
Paul Ammirata
Occasional Contributor

Re: nohup - capturing the return value from the command

While the man page states:

nohup will not accept a list of commands. You have to write a shell script to run the commands and nohup the script.

It also states:

Be careful to place punctuation properly. For example, in the command form:

nohup command1; command2

nohup applies only to command1. To correct the problem, use the command form:

nohup (command1; command2)

***

I wish to capture the return value from the command that runs nohup in the background.