- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Linux Job Control &, disown, and nohup
Linux is an open-source operating system that is widely used in industry. One of most notable features of Linux is its command-line interface (CLI), which provides a lot of flexibility and power to users. When working in 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 refers to 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 current shell session.
One of most common use cases of job control is to run a command in background. This means that command is executed without blocking shell prompt, allowing you to continue working in same shell session. To run a command in background, you can append an ampersand (&) symbol at end of 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 command in background using following syntax −
$ long_running_command &
The shell prompt will return immediately, and command will be executed in background. You can continue working in same shell session while command is running in background.
Managing Background Jobs
Once you have started a background job, you may want to manage it, such as bringing it to foreground, sending it to background, or killing it. following are some of most commonly used commands to manage background jobs −
jobs − This command displays a list of all running jobs in current shell session. It provides information such as job ID, status, and command.
$ jobs  Running long_running_command & - Stopped another_command
The above output shows that there are two jobs running in current shell session. first job is running in background, while second job is stopped.
fg − This command brings a background job to foreground, allowing you to interact with it directly.
$ fg %1
The above command brings job with ID 1 to foreground. You can now interact with job directly.
bg − This command sends a stopped or a running job to background, allowing it to continue running in background.
$ bg %2
The above command sends job with ID 2 to background. job will continue running in background.
kill − This command sends a signal to a running job, allowing you to terminate job.
$ kill %1
The above command sends a SIGTERM signal to job with ID 1, terminating it.
Disown is a shell command that is used to remove a job from shell's job control. When you disown a job, it no longer belongs to current shell session, and it will not receive any signals from shell. This means that job will continue running even if you close shell session.
The syntax for disown is as follows −
$ disown [job ID]
$ long_running_command & $ disown %1
The above commands start a long-running command in background and then disown it, removing it from shell's job control. This means that even if you close shell session, command will continue running in background.
Nohup is a utility command that is used to run a command immune to hangups (i.e., disconnecting from terminal). When you run a command using nohup, it will continue running even if you log out of system, close terminal window, or lose network connection.
The syntax for nohup is as follows −
$ nohup [command]
$ nohup long_running_command &
The above command runs long_running_command in background, making it immune to hangups. This means that command will continue running even if you log out of system, close 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 background, even if you are not connected to system.
In addition to basic usage of job control, disown, and nohup commands, there are also some additional features and options that can be used to fine-tune their behavior. Here are some examples −
Ctrl + Z − This keyboard shortcut is used to suspend a running job and send it to background. This is similar to bg command, but it is done while job is still running. You can bring job back to foreground using fg command.
$ long_running_command ^Z + Stopped long_running_command $ bg %1
The above commands start a long-running command, suspend it using Ctrl + Z, and then send it to background using bg command.
Ctrl + C − This keyboard shortcut is used to terminate a running job by sending a SIGINT signal to it. This is similar to kill command with SIGINT signal.
$ long_running_command ^C
The above command starts a long-running command and terminates it using Ctrl + C.
-h option − This option is used to mark a job as not to be killed when shell receives a SIGHUP signal. This is similar to nohup command, but it is done after job has already started.
$ long_running_command & $ disown -h %1
The above commands start a long-running command in background and then mark it as not to be killed using disown command with -h option.
-p option − This option is used to write process ID (PID) of nohup command to a file. This can be useful if you need to monitor status of command or terminate it manually.
$ nohup -p pid_file long_running_command &
The above command starts a long-running command in background and writes its PID to pid_file using nohup command with -p option.
-e and -o options − These options are used to redirect standard error and standard output streams of command to a file. This can be useful for debugging purposes or for logging output of command.
$ nohup -e error_file -o output_file long_running_command &
The above command starts a long-running command in background and redirects its standard error and standard output streams to error_file and output_file, respectively, using nohup command with -e and -o options.
In conclusion, Linux job control, disown, and nohup are powerful commands that provide users with a lot of flexibility and control over running processes in Linux system. Job control allows you to manage running processes in current shell session, while disown and nohup provide a way to run processes immune to hangups and detached from shell's job control.
By mastering these commands, you can significantly improve your productivity and efficiency when working in Linux command line.
Kickstart Your Career
Get certified by completing the courseGet Started