user752590
user752590

Reputation: 101

Logs are not inserting in 'nohup.out' file

I am executing script1.sh through crontab.

script1.sh

echo "Crontab is executing this script."
echo "Some code is running here."
cd cron
./script2.sh

'script1.sh' is invoking 'script2.sh'.

'script2.sh'

echo "Exceuting script2."
echo "log of script3.sh is inserting in nohup.out file."
nohup sh script3.sh &
echo "Above syntax is not logging in nohup.out file. but this syntax is 'sh script3.sh > nohup.out &' is logging in nohup.out file. Why is it so?"

'script2.sh' is invoking 'script3.sh',but not able to log in nohup.out file. For logging following syntax is used.

nohup sh script3.sh &

'script3.sh' contains below mention line of codes.

echo "Executing script3. This is just test example to simulate the things."

Why logs are not inserting in nohup.out file?

Upvotes: 0

Views: 1797

Answers (3)

Walter A
Walter A

Reputation: 19982

From info nohup:

23.4 ‘nohup’: Run a command immune to hangups =============================================

‘nohup’ runs the given COMMAND with hangup signals ignored, so that the command can continue running in the background after you log out.
...
If standard output is a terminal, the command’s standard output is appended to the file ‘nohup.out’;
if that cannot be written to, [waltera: the write permission/space in current dir] it is appended to the file ‘$HOME/nohup.out’; and if that cannot be written to, the command is not run.
...
However, if standard output is closed, standard error terminal output is instead appended to the file ‘nohup.out’ or ‘$HOME/nohup.out’ as above.

When cron executes the script, stdout is not a terminal nor closed, so nohup has no reason to redirect any output.
Another demo of nohup without a terminal is when you start your script2.sh, that has an nohup command, with another nohup:

nohup ./script2.sh > masternohup.out

The output of script3.sh will be written to masternohup.out.

Upvotes: 4

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136238

You don't need nohup in modern days.

Shell escape method allows a process to leave its process group and never receive SIGHUP nor any other signals directed to a process group.

In bash shell:

(command &>log.txt &)

Upvotes: 1

Ivan
Ivan

Reputation: 7277

From man nohup

...
If standard input is a terminal, redirect it from an unreadable file.
If standard output is a terminal, append output to 'nohup.out' if possible, '$HOME/nohup.out' otherwise.
If standard error is a terminal, redirect it to standard output.
To save output to FILE, use 'nohup COMMAND > FILE'.
...

So it could be in $HOME/nohup.out either way it's best to use this to control output:

nohup COMMAND &> FILE

Upvotes: 1

Related Questions