Start 2 VENVs and 2 Python programs from 1 Batch file - python

I have 2 python programs that act as 'services', each of them runs from its own venv. I'm looking to create a batch file to initiate both services, each on their own dedicated terminal.
I was able to create 1 batch file to initiate the venv and the service, but I cant merge them into one batch file.
CALL <PATH_TO_VENV_SCRIPTS>\activate
"<PATH_TO_PYTHON_FILE>main.py"
pause
When I run the file above, a terminal opens, activates the venv, and runs the main.py using the venv.
Now I'm trying to have 1 batch to start both services. I have the following:
START <PATH_TO_VENV1_ACTIVATE.BAT>
"<PATH_TO_SERVICE1_PYTHON_FILE>main.py"
START <PATH_TO_VENV2_ACTIVATE.BAT>
"<PATH_TO_SERVICE2_PYTHON_FILE>main.py"
The file above indeed opens 2 terminals, and activates both VENVs, but unfortunatelly the python files are initiated from a third terminal which opens before the aforementioned 2.
How can I get the python files to be initiated from their own VENV terminal?
Any help is greatly appreciated.
Thanks in advance!

You have to launch your python files as a command in your newly created venv terminal. If you start the python service from the parent script, they're executed in this terminal context.
In other words: You have to include the execution of the scripts into your PATH_TO_VENV1_ACTIVATE.BAT. However, if your python script does not terminate after execution, you won't ever get there and the "parent" terminal will hang on the first script. So make sure that the script runs in a way which does not block the parent calling context.
HTH
EDIT: If your scripts are starting a service anyway, consider creating a container and launch these from there. Therefore you have an encapsulated context paired with easy execution.

Related

How to schedule a python script to run from virtual environment via task scheduler

I stumbled on an answer for basically the exact same question, but either I am implementing the solution wrong, the solution is no longer accurate, or some other issue is at play.
Run a python script in virtual environment from windows task scheduler
I used the code from above link, edited for my directories, and copied directly into the task scheduler program/script prompt.
C:\Users\I511843\myenv\activate.bat && python C:\Users\I511843\Desktop\Atom_scripts\test.py
I then set the program to run every time I unlocked the machine. The program simply writes a text file called test.txt containing the text "success!"
The text file populates in the correct directory when I run from the command line, however, the program doe snot execute from the task scheduler.
Did you try putting the commands in a batch file and executing the batch file from task scheduler? Something like below:
C:\Users\I511843\myenv\activate.bat && python C:\Users\I511843\Desktop\Atom_scripts\test.py goes into task.bat file.
In task scheduler, run task.bat from cmd.

How do I activate python anaconda and run script from regular command prompt in Windows 10

I am trying to create an automated job to navigate to a network share and from there to run a Python script but permissions are a little wonky. If I start python first, it can't find the share. However, I found that if I start an Anaconda prompt first, it can navigate to the share and execute no problem. Here is the sequence of commands in a batch file:
z:
CD Python
python ImportTool.py LocalCommands.txt
Unless there is a way to call an anaconda prompt directly from Task Scheduler, I figured I would need to add a line to the batch file to convert the command prompt to an Anaconda prompt. This works:
C:\ProgramData\Anaconda3\Scripts\activate.bat
The problem is once this runs, it ends the batch so the other commands don't run. From the new command prompt if I call the remaining lines in another batch file it works but that means a second call. This has to work in a single batch as a schedule job.
So my question is, how can I modify my batch file to get my anaconda prompt and still be able to run the remaining commands without a second call? TIA
I dug through the various calls. For some reason calling the conda.exe app is what throws off the batch execution. It isn't necessary to call, however, if you add all the necessary paths for anaconda. My final batch file looks like this:
SET PATH=C:\ProgramData\Anaconda3;C:\ProgramData\Anaconda3\Library\mingw-w64\bin;C:\ProgramData\Anaconda3\Library\usr\bin;C:\ProgramData\Anaconda3\Library\bin;C:\ProgramData\Anaconda3\Scripts;C:\ProgramData\Anaconda3\bin;C:\ProgramData\Anaconda3\condabin;%PATH%
z:
CD Python
python ImportTool.py LocalCommands.txt
That does the job

Unexpected behaviour when running python from crontab

