Linux tips: Keep the task running

Source: Internet
Author: User
Linux tips: Keep the task running at work and often encounter time-consuming commands, such as reposync an android full-project code. These commands are time-consuming and cannot always be stared. At this time, we need to do something to ensure that these tasks will not be linux Tips: to keep the tasks running at work, we often encounter time-consuming commands, such as repo sync an android full-project code. These commands are time-consuming and cannot always be stared. At this time, we need to do something to ensure that these tasks are continuously executed. I feel that the safe and reliable execution of engineering code is an important part of my work and is becoming more and more important. First, if an error occurs during command execution and you need to re-enter the command to continue the execution, you can write a script to help us complete the execution. Or the repo sync code error in the previous example? You can use the repo command to write the following shell script: [plain] while [$? = 1]; do repo sync-c done gives the most important part of this script, that is, once a command execution error occurs, the return value of the shell command is 1, which indicates an error, then continue to execute the original command. Here we can solve the command itself errors. Next we need to solve the common problem: network connection disconnection. A lab or team can share a good linux server. you can use putty and other tools to connect to the server and work under your own account. If you do not take any operation, if the putty connection is disconnected, the task will be terminated. First, we need to master some basic things: when the user logs out or the network is disconnected, the control process will receive the HUP signal to exit, and the signal will be sent to the front-end process Group, which eventually closes all processes. It seems a little complicated. to simplify it, after the putty is disconnected, the control process receives the HUP signal and exits, and the HUP signal also causes other processes to exit. The first method: nohup, as its name implies, is to let the submitted command ignore the hangup signal. If you use nohup directly in putty, shutting down the putty connection in the case of misoperations will still cause the task to be interrupted. Therefore, nohup is generally executed together with the & Command, which puts the task in the background process and ignores the interruption signal. In this way, the network connection is disconnected or the putty is disabled, and the task continues. The following is the nohup format: nohup [Argument…] & Method 2: setsidsetsid refers to the idea of the daemon process. this task is executed in a completely new process so that the process is not a sub-process that accepts the HUP signal, this will not affect the execution of the task. Setsid is easy to use, as follows: setsid [Argument…] After executing this command, run the pstree command to check that the task process is in a new process tree, so that the hanging signal will not affect the task execution. In addition, you can also use a very powerful application screen to prevent task interruptions. However, this is too powerful and very useful. it is only a small part to prevent task interruptions. I will mention it here and will not describe it in detail.
Related Article

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.