What is job control in linux

Jobs and Job Control in Linux

In the Linux operating system, jobs refer to processes that are running in the background or foreground. Job control refers to the ability to manipulate these processes, including suspending, resuming, and terminating them. This can be useful for managing multiple tasks or for debugging problems with a process.

Job control is made possible by the shell, which is a command-line interface that allows users to interact with the operating system. The most common shell in Linux is the Bourne Again Shell (BASH), but other shells such as the Z Shell (ZSH) and the Korn Shell (KSH) are also available.

In this article, we will explore the basics of job control in Linux and how to use it to manage processes.

Understanding Processes and Jobs in Linux

In Linux, every program that is running is considered a process. A process can be a standalone program or a part of a larger program.

Each process is assigned a unique identifier called a process ID (PID). The PID can be used to refer to the process and perform actions on it, such as suspending or terminating it.

A job is a process that is either running in the foreground or the background. The foreground is the active window in the terminal, and the background is any process that is running but not actively being used in the terminal.

By default, when you run a command in the terminal, it runs in the foreground. You can tell that a process is running in the foreground because it displays output and you cannot enter any more commands until it finishes.

To run a process in the background, you can use the & symbol at the end of the command.

Example

In this example, the sleep command causes the process to sleep for 30 seconds. The & symbol causes the process to run in the background, and the output [1] 12345 indicates that it is job number 1 with a PID of 12345.

Managing Jobs with the fg and bg Commands

The fg (foreground) and bg (background) commands allow you to move jobs between the foreground and the background.

To bring a background job to the foreground, you can use the fg command followed by the job number or the PID.

Example

This brings the sleep command, which is job number 1, to the foreground and displays its output.

To send a foreground job to the background, you can use the bg command followed by the job number or the PID.

Читайте также:  Virtualbox передать файл linux

Example

$ sleep 30 [1] 12345 ^Z [1]+ Stopped sleep 30 $ bg %1 [1]+ sleep 30 &

In this example, the sleep command is run in the foreground and then suspended with the ^Z keyboard shortcut. The bg command is then used to resume the job in the background.

Suspend or Resume Jobs in Linux

The suspend command allows you to temporarily stop a job, while the kill command allows you to permanently terminate a job.

To suspend a job, you can use the suspend command followed by the job number or the PID.

Example

$ sleep 30 & [1] 12345 $ suspend %1 [1]+ Suspended

This suspends the sleep command, which is job number 1. The job can then be resumed with the fg command or left suspended in the background.

To terminate a job, you can use the kill command followed by the job number or the PID.

Example

$ sleep 30 & [1] 12345 $ kill %1

This terminates the sleep command, which is job number 1.

View and Manage Jobs with the jobs and ps Commands

The jobs command allows you to view a list of all jobs running in the background or suspended in the current shell. The ps command allows you to view a list of all processes running on the system.

Example

$ sleep 30 & [1] 12345 $ sleep 60 & [2] 12346 $ jobs [1] Running sleep 30 & [2] Running sleep 60 &

You can use the jobs command to view the status of each job and its job number or PID.

The ps command allows you to view a list of all the processes that are currently running on the system. You can use the -a flag to show all processes and the -x flag to show processes that are not associated with a terminal.

Example

$ ps -ax PID TTY STAT TIME COMMAND 1 ? Ss 0:00 /sbin/init 2 ? S 0:00 [kthreadd] 3 ? S 0:00 [ksoftirqd/0] 4 ? S 0:00 [kworker/0:0] .

The ps command displays the PID, terminal (TTY), status, time, and command for each process.

Conclusion

In Linux, job control allows you to manage the processes that are running on your system. You can use commands such as fg, bg, suspend, and kill to manipulate jobs and the jobs and ps commands to view and manage them. Understanding and using these commands can help you effectively manage your system and troubleshoot problems.

Источник

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

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.

Читайте также:  Вывод системных переменных linux

Background Execution

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 −

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 [1] Running long_running_command & [2]- 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.

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.

The above command sends job with ID 2 to background. job will continue running in background.

The above command sends a SIGTERM signal to job with ID 1, terminating it.

Disown

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 −

$ 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

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 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.

Читайте также:  Linux утилиты командной строки

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.

Additional Information

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 [1]+ 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.

The above command starts a long-running command and terminates it using Ctrl + C.

Disown

  • -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.

Nohup

  • -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.

For example

$ 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.

For example

$ 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.

Conclusion

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.

Источник

Оцените статью
Adblock
detector