How to run process in background Linux

As a rule, the commands in the terminal is connected with one disadvantage — before writing the next command must wait for the previous one is completed. This occurs because the current process blocks access to the operating system shell and in such cases we say that the command runs in the foreground. What to do if you want to run multiple commands at the same time? There are several solutions. The first and most obvious is to open an additional terminal window. The second is to initiate execution of the command in the background.

If any process happens in the background, this means that it does not provide for user interaction, therefore, the shell access remains free. After reading this article, you will learn how to run process in background Linux and what to do to their execution has not been interrupted after the closure of the terminal.

How to run process in background Linux

To run a command in background just add at the end the ampersand (&):

command &

The output terminal will display the sequence number of the task (in square brackets) and the process ID:

In the background you can run two, three, four processes or even more.

Working in the background, the team still continues to bring the message to the terminal from which it was launched. To do this it uses stdout and stderr, which can be closed by using the following syntax:

command > /dev/null 2>&1 &

Here >/dev/null 2>&1 indicates that stdout is redirected to /dev/null and stderr to stdout.

To know the status of all the stopped and running in the background tasks in the current session of the terminal is possible by means of utility jobs using option -l:

jobs -l

The output contains the sequence number of the task, the ID of the background process, the task status and the name of the command that initiated the job.

At any time you can return the process from the background to the foreground. To do this, use the command fg:

fg

If the background is running multiple programs, you must also specify the number. For example:

fg %1

To complete the background process use kill command with the program number:

kill %1

How to translate the process into the background

If initially the process was started in the usual way, it can be translated in the background by doing the following:

  • To stop the command by pressing the key combination Ctrl+Z.
  • To translate the process into the background with the command bg.
  • bg

    Work processes in the background

    Run script in background linux is one thing, but it is necessary that it still worked after closing the terminal. The closure of the terminal by clicking on the x in the top corner of the screen entails the completion of all background processes. However, there are several ways to save them after the link with an interactive shell will abort. The first method is the removal of tasks from the job queue using the disown command:

    disown

    As in the previous cases, if there are many concurrent processes should indicate the number of the person against which an action will be performed

    disown %1

    Make sure there are no more tasks in the task list, you must use the already familiar utility jobs -l. And to see a list of all running processes (including disabled) command is issued

    ps aux

    The second way to keep running processes after you stop working terminal command nohup. It performs a different command that was specified as argument, while ignoring all the signals SIGHUP (the ones that gets the process when you close the terminal). To run the command in the background you need to write a command in the form:

    nohup command &

    As you can see in the screenshot, the command output is redirected to a file nohup.out. In this case, after logout or close the terminal the process is terminated. There are a number of programs that allow you to run multiple interactive sessions at the same time. The most popular of them are Screen and Tmux.

    • Screen or GNU Screen is a terminal multiplexer that allows you to run one working session, and within it open any number of Windows (virtual terminals). The processes in this program will be executed, even if the window is invisible or the program has stopped working.
    • Tmux is a more modern alternative to GNU Screen. However, the possibility of Tmux do not have fundamental differences in this program similarly, you can open multiple Windows within one session. Tasks running in Tmux, continue to execute, if the terminal was closed.

    Insights

    To run the script in background linux, just add at the end of the sign &. When you run commands in the background, there is no need to wait for the termination of one command in order to enter another. If you have any questions, please ask them in the comments.

    Source: losst.ru

    (Visited 5 times, 1 visits today)