0

I have a python script, whose output is piped into a log file. I can restart the file from within with an os.exec call, but if i do that, the new process doesnt write its output into the log file. How can i keep the output redirection after restarting the process?

My script start:

python3.6 script.py >> home/pi/log/telegram_bot.log 2>&1 &

My restart line:

import os
import sys
os.execv(sys.executable, [sys.executable] + sys.argv)
Nexxurs
  • 53
  • 7
  • Any reason you're using os.execv and not subprocess? – Alan Jul 25 '17 at 17:32
  • @Alan they serve different purposes: subprocess spawns a child process (leaving you w/ two) and exec transforms the current process into the new one. @Nexxurs, I don't think the [`exec*`](https://docs.python.org/3.6/library/os.html#process-management) family provides an interface for redirecting streams. But why bother exec-ing your process instead of just calling your main function again? – wbadart Jul 25 '17 at 17:34
  • This is answered [here](https://stackoverflow.com/questions/8500047/how-to-inherit-stdin-and-stdout-in-python-by-using-os-execv) – user2722968 Jul 25 '17 at 17:47

1 Answers1

0

I can't reproduce this problem. Here's my first.py:

import os
import sys
os.execv(sys.executable, [sys.executable, "second.py"])

Here's second.py:

print("Hello")

Here's how I run it and check the result. As you can see, it works fine:

$ python first.py >> logfile 2>&1 &
[1] 3098
$ wait
$ cat logfile
Hello
that other guy
  • 116,971
  • 11
  • 170
  • 194