admin管理员组文章数量:1312982
I am running a Python 3 script with flag -u
to send output to a file like a log
The exact command i use is
/usr/bin/python3 -u /home/pi/MyScript.py >> /home/pi/myScript.log
This works like a charm.
Now for some reason I need to restart the MyScript.py
to re-initialize some functions inside, so I have found that os.execl
function could be the right solution.
My problem is understand how to pass the -u
and >>
options to command, maybe it will be very easy but I'm spending a lot of time without result...
I have tried a lot of ways, one for example is this:
os.execv('/usr/bin/python3', ['/usr/bin/python3'] + ['-u'] + ['./PythonMqttMaster.py'] + [' >> '] +['PythonMqttMaster.log'])
but the output go to terminal instead of another file, without error.
Maybe I'm using a totally wrong approach, let me know if so.
I am running a Python 3 script with flag -u
to send output to a file like a log
The exact command i use is
/usr/bin/python3 -u /home/pi/MyScript.py >> /home/pi/myScript.log
This works like a charm.
Now for some reason I need to restart the MyScript.py
to re-initialize some functions inside, so I have found that os.execl
function could be the right solution.
My problem is understand how to pass the -u
and >>
options to command, maybe it will be very easy but I'm spending a lot of time without result...
I have tried a lot of ways, one for example is this:
os.execv('/usr/bin/python3', ['/usr/bin/python3'] + ['-u'] + ['./PythonMqttMaster.py'] + [' >> '] +['PythonMqttMaster.log'])
but the output go to terminal instead of another file, without error.
Maybe I'm using a totally wrong approach, let me know if so.
Share Improve this question edited Feb 2 at 10:32 ZCGCoder 5322 gold badges10 silver badges29 bronze badges asked Feb 1 at 12:35 GiammariaGiammaria 31 bronze badge1 Answer
Reset to default 0You can use the os.execl function to start a shell and pass your entire command import os
# Restart the script with shell redirection
os.execl(
'/bin/sh', # Path to the shell executable
'sh', # First argument (name of the shell)
'-c', # Flag to pass the command as a string
'/usr/bin/python3 -u /home/pi/MyScript.py >> /home/pi/myScript.log' # Command with redirection
)
本文标签: Restart python thread with osexecl methodStack Overflow
版权声明:本文标题:Restart python thread with os.execl method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741875567a2402441.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论