Explaining nohup in Bash

The aim of this pageđź“ť is to explain the usage of nohup in Bash based on the particular example of starting a music player process in the background. My player is VLC and I am playing rain when working

Pavol Kutaj
3 min readFeb 28, 2024
function rain {
nohup vlc --intf dummy --play-and-exit "/Users/admin/Music/rain30.mp3" >/dev/null 2>&1 &
}
  • The function above resides in .bash_profilecontains a one-liner starting with a nohup command
  • nohup is used to run a command immune to hangups in Bash.
  • hangup is a type of signal in UNIX systems — it is the first signal on the long signal list
  • the list of signals for BSD/MAC OS is at: https://man.freebsd.org/cgi/man.cgi?sektion=3&query=signal

signal

  • signal is a communication mechanism which is passing information to a running process
  • when a process receives a signal it …either terminates or does something else (changes the flow of execution, etc.) → signals are software interrupts.
  • if on Mac, run man signal in the terminal, see some examples
Name          Default Action       Description
1 SIGHUP terminate process terminal line hangup
2 SIGINT terminate process interrupt program
3 SIGQUIT create core image quit program
4 SIGILL create core image illegal instruction
5 SIGTRAP create core image trace trap
6 SIGABRT create core image abort program (formerly SIGIOT)
7 SIGEMT create core image emulate instruction executed
8 SIGFPE create core image floating-point exception
9 SIGKILL terminate process kill program
...
17 SIGSTOP stop process stop (cannot be caught or ignored)
18 SIGTSTP stop process stop signal generated from keyboard

background vs dialog

  • Look at the function again and note that — crucially — it ends with &
function rain {
nohup vlc --intf dummy --play-and-exit "/Users/admin/Music/rain30.mp3" >/dev/null 2>&1 &
}
  • Appending & at the end of a command runs it as a background process, that is it does not block your interaction with the terminal (that would be a dialog process)
  • Using nohup with & allows a process to continue running in the background even after the terminal is closed.

To background an already running program you can do Ctrl-Z to suspend it + followed by bg. Note that backgrounded processes are still children processes of your terminal and will die if you close the terminal (this will send yet another signal, SIGHUP). To prevent that from happening you can run the program with nohup (a wrapper to ignore SIGHUP), or use disown if the process has already been started. Alternatively, you can use a terminal multiplexer as we will see in the next section

— https://missing.csail.mit.edu/2020/command-line/

signal  | key_shortcut
--------|---------
SIGINT | CTRL + C
SIGTSTP | CTRL + Z
SIGINFO | CTRL + T

jobs and kill

  • To suspend a background process, begin by finding the job_id with jobs
jobs
[1]+ Running nohup vlc --intf dummy --play-and-exit "/Users/admin/Music/rain30.mp3" > /dev/null 2>&1 &
  • Then, use the kill command and specify that you want to use SIGTSTP with the job_id prefixed with %
kill -SIGTSTP %1
[1]+ Stopped nohup vlc --intf dummy --play-and-exit "/Users/admin/Music/rain30.mp3" > /dev/null 2>&1
  • This has suspended/paused the rain in the background
  • To resume the suspended job (the background rain) press bg or bg %1 (or other job_id if you have many)

--

--

No responses yet