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

Gui*_*mos 50 linux

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

(time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt
Run Code Online (Sandbox Code Playgroud)

虽然,1是我用来运行这项工作的一些过程.我必须改变每次运行的进程数.每次使用很长时间才能完成.然后我想将它作为后台进程运行.

我该怎么做?

我试过了:

nohup ((time bash executeScript 1 input fileOutput $> scrOutput) &> timeUse.txt)
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

Sha*_*hin 60

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

例如:

nohup bash -c "(time ./script arg1 arg2 > script.out) &> time_n_err.out" &
Run Code Online (Sandbox Code Playgroud)

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

所以,在你的情况下:

nohup bash -c "(time bash executeScript 1 input fileOutput > scrOutput) &> timeUse.txt" &
Run Code Online (Sandbox Code Playgroud)


Ton*_*Gao 31

您可以编写脚本然后使用nohup ./yourscript &来执行

例如:

vi yourscript
Run Code Online (Sandbox Code Playgroud)

#!/bin/bash
script here
Run Code Online (Sandbox Code Playgroud)

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

chmod u+rwx yourscript
Run Code Online (Sandbox Code Playgroud)

最后

nohup ./yourscript &
Run Code Online (Sandbox Code Playgroud)


Eri*_*rik 6

  • 使用屏幕:启动screen,启动脚本,按Ctrl+ A, D. 重新连接screen -r.

  • 创建一个以"1"作为参数的脚本,运行nohup yourscript:

    #!/bin/bash
    (time bash executeScript $1 input fileOutput $> scrOutput) &> timeUse.txt
    
    Run Code Online (Sandbox Code Playgroud)


Mos*_*ted 6

现代且易于使用的方法允许管理多个进程并具有漂亮的终端用户界面,但它是不幸的实用程序。

使用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
Run Code Online (Sandbox Code Playgroud)

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

用户界面