Several ways to run the Linux process in the background (screen)

Source: Internet
Author: User
Tags rsync

ctrl+z/bg/nohup/setsid/&

In Linux, if you want the process to run in the background, in general, we'll add & after the command, in fact, this is putting the command into a job queue:

./rsync.sh Jobs

However, if the process is executed above the background, its parent process is still the process of the current terminal shell, and once the parent process exits, the hangup signal is sent to all child processes, and the child process will exit after receiving hangup. If we are going to continue running the process while exiting the shell, we need to nohup either ignore the hangup signal or setsid set the parent process to the INIT process (process number 1): For commands that have already been executed in the foreground, you can also put them back in the background, first press CTRL + Z pauses a process that is already running, and then uses the BG command to put the stopped job in the background: bg %1 , put it back in the foreground to run: %1 .

# nohup./rsync. sh &# Setsid. /rsync. sh & or # (. /rsync. SH /// /execute in a subshell PS -ef| grep rsync

The purpose of Nohup is to have the submitted command ignore the hangup signal, and the standard output and standard error-deficient capital are redirected to the Nohup.out file. In general we can add "&" at the end to run the command in the background, or "> Log.out 2>&1" To change the default redirect file name.

The above experiment demonstrates the use of Nohup/setsid plus & to make the process run in the background without being affected by the current shell exit. So what do you do with processes that are already running in the background? You can use the disown command:

-H%1PS -ef| grep rsync

The effect is the same as SetID, but cannot be viewed by command after disown jobs .

Screen

There is also a more powerful way is to use screen, first create a disconnected mode of the virtual terminal, and then use the -r option to reconnect the virtual terminal, in which any command executed, can achieve the effect of nohup, which is more convenient when multiple commands need to be executed in the background continuously.

GNU Screen is a free software developed by the GNU Program for command-line terminal switching. The software allows users to connect multiple local or remote command lines simultaneously and freely switch between them, which can be viewed as a command-line interface version of the window manager. It provides a unified interface for managing multiple sessions and corresponding functions.


Yum Install screen-y

Common screen Parameters:

# screen-s docker-d New one named docker--dms docker-d New one named docker--ls-R docker-d Revert to Zhouxiao this session, provided it is already disconnected (--x docker-D is connected to the offline mode of the session (Multi-Window synchronous demo) # screen. /rsync. SH  -wipe Check all current screen jobs and delete screen jobs that are not already available

Normally, when you exit the last program (usually bash) in a window, the window is closed. Another way to close the window is to use C-a K, which kills the current window and kills the running process in the window.

Under each screen session, all commands start with Ctrl + A (C-A).

W Show all Windows list C-A K this shortcut will kill the current window and also kill the running process in this window. C-A D detach, leaving the current session temporarily

The above is just the basic and most commonly used usage, more please refer to or the Linux screen command in detail man screen . It is necessary to understand that a user-created screen, other users (even root) through screen -ls is invisible. In addition, Ctrl+a under bash is used to go back to the beginning of the line, unfortunately with the above combination shortcut keys conflict.

Original connection: http://seanlook.com/2014/02/20/linux-process-running-background-screen/

Several ways to run the Linux process in the background (screen)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.