Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the relationship between nohup and & under Linux?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Background

Have not been clear about the difference between nohup and &, looking at the shell left in the company, also confused with the use of … That's a bad attitude.

The conclusion is put in front.

&

Causes the process to run in the background and defaults to standard output (that is, the current screen) unless the output is redirected.

Ignore the SIGINT signal at this time.

If you close the session, the process ends

Nohup

The process is still running in the foreground and outputs to nohup.out by default.

At this point, the process ignores the SIGHUP signal, and closing the session does not affect the process.

Ctrl+c interrupts the process

Nohup + &

The process runs in the background, ignoring SIGINT and SIGHUP signals.

test

Repeat=$ {1RV Rose 20}

Number of echo cycles $repeat

Counter=0

While [$counter-lt repeat] do echo cycle {counter}

Let "counter + +"

Sleep 1

Done

Echo end cycle

Direct operation

[root@linuxidc tmp] # sh test.sh

Number of cycles 20

The 0th cycle

The first cycle

The second cycle

^ C

At this point, either direct Ctrl+c or closing the current ssh connection will cause the process to stop.

[root@linuxidc tmp] # sh test.sh > test.out

^ C

At this time, the process is still running at the front end. Looking at the test.out file, you will find that whether it is directly Ctrl+C or closing the current ssh connection, it will cause the process to stop.

Used alone &

[root@linuxidc tmp] # sh test.sh &

[1] 111418

[root@linuxidc tmp] # number of cycles 20

The 0th cycle

The first cycle

The second cycle

^ C

[root@linuxidc tmp] # third cycle

The fourth cycle

The fifth cycle

^ C

[root@linuxidc tmp] # the 6th cycle

The 7th cycle

The 8th cycle

The 9th cycle

The 10th cycle

The 11th cycle

At this point, the process runs in the background and is output to the screen by default.

The Ctrl+c is invalid at this time, that is, the process ignores the SIGINT signal.

However, if the ssh connection (session) is closed directly, the process will be closed because the process is affected by the SIGHUP signal.

The default processing of the SIGHUP signal is to terminate the process that receives the signal. So if the signal is not captured in the program, the process exits when the signal is received.

[root@linuxidc tmp] # sh test.sh > test.out &

[4] 111758

[root@linuxidc tmp] # http://www.iis7.com/a/lm/ftp/

Causes the process to run in the background and the output is redirected to the specified file

You can use jobs-l to get all current background processes

[root@linuxidc tmp] # jobs-l

[2] + 113043 Running sh test.sh > / dev/null &

Using nohup alone

[root@linuxidc tmp] # nohup sh test.sh > test.out 2 > & 1

After running, it runs at the front end. If you close the session, the process will not be affected.

But at this point Ctrl+c will end the process.

Nohup + &

[root@linuxidc tmp] # nohup sh test.sh &

[2] 112987

[root@linuxidc tmp] # nohup: ignore input and append output to "nohup.out"

[root@linuxidc tmp] #

Ignoring the impact of Ctrl+c and session closure, the process can run continuously in the background.

If the session is not closed, you can view the current process through jobs-l

[root@linuxidc tmp] # jobs-l

[1] + 112987 Running nohup sh test.sh &

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report