Make procedures execute background through nohup in Linux system

Products

Virtual Machines

2022-02-25 14:40:35

The Linux system uses nohup to put the program to the background for execution

Sometimes, a command needs to be executed in the background without affecting the entry of the command line. Also, the user logout will not affect the execution of the process. At this moment, the nohup and "&" functions can be used.

As its name implies, Nohup disables the subsequent commands to respond to the hangup (SIGHUP) signal. In other words, after nohup is executed upon remote login, even after logging out, the program will still execute normally. Under normal circumstances, the nohup command will be followed by the "&" character at the end, indicating that the command will be executed in the background, so that the command can be placed in the background for continuous execution.

Example of use:

  1. The normal execution command is bash hello.sh, and the execution result is a miniprogram that outputs one line per second:

  2. Add nohup and & at the beginning and end of the command to change it to nohup bash hello.sh &. You can see that nohup outputs a line of information. Then, press the Enter key to jump back to the shell command line. Here, the command has been executed in the background. Nohup redirects the output of the command to the "nohup.out" file in the current directory. In addition, it shall be noted that nohup will output the PID of the corresponding program. This PID can be recorded to kill process later.

  3. Through tail -f nohup.out, you can continuously view the output of nohup.out, thus continuously monitoring the program.

  4. You can also use redirection in the command to change the output of the program to the filename you like, such as nohup bash hello.sh >hello.log &. Then, the output of the program will be written to the hello.log file.

  5. If the program does not exit automatically, you need to use the Kill command to kill the process at this moment. You can use the command kill -TRM PID_NUMBER, where PID_NUMBER is the value output by nohup before, 1231 in this case.

If your problem cannot be solved, please submit a ticket to us.

Feedback

开始与售前顾问沟通

可直接拨打电话 400-098-8505转1

我们的产品专家为您找到最合适的产品/解决⽅案

在线咨询 5*8⼩时

1v1线上咨询获取售前专业咨询

点击咨询
企微服务助手

专业产品顾问,随时随地沟通