Linux中nohup和ctrl+z命令的区别。

时间:2023-02-07 21:54:04

I want to run jobs in the background so that I can logout from terminal once any job is started. I know two ways

我想在后台运行作业,这样一旦开始工作,我就可以从终端退出。我知道两种方式

1) run job and then press ctrl+z and then enter bg

1)运行作业,然后按ctrl+z,然后输入bg。

mysqldump -uroot -p dbname  > dbname.sql

2) using nohup to run job and then press ctrl+z and then enter bg

2)使用nohup运行作业,然后按ctrl+z,然后输入bg。

nohup mysqldump -u root -p dbname  > dbname.sql 2>&1

I want to know the difference between above two commands and which one is best in which scenario.

我想知道这两个命令之间的区别,哪一个是最好的。

2 个解决方案

#1


2  

Running your process as a job constrains your job to the current session. So if you run

把你的工作流程作为一份工作来约束你的工作。所以,如果你运行

$ start_running_job &
$ exit

The job will stop when you exit.

当你离开时,工作就会停止。

Pressing Control-Z has the same effect as the lines above.

压控z的效果和上面的线一样。

Running nohup places the job so that it survives the end of the current session. So if you run

运行nohup可以让它在当前会话结束时存活下来。所以,如果你运行

$ nohup start_running_job &
$ exit

The job will continue running.

工作将继续进行。

#2


1  

I agree with answer provided...

我同意所提供的答案……

"nohup" basically runs your command/script in back-end mode on server itself and it is not related to your current login session while putting "&" just puts the script in background and remains connected to your current login session. In any case you can use fg to bring back script/command.

“nohup”基本上是在服务器本身的后端模式中运行您的命令/脚本,而它与您当前的登录会话无关,而“&”只是将脚本放在后台,并保持与当前登录会话的连接。无论如何,您可以使用fg恢复脚本/命令。

Which is good for you, for this it depends how you are accessing your server and for how long your script is gonna take to complete. Say your script will take more amount of time and your current login session remains ideal it will close down and script will get killed if you have only given &. So much of the time just give nohup and don't worry it.

这对你有好处,因为这取决于你如何访问你的服务器,以及你的脚本需要多长时间才能完成。说你的脚本需要更多的时间,你当前的登录会话仍然是理想的,如果你只给了&,脚本将会被杀死。所以,大部分的时间都是在给nohup,不要担心。

#1


2  

Running your process as a job constrains your job to the current session. So if you run

把你的工作流程作为一份工作来约束你的工作。所以,如果你运行

$ start_running_job &
$ exit

The job will stop when you exit.

当你离开时,工作就会停止。

Pressing Control-Z has the same effect as the lines above.

压控z的效果和上面的线一样。

Running nohup places the job so that it survives the end of the current session. So if you run

运行nohup可以让它在当前会话结束时存活下来。所以,如果你运行

$ nohup start_running_job &
$ exit

The job will continue running.

工作将继续进行。

#2


1  

I agree with answer provided...

我同意所提供的答案……

"nohup" basically runs your command/script in back-end mode on server itself and it is not related to your current login session while putting "&" just puts the script in background and remains connected to your current login session. In any case you can use fg to bring back script/command.

“nohup”基本上是在服务器本身的后端模式中运行您的命令/脚本,而它与您当前的登录会话无关,而“&”只是将脚本放在后台,并保持与当前登录会话的连接。无论如何,您可以使用fg恢复脚本/命令。

Which is good for you, for this it depends how you are accessing your server and for how long your script is gonna take to complete. Say your script will take more amount of time and your current login session remains ideal it will close down and script will get killed if you have only given &. So much of the time just give nohup and don't worry it.

这对你有好处,因为这取决于你如何访问你的服务器,以及你的脚本需要多长时间才能完成。说你的脚本需要更多的时间,你当前的登录会话仍然是理想的,如果你只给了&,脚本将会被杀死。所以,大部分的时间都是在给nohup,不要担心。