Several methods to make processes run reliably in the CentOS system Background

Source: Internet
Author: User

Several methods to make processes run reliably in the CentOS system Background

We often encounter this problem. We use telnet/ssh to log on to a remote Linux server and run some time-consuming tasks. As a result, the task fails midway through due to network instability. After the command is submitted, how does one prevent the local terminal window/network disconnection? Here are some examples. You can select different methods for different scenarios to solve this problem.

Nohup/setsid/& scenario:

If a temporary command needs to be run for a long time, what method can be used to ensure its stable operation in the background?

Reason for hangup name

In earlier versions of Unix, each terminal communicates with the system through modem. When a user logs out, the modem hangs up. Similarly, when the modem is disconnected, it will send an hangup signal to the terminal to notify it to close all sub-processes.

Solution:

We know that when a user logs out or the network is disconnected, the supervisor will receive an HUP signal to close all its sub-processes. Therefore, our solution has two ways: either let the process ignore the HUP signal, or let the process run in a new session to become a child process that does not belong to this terminal.

1. nohup

Nohup is undoubtedly the first method we have come up. As the name suggests, nohup is used to make the submitted command ignore The hangup signal. Let's take a look at the help information of nohup:

NOHUP(1)UserCommandsNOHUP(1)NAMEnohup-runacommandimmunetohangups,withoutputtoanon-ttySYNOPSISnohupCOMMAND[ARG]...nohupOPTIONDESCRIPTIONRunCOMMAND,ignoringhangupsignals.--helpdisplaythishelpandexit--versionoutputversioninformationandexit

It can be seen that nohup is very convenient to use. You only need to add nohup before the command to be processed. The standard output and standard error will be redirected to the nohup. out file by default. Generally, we can add"&" To run commands in the background at the same time. It is also available">filename2>&1"To change the default redirection file name.

Nohup example
[root@pvcent107~]#nohuppingwww.ibm.com&[1]3059nohup:appendingoutputto`nohup.out'[root@pvcent107~]#ps-ef|grep3059root3059984021:06pts/300:00:00pingwww.ibm.comroot3067984021:06pts/300:00:00grep3059[root@pvcent107~]#

2. Setsid

Nohup can undoubtedly avoid interruption of our process by ignoring the HUP signal. However, if we think from another perspective that our process is not a sub-process of the terminal that accepts the HUP signal, then it will naturally not be affected by the HUP signal. Setsid can help us do this. Let's take a look at the help information of setsid:

SETSID(8)LinuxProgrammer’sManualSETSID(8)NAMEsetsid-runaprograminanewsessionSYNOPSISsetsidprogram[arg...]DESCRIPTIONsetsidrunsaprograminanewsession.

It can be seen that the use of setsid is also very convenient, you only need to add setsid before the command to be processed.

Setsid example
[root@pvcent107~]#setsidpingwww.ibm.com[root@pvcent107~]#ps-ef|grepwww.ibm.comroot310941007:28?00:00:00pingwww.ibm.comroot3110229217007:29pts/400:00:00grepwww.ibm.com[root@pvcent107~]#

In the preceding example, the process ID (PID) is 31094, and its parent ID (PPID) is 1 (that is, the init process ID ), it is not the process ID of the current terminal. Compare this example with the parent ID in the nohup example.

3. &

Here is also a tip about subshell. We know that the inclusion of one or more names in "()" allows these commands to run in the sub-shell, thus extending many interesting functions, we will discuss one of them now.

After we put "&" into "()", we will find that the submitted jobs are not in the job list, that is, they cannot passjobs. Let's take a look at why we can escape the influence of the HUP signal.

Subshell example
[root@pvcent107~]#(pingwww.ibm.com&)[root@pvcent107~]#ps-ef|grepwww.ibm.comroot162701014:13pts/400:00:00pingwww.ibm.comroot1627815362014:13pts/400:00:00grepwww.ibm.com[root@pvcent107~]#

From the above example, we can see that the parent ID (PPID) of the newly submitted process is 1 (PID of the init process) and is not the process ID of the current terminal. Therefore, it does not belong to the sub-process of the current terminal, so it will not be affected by the HUP signal of the current terminal.

Back to Top

Disown scenario:

We already know that adding nohup or setsid before the command can avoid the influence of the HUP signal. But if we have submitted the command without any processing, how can we remedy it to avoid the impact of the HUP signal?

Solution:

It is too late to add nohup or setsid. You can only solve this problem through job scheduling and disown. Let's take a look at the help information of disown:

disown[-ar][-h][jobspec...]Withoutoptions,eachjobspecisremovedfromthetableofactivejobs.Ifthe-hoptionisgiven,eachjobspecisnotremovedfromthetable,butismarkedsothatSIGHUPisnotsenttothejobiftheshellreceivesaSIGHUP.Ifnojobspecispresent,andneitherthe-anorthe-roptionissupplied,thecurrentjobisused.Ifnojobspecissupplied,the-aoptionmeanstoremoveormarkalljobs;the-roptionwithoutajobspecargumentrestrictsoperationtorunningjobs.Thereturnvalueis0unlessajobspecdoesnotspecifyavalidjob.

We can see that we can achieve our goal in the following ways.

Flexible Use of CTRL-z

In our daily work, we can use CTRL-z to suspend the current process to the background and execute some other operations, then, use fg to re-run the suspended process back to the foreground (you can also use bg to put the suspended process in the background) to continue running. In this way, you can flexibly switch between multiple tasks in one terminal, which is particularly useful in code debugging. Because when the code editor is suspended to the background and then placed back, the cursor position remains at the position of the last suspension, avoiding the trouble of locating again.

  • Usedisown -hjobspecTo enableA job ignores the HUP signal.

  • Usedisown -ahTo enableAll jobs ignore the HUP signal.

  • Usedisown -rhTo enableThe running job ignores the HUP signal.

