Several ways to run Linux---programs in the background

Source: Internet
Author: User
Tags sigint signal

Sometimes it takes a long time to run a program, so it's a bit crazy to get off the clock or when the network is unstable. Today I'm going to share a few of the methods I've used in my work to keep the program running in the background.

Nohup
$ nohup--husage:nohup COMMAND [ARG] ...  Or:  nohup optionrun COMMAND, ignoring hangup signals.      --help Display this help and     exit      --version  output version information and exit

When running a program, we exit the current account, or close the terminal, the program will receive a SIGHUP signal, nohup is to ignore this signal, so that the program does not hang. Nohup is very simple to use, namely: Nohup command. There are several tips for using the process:

1. Simultaneous use of Nohup and &

How to use: Nohup Command &

When exiting the current account or closing the terminal, the program will receive the SIGHUP signal, this signal will be used by the NOHUP program ignored, but if you use the CTRL + C end command, the program will receive the SIGINT signal, the signal will still let the program end, if you do not want the program to be ended, Add a & at the end, and let the program also ignore the SIGINT signal.

2. REDIRECT standard output and standard error

How to use: Nohup command > FileName 2<&1 &

Here is the file descriptor, 0 for stdin standard input, 1 for stdout standard output, 2 for stderr standard error, and one/dev/null for empty device files.

Nohup output to the Nohup.out file by default, if you want to redirect output to another file, you need to add > filename after the nohup command, 2<&1, to redirect the standard error to the standard output.

Setsid
$ setsid--husage:setsid [options] <program> [arguments ...] Run a program in a new session. Options:-C,--ctty set the controlling terminal to the current     one-w,--wait wait program to     exit, and use the S Ame Return-h,--help     Display this help and exit-v,--version  output version information and exit

  

Nohup prevents the process from being interrupted halfway by ignoring the HUP signal, and is not affected by the HUP signal if our process is not part of the terminal subprocess that accepts the HUP signal. Using Setsid can help us do this.

How to use: Setsid command

Several ways to run Linux---programs in the background

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.