I have a python script (script A) that runs multiple sub-processes (running another python script - script B). When I run the script manually, it works fine, but when it is being run using crontab it behaves differently.
To be more specific, my script is limited to 100TB of quota, so script B runs over a tree and produce a text file of size-per-dir. This script is being run multiple times on several dir-trees as a subprocess, and the results are being further analyzed by script A.
When I run manually, both scripts run fine. But when I run it using Cron, script A runs fine but script B produce odd resutls.
Any ideas of why using Cron would affect a python script?
Make sure the crontab you are using is with full path of the .sh file.
And in beginning of your script try to add the following:
. /home/gemapp/.bash_profile
to load all needed items in bash_profile.
The default directory for any scheduled job is user's home dir, not the location of the shell script/program itself.
To mimic the crontab job, you must simulate running your script from user's home dir using "cd ~" command. Run your job using the exact same syntax as in your "crontab -l" output to simulate the error.
In other word, you must simulate running your schedule job from home dir. Once it worked, then you put it into the crontab.

Windows task scheduler not running python script/batch file

I have a very simple python script (to send an email) which works perfectly when I run it in the cmd window, or in python, or if I directly start a a .bat file pointing to it.
However, when I try to get the task scheduler to run it, nothing happens. The task scheduler says that it runs and completes successfully, the log file is blank, but no email is sent.
I'm aware there are a lot of other questions relating to this problem, and I've read through them and tried the solutions, but nothing seems to work. I am new to python (and to scheduling tasks!), so I may be implementing the solutions incorrectly.
Here is what I've tried...
Creating a batch file with the script in it in various ways:
python C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt
python "C:\Users\me\Documents\etc\script.py >C:\Users\me\Documents\etc\log.txt"
C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe C:\Users\me\Documents\etc\script.py
"C:\Users\me\AppData\Local\Programs\Python\Python35-32\python.exe" "C:\Users\me\Documents\etc\script.py"
C:\Users\me\Documents\etc\script.py
All of these work fine when double clicking on the file - but none run in task manager. (Although they say they have completed successfully). In task manager I just put the link to the .bat file in the "Program/script" box.
Doing the above but with the full path to the cmd.exe in the program/script, and the .bat file as an argument. I've also tried putting the location of the bat file in the "Start in (optional):"
Trying to run the .py file directly via the task scheduler, putting it in the "Program/script:" box
Trying to run the .py file directly via the task scheduler, putting the full path to the python.exe (see above) in the "Program/script", and the sript.py in the "Add arguments". I've also tried this with path to the location of the script in "Start in".
Trying to run the .py file via the cmd - so putting the full path to the cmd.exe in "Program/script" and the script.py file (full path) in the "Arguments".
Some of the ones where I try to run the script.py directly just say "running" in the Task Scheduler forever, but I didn't note down which these were.
I'm running Windows 7 (64bit), and have got Python 3.5.1 (32bit). I've got local admin rights.
Other scheduled tasks I have created (not involving a python script) work fine, but this has stumped me. Please help!
Just adding an answer in case this affects any other newbie :). I needed to check "Run only when user is logged on", and also uncheck "Run with highest privileges".
I'm guess this is because as eryksun says Outlook has a GUI.
Something else to try:
Make sure in your batch file you're adding a command to change directories to where your executable lives.
#echo off
echo.------------------------------------------------
echo.Windows Task invoked on %date%, %time% (local time)
echo.------------------------------------------------
SET My_exe_dir="C:\Program Files (x86)\MyProgram\FolderWhereExeLives"
SET Input_dir="C:\Program Files (x86)\MyInputFolder"
cd %My_exe_dir% <-- This was the key for me.
%My_exe_dir%\myprogram.exe %Input_dir%\MyInputFile.xml -1
For me, specifying the "start in" directory worked:
Define Start in directory in properties of scheduled task.
Start Windows Task Scheduler.
Navigate to the task and double click on it to open the Properties of the task.
Select tab Action and click on button Edit.
There is Start in (optional). Enter here the path of the executed batch file.
Click two times on button OK to save this important modification in properties.

python multithreading issue in cronjob

I have a python program that uses the ThreadPool for multithreading. The program is one step in a shell script. When I execute the shell script manually on the command line, the entire flow works as expected. However, when I execute the shell script as a cronjob, it appears that the flow goes to the next steps before the python multithreading steps are completely finished.
Inside the python program, I do call AsyncResult.get(timeout) to wait for all the results to come back before moving on.
Run your program via batch(1) (see the output of the command man batch) as well. If that works OK, but the cron version does not, then it is almost certainly a problem with your environment variable setup. To verify that, run printenv from your interactive shell to inspect your environment there. Then do the same thing inside the crontab (you will just need to temporarily set up an extra cron entry for it). Try setting the variables in your shell script before invoking Python.
On the other hand, if it doesn't work via batch(1) either, it could be something to do with the files that your code has open. Try running your shell script with input redirected from /dev/null and output going to a file:
$ /usr/local/bin/myscript </dev/null >|/tmp/outfile.txt 2>&1
Try setting "TERM=xterm" (or whatever env variable you have, figure out by command 'env' on your terminal) in your crontab.

Categories