Linux Command nohup + Screen

Source: Internet
Author: User

 

If you want to start Program To continue running, use nohup. However, if you want to activate SSH the next day, you can check the status of the program running yesterday and continue working. nohup will not work at this time and you need to use screen to achieve this purpose.

 

The nohup command can be used to execute other commands and ignore the sighup signal (run a command immune to hangups, with output to a non-tty. Run Command, ignoring hangup signals .) when the network connection of a virtual terminal is disconnected, the operating system sends a sighup signal (Hup = hangup, hanging up) to the running program. By default, this signal causes the program to exit.

A nohup

 

Question 1: When SSH is disabled, the program will no longer run. Why? Culprit: sighup signal. Let's see why closing the window/disconnecting will cause the running program to die.

 

In Linux/Unix, there are several concepts: Process Group: A collection of one or more processes. Each process group has a unique process group ID, that is, the ID of the Process leader process. Session: a set of one or more process groups. It has a unique session leader ). The ID of the first process. A single control terminal (controlling terminal) is available during the session ). The first process connecting to the control terminal is called the control process ). The process currently interacts with the terminal is called the foreground process group. Other process groups are called background process groups. According to posix.1, the default action of sighup is to terminate the program. When the terminal interface detects that the network connection is disconnected, it sends the hanging signal to the control process (the first process in the session ). If the first process of the session ends, the signal is sent to the foreground Process Group of the session. When a process exits and an orphan process group is generated, if any process in the orphan process group is in the Stop state, the sighup and sigcont signals are sent to all processes in the process group. Conclusion: when the network is disconnected or the terminal window is closed, that is, after SSH is disconnected, the control process receives the sighup signal and exits, causing other processes to exit during the session period. In short: Bash and so on are all their subroutines after the SSH is enabled. Once the SSH service is disabled, the system will kill all related processes !! Once SSH is disabled, the task in progress is canceled. For example, let's look at an example. Open two SSH terminal windows and run one of the top commands. [Root @ tivf09 root] # top in another terminal window, find that the top process ID is 5180, and its parent process ID is 5128, that is, log on to shell. [Root @ tivf09 root] # ps-Ef | grep toproot 5180 5128 0 000:00:02 pts/0 00:00:00 toproot 5857 3672 0 0pts/2 grep top use the pstree command to see more clearly this relationship: [root @ tivf09 root] # pstree-H 5180 | grep top |-sshd-+-sshd --- bash --- top run the PS-XJ command to view the result. log on to shell (PID 5128) in the same session period as top, shell is the first process in the session period. The pgid of the Process Group is 5128, And the pgid of the Process Group where top is located is 5180. It is the frontend process group. [Root @ tivf09 root] # ps-XJ | grep 5128 5126 5128 5128 pts/0 5128 S 0 0: 00-bash 5180 5128 5180 5180 pts/0 5128 S 0 0: 50 top 5180 3672 18094 3672 pts/2 18094 S 0 grep 5128 close the first SSH window, in another window, we can see that the top is also killed. [Root @ tivf09 root] # ps-Ef | grep 5128 root 18699 3672 0 00:00:00 pts/2 grep 5128 Question 2 Why does the daemon even open SSH, even if SSH is disabled, does it affect its operation? Because their program is special, for example, after httpd-K start runs this, it does not belong to the sshd process group but a separate process group, so even if SSH is disabled, it has nothing to do with him! [Root @ CentOS5-4 ~] # Pstree | grep HTTP |-HTTPd [root @ CentOS5-4 ~] # Pstree | grep top |-sshd-+-sshd --- bash --- top conclusion: the startup command of the daemon is special and different from the General Command, for example, a command such as mysqld_safe is run by the daemon once used. Therefore, it is impossible to transform a general program into a daemon. Question 3: Can I use the background running command to remove the program from the SSH process group control, that is, close SSH and continue running the background program? Let's do a test: Find/-name '* HTTP *' & use Ctrl + D to log out and then enter the system. Will this command be run again? The answer is: the command is suspended !! Because he still belongs to this SSH process group, even if it is added, he cannot get rid of it !! [Root @ CentOS5-4 ~] # Pstree | grep find |-sshd-+-sshd --- bash --- find: as long as it is a general command opened and executed by SSH, it is not a daemon &, once SSH is disabled, the system will use sighup to terminate the problem. 4 nohup can solve the problem. However, in order to log out and run it in the background, we can use the nohup command, now we start to find/-name '* HTTP *' & and want to run it in the background. Then we use nohup: nohup find/-name "* httpd *" at this time, the output information of the program running by default is put into the nohup in the current folder. adding or not to the out file does not affect this command, but it only allows the program to run on the foreground or background.

 

Although nohup is easy to use, it is still relatively "simple". It can handle simple commands and is troublesome for complicated tasks that require human-computer interaction. In fact, we can use a more powerful utility screen. Popular Linux distributions (such as Red Hat Enterprise Linux 4) usually come with screen utilities. If not, you can download them from the GNU screen official website.

 

1) run screen and press any key to enter the sub-interface. I run the ping command. If I get off work, but want to close SSH, ping the command to continue running, then press Ctrl + A and then press d to pause the sub-interface and display the [detached] words. At this time, I returned to the parent interface; use screen-ls to view the status screen-lsthere is a screen on: 22292. pts-3.free (detached) 1 socket in/tmp/Screens/S-root, Here 22292 is actually the child interface PID; if you return to the Child interface with screen-r 22292, A bullet arrives at the ping sub-interface; 2) more help can be done through C-A (CTRL + )? To view all key bindings. common key bindings include: C-? Show all key binding information C-a w show all windows List C-a c-a switch to the previously displayed window C-a c create a new shell window and switch to this window c-A n switch to the next window C-a p switch to the previous window (relative to C-a n) c-A 0 .. 9 switch to window 0 .. 9c-a A sends C-A to the current window C-a d temporarily disconnects the screen Session C-a k to kill the current window C-A [other common options for entering the copy/rollback mode: -C file: Use the configuration file instead of the default $ home /. screenrc-d |-d [pid. TTY. host] does not enable a new screen session, but disconnects other running screen sessions-H num specifies that the historical rollback buffer size is num row-list |-ls to list existing screen sessions, the format is PID. TTY. host-D-M starts a session in disconnected mode-r sessionowner/[pid. TTY. host] reconnect to a disconnected session. In multi-user mode, you must specify the sessionowner to connect to screen sessions of other users, setuid-root permission-s sessionname is required to specify a name for the session when the screen session is created-V displays the screen version information-Wipe [Match] Same-list, but deletes sessions that cannot be connected for reference: http://chlotte.blog.51cto.com/318402/556540 finished!

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.