Python Tutorials - Herong's Tutorial Examples - v2.14, by Herong Yang
Run Conda Environment Command in Background
This section provides a tutorial example on how to run Conda environment commands in background processes. file permissions and SELinux contexts must be changed on the Conda environment files and directories.
Conda environments are primarily designed to run on interactive terminals. If you want to run them in background processes, you may face multiple issues as shown in this tutorial.
1. Access the "conda" command - In a background process, the $PATH is not set properly to access the "conda" command. So you have to invoke it with the full path name. "/usr/local/anaconda3/condabin/conda" is the default location.
herong$ /usr/local/anaconda3/condabin/conda --version conda 4.10.3
2. Access the environment - Some background processes are running under special user names. So you need to give "read" and "execute" permissions to that user for the Conda environment directory.
For example, if you want to run a Python script in the "graph" Conda environment from the Apache web server, you have to give "read" and "execute" permissions to "apache" for the "/home/herong/anaconda3/envs/graph" directory.
herong$ chmod -R 777 /home/herong/anaconda3/envs/graph herong$ ls -l /home/herong/anaconda3/envs drwxrwxrwx. 3 herong it 24 Jul 30 11:09 graph
If "apache" as a user has no access to the Conda environment, you will get the "/home/herong/anaconda3/envs/graph/bin/python: Permission denied" error when calling system("/usr/local/anaconda3/bin/conda run -n graph python") from Apache Web server.
3. SELinux permission for executable files - If your system has SELinux turned on, you must make sure that the entire environment tree has the SELinux context of "unconfined_u:object_r:usr_t:s0".
herong$ ls -lZ /home/herong/miniconda3/envs drwxrwxrwx. 11 herong it unconfined_u:object_r:user_home_t:s0 173 Aug 23 12:14 graph herong$ sudo chcon -R -t usr_t /home/herong/miniconda3/envs/graph herong$ ls -lZ /home/herong/miniconda3/envs drwxrwxrwx. 11 herong it unconfined_u:object_r:usr_t:s0 173 Aug 23 12:14 graph
If SELinux context type is not set properly for executable files, you will get the "/home/herong/anaconda3/envs/graph/bin/python: Permission denied" error "/home/herong/anaconda3/envs/graph/lib/python: when calling system("/usr/local/anaconda3/bin/conda run -n graph python") from Apache Web server.
4. SELinux permission for create temporary files - If Conda wants to create temporary files at the environment root level, you must change the context type to "tmp_t" on the environment root directory only, not the entire tree.
herong$ sudo chcon -t tmp_t /home/herong/miniconda3/envs/graph herong$ ls -alZ /home/herong/miniconda3/envs/graph drwxrwsrwx. 14 herong it unconfined_u:object_r:tmp_t:s0 206 Sep 13 20:26 . drwxrwsrwx. 6 herong it unconfined_u:object_r:usr_t:s0 98 Sep 13 10:04 .. drwxrwsrwx. 2 herong it unconfined_u:object_r:usr_t:s0 8192 Sep 10 20:41 bin drwxrwsrwx. 21 herong it unconfined_u:object_r:usr_t:s0 24576 Sep 10 20:41 lib ...
If SELinux context type is not set properly, you will get the "/home/herong/anaconda3/envs/graph/bin/python: Permission denied" error when calling system("/usr/local/anaconda3/bin/conda run -n graph python") from Apache Web server.
Now you can run a Python script from a PHP script on the Apache Web server.
... $py = "/home/herong/nice-graph.py arg1 arg2"; system("/usr/local/anaconda3/bin/conda run -n graph python $py"); ...
5. Do not use stdin and stdout - When you are running a command in a Conda environment with "/usr/local/anaconda3/bin/conda run -n graph $cmd", Conda will not forward any data in stdin and stdout to and from the running command. So you have to exchange data with your command through files or database tables.
Table of Contents
Variables, Operations and Expressions
Function Statement and Function Call
Iterators, Generators and List Comprehensions
Packages and Package Directories
"pathlib" - Object-Oriented Filesystem Paths
"pip" - Package Installer for Python
SciPy.org - Python Libraries for Science
pandas - Data Analysis and Manipulation
►Anaconda - Python Environment Manager
Introduction to Conda Environment
Share Conda Environment with Others
Manage Packages in Conda Environment
►Run Conda Environment Command in Background