Linux Process background running methods (screen), linuxscreen

Source: Internet
Author: User

Linux Process background running methods (screen), linuxscreen
Ctrl + z/bg/nohup/setsid /&

In Linux, if you want to run the process in the background, we usually add & to the end of the command. In fact, the command is put into a Job Queue:

./rsync.sh &# jobs

However, for processes executed from the top to the backend, the parent process is still the shell process of the current terminal. Once the parent process exits, The hangup signal is sent to all sub-processes, the child process also exits after receiving the hangup. If we want to continue running the process when exiting the shell, we need to usenohupIgnore the hangup signal, orsetsidSet the parent process as the init process (process number 1): For commands that have been executed on the foreground, you can also run them in the background. First, press ctrl + z to pause the running process, run the bg command in the background:bg %1, Put it back to the foreground to run:%1.

# Nohup. /rsync. sh & # setsid. /rsync. sh & or #(. /rsync. sh &) // execute # ps-ef | grep rsync in a subshell

The purpose of nohup is to make the submitted command ignore The hangup signal. The standard output and standard error will be redirected to the nohup. out file by default .. Generally, you can add "&" at the end to run the command in the background, or use "> log. out 2> & 1" to change the default redirection file name.

The above test demonstrates that the process is run in the background with nohup/setsid and is not affected by the current shell exit. So what should we do for processes that are already running in the background? AvailabledisownCommand:

# jobs# disown -h %1# ps -ef|grep rsync

The effect is the same as that of setid, but disown cannot passjobsCommand.

Screen

Another more powerful method is to use screen. First, create a virtual terminal in disconnected mode, and then use-rOption to reconnect to this virtual terminal, and any command executed in it can achieve the nohup effect, which is more convenient when multiple commands need to be executed continuously in the background.

GNU Screen is a free software developed by GNU program for command line terminal switching. You can use this software to connect to multiple local or remote command line sessions at the same time, and switch between them freely. It can be seen as the 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 create a session named docker-d, and immediately go to # screen-dmS docker-d to create a session named docker-d, but not now, it can be used in the system startup script # screen-ls to list all the current sessions # screen-r docker-d to restore to the zhouxiao session, the premise is that the session is disconnected (-d can be remotely disconnected) # screen-x docker-d connects to the offline mode SESSION (Multi-Window Synchronization Demonstration) # screen. /rsync. sh screen creates a single window session for script execution. You can attach the process ID # screen-wipe to check all current screen jobs and delete unusable screen jobs.

Normally, when you exit the last program in a window (usually bash), the window is closed. Another way to close the window is to use C-a k. This shortcut will kill the current window and also kill the running process in the window.

In each screen session, all commands start with ctrl + a (C-.

C-a w: display the list of all windows. C-a k is the shortcut key used to kill the current window. It also kills the running process in the window. C-a d detach: temporarily leaves the current session

The above is just the basic and most common usage. For more information, seeman screenOr linux screen command. You need to know that the screen created by a user, other users (or even root users) Usescreen -lsIs invisible. In addition,Ctrl+aIn bash, it is used to return to the beginning of the line. Unfortunately, it conflicts with the combination shortcut key above.

Connection: http://seanlook.com/2014/02/20/linux-process-running-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.