简体   繁体   中英

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 :

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 & . python keyword invokes anaconda on my machine. 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.

What is the correct way to trigger a python script to run on anaconda in background?

$ 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

Use the python executable of your conda environment.

  1. run conda info to get the path to the base environment:<base_environment_path>
  2. to use specific env, use its name and then access the python bin executable:
  3. 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.

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

nohup <absolute path to your anaconda>/anaconda3/envs/<your environment>/bin/python <YOUR SCRIPT> > output.txt &

I have a simple python script 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 & . python keyword invokes anaconda on my machine. 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.

What is the correct way to trigger a python script to run on anaconda in background?

$ 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM