1

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.

1 Answers1

1

The $(...) is resolved first, only then nohup. I.e. when you run nohup $(python test.py; python test.py) & this is what happens:

  1. The shell runs $(...) which returns string Start time ... and then
  2. Runs nohup Start time ... which obviously fails because there is no such command Start.

Since nohup only can run one command this is a common workaround:

~ $ nohup sh -c "python test.py; python test.py" &

So you run sh in nohup and it then executes the commands you want.

I-P-X
  • 163
  • 10