I would like to know the purpose of nohup
these days.
I ask because I read this
Nohup is short for “No Hangups.” It’s not a command that you run by itself. Nohup is a supplemental command that tells the Linux system not to stop another command once it has started. That means it’ll keep running until it’s done, even if the user that started it logs out. The syntax for nohup is simple and looks something like this:
nohup sh your-script.sh &
Notice the “&” at the end of the command. That moves the command to the background, freeing up the terminal that you’re working in.
Nohup works with just about any command that you run in the terminal. It can be run with custom scripts as well as standard system commands and command line utilities.
With using linux distributions such as SLES 11.4, or RHEL/CentOS 7.x, I can
- remote log in to linux over the network via SSH
- run the simple program below, or any other, doing
./a.out &
- log out of linux; type exit in the putty SSH terminal
- my sample program below, or any other including bash, csh, or tcsh shell scripts, run to completion just fine without the need for
nohup
- typing exit in putty to close an SSH session, I would think that qualifies as a logout?
Is there any purpose or value with nouhup
today? Is it a kernel >= 3.x thing? For however long I can remember (kernel >= 2.6) I've never used nohup
and have always just use &
with no problems.
Can someone give me a practical scenario where nohup
would be used?
#include <stdlib.h>
#include <stdio.h>
/*
sample C code, to print numbers to a file named zz.tmp
the numbers 0 to 29 over 30 seconds
*/
int main ( int argc, char *argv[] )
{
FILE *fp;
int i;
i = 0;
fp = fopen("zz.tmp", "w" );
while ( i < 30 )
{
fprintf( fp, "%d\n", i++ );
sleep( 1 );
}
fclose( fp );
return 0;
}
mysleep.sh
#!/bin/bash
sleep 1000
On Centos 7.7, optiplex pc login with keyboard & mouse, my account is bash. I do nohup ./mysleep.sh
and then control-z. When typing jobs
I see [1]+ Stopped nohup ./mysleep.sh
A ps -ef | grep mysleep
shows that process id existing, but if I close the terminal window via X in upper right corner, that process goes away so it seems nohup
is not working?
while < 1000
andfprintf( stdout, "%d\n", i++ );
and it continues to run; albeit when I log back in it is not printing to the terminal but the program [process] is still running just fine. I experience this all the time running number crunching codes on linux at work- they write info to the terminal but the valued result ends up in a text file, we docrunchmynumbers.x > log.txt &
and we do not need to use nohup.shopt -s huponexit
or the job control features implemented by the OS. Usingnohup
is NOT only for the case wherehuponexit
is set in bash.