简体   繁体   English

nohup多个顺序命令不记录输出

[英]nohup multiple sequential commands not logging output

I have a python script (which takes a lot of time to complete the execution) that I need to run several times varying the parameters. 我有一个python脚本(需要花费大量时间才能完成执行),我需要多次运行以更改参数。 And it's executed in a remote machine. 它在远程计算机上执行。 For instance and test purposes, take the following script test.py : 出于示例和测试目的,请使用以下脚本test.py

import time

print("\nStart time: {}".format(time.ctime()))
time.sleep(10)
print("End time: {}".format(time.ctime()))

For this I normaly use nohup . 为此,我通常使用nohup It's work just fine whith one execution using the following command: 使用以下命令执行一次即可正常工作:

nohup test.py &

The outputs are correctly saved in nohup.out file. 输出正确保存在nohup.out文件中。 To run in sequence I've done some research and found [this question][1] and I came up with the following command: 为了按顺序运行,我进行了一些研究,发现了[这个问题] [1],然后我想到了以下命令:

nohup $(python test.py; python test.py) &

Which works fine. 哪个工作正常。 I run the command, quickly logged out and in again and saw through htop the first execution running, then finishing and then the second one starting. 我运行命令,快速注销并再次登录,并通过htop看到第一个执行正在运行,然后完成,然后第二个开始。 But the problem is that the output isn't been saved into nohup.out file. 但是问题是输出没有保存到nohup.out文件中。 If I wait in terminal for both executions to finish, the following error is showed: 如果我在终端中等待两个执行完成,则会显示以下错误:

nohup: failed to run command 'Start': No such file or directory nohup:无法运行命令“开始”:没有这样的文件或目录

What am I doing wrong here? 我在这里做错了什么?

PS.: I need to log the outputs because I need to see the current progress of the script and know which error happened if it doesn't finish properly. PS .:我需要记录输出,因为我需要查看脚本的当前进度并知道如果未正确完成,则会发生哪个错误。 So if there is some other command to use instead of nohup which could log python print 's it will be welcomed too. 因此,如果有其他命令代替nohup可以记录python print的信息,也将受到欢迎。

The command you have: 您拥有的命令:

nohup $(python test.py; python test.py) &

Will attempt to execute the output of the script. 将尝试执行脚本的输出。 It's likely not what you wanted. 这可能不是您想要的。

What you wanted here is to have nohup start off a command that will execute the two commands in sequence. 您在这里想要的是让nohup开始一个命令,该命令将按顺序执行这两个命令。 The most straight forward program that can do this is to use run a child shell: 可以执行此操作的最直接的程序是使用运行子外壳程序:

nohup bash -c "python one.py; python two.py" &

As for a better way to do this, you might want to investigate tmux or screen. 作为一种更好的方法,您可能需要研究tmux或屏幕。 If you start off a command in a tmux/screen, not only you can detach the session from the currently running shell, you'd also be able to reconnect to the session later on to resume and interact with the program. 如果在tmux /屏幕上启动命令,不仅可以将会话与当前正在运行的shell分离,还可以稍后重新连接到该会话以恢复程序并与程序进行交互。

The nohup command is passed a utility and arguments for that utility. 向nohup命令传递了一个实用程序和该实用程序的参数

If you like to run your script in sequence via nohup, perhaps using bash as the utility isn't a bad idea 如果您想通过nohup依次运行脚本,也许使用bash作为实用程序不是一个坏主意。

nohup bash -c "python ./test.py; python ./test.py"

However, I recommend to look into using python's logging package as I consider nohup to be a workaround ( nohup only appends to nohup.out if the standard output is the terminal .) 但是,我建议使用python的logging包,因为我认为nohup是一种解决方法( 如果标准输出是terminal,则nohup仅追加到nohup.out 。)

Also, there is the approach of using a queue to manage the running of your tasks sequentially. 此外,还有一种使用队列来顺序管理任务运行的方法。
Here you needn't have to be verbose to run the same script twice. 在这里,您不必冗长地运行同一脚本两次。 Then again between what you have and writing a worker to consume the queue, I think what you've is simpler ¯\\_(ツ)_/¯ 再说一次,在你拥有的东西和写一个工人去消耗队列之间,我认为你拥有的东西更简单\\\\((ツ)_ /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM