Solved "nohup" not working when logout

Solved issue

banderas20

Active Member
Joined
Aug 1, 2018
Messages
136
Reaction score
50
Credits
1,080
Hi.

I have a very simple script


Code:
#!/bin/bash
echo "Wait 120s"
sleep 120s
echo "120s elapsed" > 120.txt

I run

Code:
nohup bash wait.sh &

then the process goes to background. If I exit my SSH session and login again, there are no jobs running, and only the nohup.out file is there.

What am I missing?

Thanks!

PS: running Debian GNU/Linux 12 (bookworm)
 


nohup bash wait.sh &
You don't need to execute bash to run a script, just call the script directly:
Bash:
nohup wait.sh &

Secondly nohup expects a command, I don't think that applies to scripts, but in any case bash is redundant.
 
Maybe it's redundant. Anyway, I don't know where to find the process once I logout and login back...
 
I don't know where to find the process once I logout and login back...
Not sure but here is some info worth reading:

You should be able to find it with top or htop I think to get PID and manipulate it.
 
Even though your script may not use them, bash opens IO channels with the terminal session in terms of STDIN, STDOUT and STDERR. While STDIN is not going to be a problem because nohup will automatically restrict it, STDOUT and STDERR need to be redirected to files.

Indeed, part of what could kill your script would be your "echo " sentence if you exited before it.

Try to redirect STDOUT and STDERR like this: nohup wait.sh>out.txt 2>err.txt or nohup wait.sh>combined.txt 2>&1

Both work for me.

More info on how it works here: https://www.linux.org/threads/can-i-outupt-a-sh-script-to-a-file.40364/post-160130

PS -- respect the spaces!
 
You don't need to execute bash to run a script, just call the script directly:
Bash:
nohup wait.sh &

Secondly nohup expects a command, I don't think that applies to scripts, but in any case bash is redundant.

Why would nohup behave any different if being a command or a script ?

All commands are files, no ?
 
Maybe it's redundant. Anyway, I don't know where to find the process once I logout and login back...

Why would you think calling bash is necessary ?

If anywhere you see a command like:
bash somescript.sh
... there should be some context as ... what are you trying to do ? Do you want to change shell ? How did you figure it was wrong in the first place ? What if you don't have bash ? Granted that is a low chance, but 100% certain there are Unix/Linux flavors that do NOT have bash.
 
Not sure but here is some info worth reading:

You should be able to find it with top or htop I think to get PID and manipulate it.

What is important to understand is that nohup and & are splitting off processes ...

... but, it's not because you split off a process, that it WILL be running it the background. It CAN be running in the background, but also not any more.

Do this :

nohup sleep 1 &

and see what happens. Try to find the sleep process "running in the background". Why do you think you can't find it ?
 


Members online


Top