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

61

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

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

我使用数字1来运行这个工程,但每次运行都需要更改进程数,而且每次运行都需要很长时间才能完成。因此,我想将其作为后台进程运行。

我尝试过:

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

但它不起作用。


nohup <command> & disown - SUMIT
4个回答

69

通常情况下,我使用nohup CMD &来运行一个后台进程。但是,如果命令的形式不被nohup接受,则会通过bash -c "..."运行它。

例如:

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

脚本的标准输出被写入script.out,而错误输出和time的输出被写入time_n_err.out

因此,在您的情况下:

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

37

你可以编写一个脚本,然后使用 nohup ./yourscript & 来执行。

例如:

vi yourscript

放置

#!/bin/bash
script here

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

chmod u+rwx yourscript

最终

nohup ./yourscript &

这个符号 '&' 在这里是做什么用的? - codingInMyBasement
@acodejdatam,它在后台启动进程而无需等待其完成。 - josemmo

8
  • 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 $1 input fileOutput $> scrOutput) &> timeUse.txt
    

6

现代且易于使用的方法,可以管理多个进程并具有良好的终端UI的实用程序是hapless

使用pip install hapless(或python3 -m pip install hapless)进行安装,然后运行即可。

$ hap run my-command  # e.g. hap run python my_long_running_script.py
$ hap status  # check all the launched processes

请参阅文档以获取更多信息。

ui


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接