Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Linux Job Control &, disown, and nohup
Linux is an open-source operating system that is widely used in industry. One of the most notable features of Linux is its command-line interface (CLI), which provides flexibility and power to users. When working in the command line, it is common to run multiple commands concurrently. However, sometimes, you may want to control how these commands are executed and how they interact with each other. This is where job control comes in.
Job Control
Job control refers to the ability to manage and manipulate running processes in a Linux system. It allows users to start, stop, pause, resume, and control execution of multiple processes concurrently. In Linux, job control is implemented using a set of built-in shell commands, such as bg, fg, jobs, and kill. These commands are used to manage jobs (i.e., running processes) in the current shell session.
Background Execution with &
One of the most common use cases of job control is to run a command in the background. This means that the command is executed without blocking the shell prompt, allowing you to continue working in the same shell session. To run a command in the background, you can append an ampersand (&) symbol at the end of the command.
For example, let's say you want to run a long-running command that takes a lot of time to complete. You can run the command in the background using the following syntax
$ long_running_command &
The shell prompt will return immediately, and the command will be executed in the background. You can continue working in the same shell session while the command is running in the background.
Managing Background Jobs
Once you have started a background job, you may want to manage it, such as bringing it to the foreground, sending it to the background, or killing it. The following are some of the most commonly used commands to manage background jobs
jobs Command
The jobs command displays a list of all running jobs in the current shell session. It provides information such as job ID, status, and command.
$ jobs
[1] Running long_running_command & [2]- Stopped another_command
The above output shows that there are two jobs running in the current shell session. The first job is running in the background, while the second job is stopped.
fg Command
The fg command brings a background job to the foreground, allowing you to interact with it directly.
$ fg %1
The above command brings the job with ID 1 to the foreground. You can now interact with the job directly.
bg Command
The bg command sends a stopped or running job to the background, allowing it to continue running in the background.
$ bg %2
The above command sends the job with ID 2 to the background. The job will continue running in the background.
kill Command
The kill command sends a signal to a running job, allowing you to terminate the job.
$ kill %1
The above command sends a SIGTERM signal to the job with ID 1, terminating it.
The disown Command
disown is a shell command that is used to remove a job from the shell's job control. When you disown a job, it no longer belongs to the current shell session, and it will not receive any signals from the shell. This means that the job will continue running even if you close the shell session.
The syntax for disown is as follows
$ disown [job ID]
For example
$ long_running_command & $ disown %1
The above commands start a long-running command in the background and then disown it, removing it from the shell's job control. This means that even if you close the shell session, the command will continue running in the background.
disown Options
The -h option is used to mark a job as not to be killed when the shell receives a SIGHUP signal. This is similar to the nohup command, but it is done after the job has already started.
$ long_running_command & $ disown -h %1
The nohup Command
nohup is a utility command that is used to run a command immune to hangups (i.e., disconnecting from the terminal). When you run a command using nohup, it will continue running even if you log out of the system, close the terminal window, or lose network connection.
The syntax for nohup is as follows
$ nohup [command] &
For example
$ nohup long_running_command &
The above command runs long_running_command in the background, making it immune to hangups. This means that the command will continue running even if you log out of the system, close the terminal window, or lose network connection.
nohup is particularly useful when you are running a long-running command that you want to continue running in the background, even if you are not connected to the system. By default, nohup redirects output to a file called nohup.out.
Advanced Job Control
In addition to the basic usage of job control commands, there are some additional keyboard shortcuts and features that can be used
Keyboard Shortcuts
Ctrl + Z This keyboard shortcut is used to suspend a running job and send it to the background. You can bring the job back to the foreground using the
fgcommand.Ctrl + C This keyboard shortcut is used to terminate a running job by sending a SIGINT signal to it.
$ long_running_command ^Z [1]+ Stopped long_running_command $ bg %1
Comparison
| Method | When to Use | Survives Shell Exit | Immune to SIGHUP |
|---|---|---|---|
| & | Run command in background | No | No |
| disown | Detach existing job from shell | Yes | With -h option |
| nohup | Run command immune to hangups | Yes | Yes |
Conclusion
Linux job control, disown, and nohup are powerful commands that provide users with flexibility and control over running processes. Job control allows you to manage processes in the current shell session, while disown and nohup provide ways to run processes that survive terminal disconnection and shell exit.