It should be noted that after disown is used, the target job will be removed from the job list and we will no longer be able to usejobsTo view it, but still can useps -efFind it.

However, there is another problem. The operation object of this method is a job. If we add"&" To make it a job and run it in the background, everything is fine. We can usejobsCommand to obtain the list of all jobs. But if the current command is not run as a job, how can I get its job number? The answer is to press CTRL-z (hold down the Ctrl key while holding down the z key!

CTRL-z is used to Suspend the current process (Suspend), and then we can usejobsCommand to query its job number, and then usebgjobspecTo put it in the background and continue running. Please use this method with caution if suspension affects the running result of the current process.

Disown Example 1 (if the command has been run in the background with "&", you can directly use "disown ")
[root@pvcent107build]#cp-rtestLargeFilelargeFile&[1]4825[root@pvcent107build]#jobs[1]+Runningcp-i-rtestLargeFilelargeFile&[root@pvcent107build]#disown-h%1[root@pvcent107build]#ps-ef|greplargeFileroot4825968109:46pts/400:00:00cp-i-rtestLargeFilelargeFileroot4853968009:46pts/400:00:00greplargeFile[root@pvcent107build]#logout
Disown Example 2 (if you do not use "&" to run the command in the background when submitting the command, you can use CTRL-z and "bg" to put it in the background, and then use "disown ")
[root@pvcent107build]#cp-rtestLargeFilelargeFile2[1]+Stoppedcp-i-rtestLargeFilelargeFile2[root@pvcent107build]#bg%1[1]+cp-i-rtestLargeFilelargeFile2&[root@pvcent107build]#jobs[1]+Runningcp-i-rtestLargeFilelargeFile2&[root@pvcent107build]#disown-h%1[root@pvcent107build]#ps-ef|greplargeFile2root57905577110:04pts/300:00:00cp-i-rtestLargeFilelargeFile2root58245577010:05pts/300:00:00greplargeFile2[root@pvcent107build]#

Back to Top

Screen scenario:

We already know how to protect the process from the HUP signal. But if a large number of such commands need to be run in a stable background, how can we avoid such operations on each command?

Solution:

The most convenient method is screen. Simply put, screen provides an ANSI/VT100 Terminal simulator to run multiple full-screen pseudo terminals under a real terminal. Screen has many parameters and has powerful functions. Here we will only introduce its common functions and briefly analyze why screen can avoid the impact of HUP signals. Let's take a look at the help information of screen:

SCREEN(1)SCREEN(1)NAMEscreen-screenmanagerwithVT100/ANSIterminalemulationSYNOPSISscreen[-options][cmd[args]]screen-r[[pid.]tty[.host]]screen-rsessionowner/[[pid.]tty[.host]]DESCRIPTIONScreenisafull-screenwindowmanagerthatmultiplexesaphysicalterminalbetweenseveralprocesses(typicallyinteractiveshells).EachvirtualterminalprovidesthefunctionsofaDECVT100terminaland,inaddition,severalcontrolfunctionsfromtheISO6429(ECMA48,ANSIX3.64)andISO2022standards(e.g.insert/deletelineandsupportformultiplecharactersets).Thereisascrollbackhistorybufferforeachvirtualterminalandacopy-and-pastemechanismthatallowsmovingtextregionsbetweenwindows.

Screen is easy to use and has the following common options:

  • Usescreen -dmSsession nameTo create a session in disconnected mode (and specify its session name ).

  • Usescreen -listTo list all sessions.

  • Usescreen -rsession nameTo reconnect to the specified session.

  • Use shortcutsCTRL-a dTo temporarily disconnect the current session.

Screen example
[root@pvcent107~]#screen-dmSUrumchi[root@pvcent107~]#screen-listThereisascreenon:12842.Urumchi(Detached)1Socketin/tmp/screens/S-root.[root@pvcent107~]#screen-rUrumchi

When we use "-r" to connect to the screen session, we can do whatever we want in this Pseudo Terminal, and no longer have to worry about the impact of the HUP signal on our processes, you do not need to add "nohup" or "setsid" to each command. Why? Let me take a look at the two examples below.

1. Process tree of the new process when screen is not used
[root@pvcent107~]#pingwww.google.com&[1]9499[root@pvcent107~]#pstree-H9499init─┬─Xvnc├─acpid├─atd├─2*[sendmail]├─sshd─┬─sshd───bash───pstree│└─sshd───bash───ping

We can see that when screen is not used, bash is a sub-process of sshd. When ssh is disconnected, the HUP signal will naturally affect all sub-processes (including the newly created ping process) under it ).

2. Process tree of the new process after screen is used
[root@pvcent107~]#screen-rUrumchi[root@pvcent107~]#pingwww.ibm.com&[1]9488[root@pvcent107~]#pstree-H9488init─┬─Xvnc├─acpid├─atd├─screen───bash───ping├─2*[sendmail]

When screen is used, bash is the sub-process of screen, while screen is the sub-process of init (PID is 1. When ssh is disconnected, the HUP signal will not affect the sub-processes under the screen.

Summary

Now several methods have been introduced. We can select different solutions based on different scenarios. Nohup/setsid is undoubtedly the most convenient method for temporary needs. disown can help us to remedy jobs that are already running afterwards, screen is the best choice for batch operations.

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.