[英]How to run python script in background using Anaconda? ('nohup python -u test.py &' doesn't work!)
I have a simple python script test.py :我有一个简单的 python 脚本test.py :
import time
import logging
logging.basicConfig(filename='app.log', filemode='w', level=logging.DEBUG)
i=0
while i<100:
i+=1
logging.info(i)
print(i)
time.sleep(1)
I want to run this script in background using anaconda. I tried: nohup python -u test.py &
.我想使用 anaconda 在后台运行这个脚本。我试过:
nohup python -u test.py &
。 python keyword invokes anaconda on my machine. python关键字在我的机器上调用 anaconda。 It seems that script is still linked to the terminal I used to run it.
似乎脚本仍然链接到我用来运行它的终端。 If I close the terminal, the execution stops and if I use 'exit' to close the terminal, the terminal turns black but doesn't close.
如果我关闭终端,执行将停止,如果我使用“退出”关闭终端,终端会变黑但不会关闭。 If I close using 'X', the execution stops.
如果我使用“X”关闭,则执行停止。
What is the correct way to trigger a python script to run on anaconda in background?触发 python 脚本在后台运行 anaconda 的正确方法是什么?
$ conda info
active environment : None
conda version : 4.9.2
conda-build version : 3.20.5
python version : 3.8.5.final.0
virtual packages : __win=0=0
__archspec=1=x86_64
base environment : F:\Automation\Anaconda3 (read only)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
platform : win-64
user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.17763
administrator : False
netrc file : None
offline mode : False
Terminal used to run script: Git , version: 2.29.2.windows.3用于运行脚本的终端: Git ,版本: 2.29.2.windows.3
Use the python executable of your conda environment.使用您的 conda 环境的 python 可执行文件。
nohup <base_environment_path>/envs/<env-name>/bin/python <script_name>.py
eg例如
nohup /home/ubuntu/anaconda3/envs/my-env/bin/python test.py > output.txt &
You will need to know the path to the python executable in the environment you want to execute the code in.您将需要知道要在其中执行代码的环境中的 python 可执行文件的路径。
You can find that by running你可以通过运行找到
conda info
康达信息
in your desired environment.在您想要的环境中。 When you find the location of environment you just have to to do this:
当您找到环境的位置时,您只需执行以下操作:
nohup <absolute path to your anaconda environment>/bin/python <YOUR SCRIPT> > output.txt &
If you don't want to use conda info to find it you can just execute this如果你不想使用 conda info 来找到它,你可以执行这个
nohup <absolute path to your anaconda>/anaconda3/envs/<your environment>/bin/python <YOUR SCRIPT> > output.txt &
I have a simple python script test.py :我有一个简单的 python 脚本test.py :
import time
import logging
logging.basicConfig(filename='app.log', filemode='w', level=logging.DEBUG)
i=0
while i<100:
i+=1
logging.info(i)
print(i)
time.sleep(1)
I want to run this script in background using anaconda.我想使用 anaconda 在后台运行此脚本。 I tried:
nohup python -u test.py &
.我试过:
nohup python -u test.py &
。 python keyword invokes anaconda on my machine. python关键字在我的机器上调用 anaconda。 It seems that script is still linked to the terminal I used to run it.
似乎该脚本仍然链接到我用来运行它的终端。 If I close the terminal, the execution stops and if I use 'exit' to close the terminal, the terminal turns black but doesn't close.
如果我关闭终端,执行将停止,如果我使用“退出”关闭终端,终端会变黑但不会关闭。 If I close using 'X', the execution stops.
如果我使用“X”关闭,则执行停止。
What is the correct way to trigger a python script to run on anaconda in background?触发 python 脚本在后台在 anaconda 上运行的正确方法是什么?
$ conda info
active environment : None
conda version : 4.9.2
conda-build version : 3.20.5
python version : 3.8.5.final.0
virtual packages : __win=0=0
__archspec=1=x86_64
base environment : F:\Automation\Anaconda3 (read only)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
platform : win-64
user-agent : conda/4.9.2 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.17763
administrator : False
netrc file : None
offline mode : False
Terminal used to run script: Git , version: 2.29.2.windows.3用于运行脚本的终端: Git ,版本: 2.29.2.windows.3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.