如何使用 nohup 在 linux 中将进程作为后台进程运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5164985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:26:43  来源:igfitidea点击:

How can I use nohup to run process as a background process in linux?

linux

提问by Guilgamos

I use this command to run my work.

我使用这个命令来运行我的工作。

(time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt

While, 1 is a number of process that I use to run this work. I have to change the number of process for each run. At each time it use long time to complete. Then I want to run it as background process.

而 1 是我用来运行这项工作的一些进程。我必须更改每次运行的进程数。每次都需要很长时间才能完成。然后我想将它作为后台进程运行。

How can I do it?

我该怎么做?

I tried:

我试过:

nohup ((time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt)

But it doesn't work.

但它不起作用。

回答by Erik

  • Use screen: Start screen, start your script, press Ctrl+A, D. Reattach with screen -r.

  • Make a script that takes your "1" as a parameter, run nohup yourscript:

    #!/bin/bash
    (time bash executeScript  input fileOutput $> scrOutput) &> timeUse.txt
    
  • 使用屏幕: Start screen,启动您的脚本,按Ctrl+ A, D。用 重新连接screen -r

  • 制作一个以“1”为参数的脚本,运行nohup yourscript

    #!/bin/bash
    (time bash executeScript  input fileOutput $> scrOutput) &> timeUse.txt
    

回答by Shawn Chin

In general, I use nohup CMD &to run a nohup background process. However, when the command is in a form that nohupwon't accept then I run it through bash -c "...".

一般来说,我nohup CMD &用来运行一个 nohup 后台进程。但是,当命令采用nohup不接受的形式时,我会通过bash -c "...".

For example:

例如:

nohup bash -c "(time ./script arg1 arg2 > script.out) &> time_n_err.out" &

stdout from the script gets written to script.out, while stderr and the output of timegoes into time_n_err.out.

脚本中的 stdout 被写入script.out,而 stderr 和 的输出time进入time_n_err.out.

So, in your case:

所以,在你的情况下:

nohup bash -c "(time bash executeScript 1 input fileOutput > scrOutput) &> timeUse.txt" &

回答by Tong Gao

You can write a script and then use nohup ./yourscript &to execute

可以写一个脚本然后nohup ./yourscript &用来执行

For example:

例如:

vi yourscript

put

#!/bin/bash
script here

you may also need to change permission to run script on server

您可能还需要更改在服务器上运行脚本的权限

chmod u+rwx yourscript

finally

最后

nohup ./yourscript &