Saturday, February 26, 2022

[SOLVED] nohup multiple sequential commands not logging output

Issue

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. And it's executed in a remote machine. For instance and test purposes, take the following script 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. It's work just fine whith one execution using the following command:

nohup test.py &

The outputs are correctly saved in nohup.out file. To run in sequence I've done some research and found [this question][1] and I came up with the following command:

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. But the problem is that the output isn't been saved into nohup.out file. 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

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. So if there is some other command to use instead of nohup which could log python print's it will be welcomed too.


Solution

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. 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. 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.



Answered By - Lie Ryan
Answer Checked By - Clifford M. (WPSolving Volunteer)