Intro To 'nohup' Command In Linux
2023-06-16 - By Robert Elder
I use the 'nohup' command to prevent programs from receiving the SIGHUP signal:
nohup my-command
The SIGHUP Signal
The SIGHUP signal is a message that the operating system can send to a process to indicate that the opposite side of a conversation has hung up, or ended the connection. The phrase 'hang up signal' comes from a time when most network communications took place over a telephone modem where the telephone could be physically 'hung up' to end the call. You can read more about the SIGHUP signal and other signals in the man pages:
man 7 signal
...
Standard signals
Linux supports the standard signals listed below. The second column of the table indicates which standard (if
any) specified the signal: "P1990" indicates that the signal is described in the original POSIX.1-1990 stan‐
dard; "P2001" indicates that the signal was added in SUSv2 and POSIX.1-2001.
Signal Standard Action Comment
────────────────────────────────────────────────────────────────────────
...
SIGFPE P1990 Core Floating-point exception
SIGHUP P1990 Term Hangup detected on controlling terminal
or death of controlling process
SIGILL P1990 Core Illegal Instruction
...
Example Use Of 'nohup' Command
Here, I have a simple C program that contains a busy loop that runs forever:
#include <stdio.h>
int main(){
int i;
while(1) { i++; }
return 0;
}
If I compile and run this program:
gcc main.c
./a.out
and then attempt to close the terminal, I can see that the process was automatically terminated:
ps -ef | grep a.out | grep -v grep
(no matching processes)
However, if I try running the program again with the 'nohup' command:
nohup ./a.out
and then close the terminal, I can see that my program continues to run:
ps -ef | grep a.out | grep -v grep
robert 29290 22470 99 15:34 pts/1 00:00:05 ./a.out
Caveats Of 'nohup' Command
Other factors, like custom signal handlers, can potentially negate the effect of the 'nohup' command. The following C program installs a custom signal handler for SIGHUP. On my machine, running the program below with the 'nohup' command won't prevent it from being terminated when the terminal is closed:
#include <signal.h>
#include <stdio.h>
static volatile int isRunning = 1;
void sighupHandler(int dummy) {
isRunning = 0;
}
int main(){
signal(SIGHUP, sighupHandler);
while(isRunning) { }
printf("SIGHUP caught, exiting...\n");
return 0;
}
Further Reading
Many of these details are part of a larger topic called job control, which you can read about in the man pages of 'bash':
man bash
...
JOB CONTROL
Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (re‐
sume) their execution at a later point. A user typically employs this facility via an interactive interface
supplied jointly by the operating system kernel's terminal driver and bash.
...
...
disown [-ar] [-h] [jobspec ... | pid ... ]
Without options, remove each jobspec from the table of active jobs. If jobspec is not present, and nei‐
ther the -a nor the -r option is supplied, the current job is used. If the -h option is given, each
jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell
receives a SIGHUP. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r
option without a jobspec argument restricts operation to running jobs. The return value is 0 unless a
jobspec does not specify a valid job.
...
And that's why the 'nohup' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|