Do you often need SSH or telent to remote Linux server? Do you tired for some long time running tasks , such as system backup, FTP transmission. Usually we are open a terminal interface for each of these tasks, because of their execution time is too long. You have to wait for it to finish executing, can’t close the terminal or disconnected during this period, otherwise this task will be killed.
When the network is disconnected or terminal window is closed, the control process receives the SIGHUP signal from the other processes, then the processes will exit during the period of the conversation.

Let’s take a look the below example that the sighup signal will terminate the process:

Open the two SSH terminal and run “top” command in one terminal

1[root@devops ~]# top
2top- 21:44:55 up 159 days, 4:09, 2 users, load average: 0.11, 0.03, 0.01
3Tasks: 65 total, 1 running, 64 sleeping, 0 stopped, 0 zombie
4Cpu(s): 0.0%us, 0.3%sy, 0.0%ni, 99.7%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
5Mem: 500832k total, 480212k used, 20620k free, 2048k buffers
6Swap: 0k total, 0k used, 0k free, 41496k cached

In another terminal, getting that the process ID of top is 8535, it’s parent ID is 8520.

1[root@devops ~]# ps -ef | grep top
2root 8535 8520 0 21:44 pts/1 00:00:00 top
3root 8537 8503 0 21:46 pts/0 00:00:00 greptop
4[root@devops ~]#

Using “pstree” command to see the relationship clearly:

1[root@devops ~]# pstree -H 8535| grep top
2| `-sshd---bash---top
3[root@devops ~]#

We can get the information from “ps -xj” command’s output that the “bash shell” and “top” processes are in the same conversation period, those parent ID is 8520 .

1[root@devops ~]# ps -xj|grep 8520
2Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
38518 8520 8520 8520 pts/1 8535 Ss 0 0:00 -bash
48520 8535 8535 8520 pts/1 8535 S+ 0 0:00 top
58503 8542 8541 8503 pts/0 8541 S+ 0 0:00 grep8520

Now close the first terminal, and check the process info in another terminal interface. there was not any top process and it indicated that the top process was killed.

1[root@devops ~]# ps -ef | grep 8535
2root 8547 8503 0 21:51 pts/0 00:00:00 grep8535
3[root@devops ~]#

If we can ignore the SIGHUP signal, Closing the terminal should not affect the program running. The nohup command can do this, if the standard output standard error output is terminal, the nohup default will be redirected to the nohup.out file. Note the nohup command is to make the program to ignore the SIGHUP signal, but also need to use the mark & put it runs in the background.

1nohup<command> [argument…] &

Although the nohup is very easy to use, but is still relatively “simple”, it just can handle the simple command , and it will be trouble for human-computer interaction requires complex tasks .

How To Use Screen Command?

In fact, we can use a more powerful utility “screen”. you can install screen with the below yum command:

1#yum install screen

Users can create multiple screen window in a screen session, each of the screen window is a true telnet/SSH connection window. There are several ways to create a new screen session:

1. Type the following command:

1[root@devops ~]# screen

Screen will create a shell with a full screen window. You can perform any shell program, like in the SSH window. if you want to exit the session, just type “exit” command. if it is the only window for the screen session, the screen session exit, otherwise the screen automatically switches to the previous session.

2.Type Screen command with the program you want to execute

1[root@devops ~]# screen top

Screen has more advanced functions. You do not need to interrupt program in screen session and temporarily disconnect (detach) screen session, and reconnect this screen session in the following time (attach) , so that to control the program in screen session .

you can press “Ctrl”+”a” +”d” to detach current screen session and using “screen -r sessionID” to reattach the screen session.

1[root@devops ~]# screen top
2[detached]
3[root@devops ~]# screen -ls
4There is a screenon:
511048.pts-0.devops (Detached)
61 Socket in/var/run/screen/S-root.
7[root@devops ~]# screen -r 11048

If you want to know more help information for screen command, can press “ctrl”+”a”+”?”: