Batch file logic failing - python

I'm trying to get a batch-file to execute the following code:
cmd /K C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
cd C:\ProgramData\Anaconda3\Lib\site-packages\tabpy_server\
run startup.bat
The first line execute properly and opens an anaconda python command-prompt window. The next lines fail to execute.
What am I missing?
The idea is to create a batch file which can be added to windows task-scheduler to start the tabpy server service.

There is no run command in batch. What you want is the start command. Also you want to use call instead of cmd /k because it is used for starting another instance of cmd while you just want to call the batch file. Here is an example:
call C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\Anaconda3
cd C:\ProgramData\Anaconda3\Lib\site-packages\tabpy_server\
start startup.bat

Related

Open cmd with Python Os.system and run commands inside the new opened cmd

I need to click in a button to call a python function (i did this), but now :
I need a python script to open a new cmd, and in the opened cmd i want to do 2 commands :
Cd and run a python file
This code is the best thing i could do but it's not running the script !
import os
username = os.getlogin()
os.system('start cmd /k ; cd C:\\Users\\' + username + '\\Desktop\\Automatisation & python serverSender.py')
To resume :
Start cmd /k (Open new cmd and remain)
cd C:\Users\' + username + '\Desktop\Automatisation (To change directory)
python serverSender.py (To run the python script inside Automatisation directory)
But the last command : python serverSender.py is not executing ! As you can see in the screen, the function opens a new cmd when i click on the button , it's goes to the directory in the cd command, but it's not starting the serverSender.py file !
Cmd Opened
Any idea on how to run the third command ? (i don't want to run it in another cmd, i want to run it in the opened cmd with the first command )
Thanks !
The & is interpreted by the outer shell and not the one inside start. You need to escape it, by changing & to ^&:
os.system('start cmd /k ; cd C:\\Users\\' + username + '\\Desktop\\Automatisation ^& python serverSender.py')
Having tried to check into this, I don't think it is a python issue.
I tried doing this, from a command line, for comparison:
start cmd /k cd .. & cd ..
It did not work. Only the first "cd .." got executed.
So it seems "&" is not a successful way to chain commands with "cmd /k". I checked && and that did not work either.
After further looking into it, there is one suggested workaround I found at the below link, which involves using a single line to temporarily write a script and execute that script. (though I did not try it)
https://superuser.com/questions/62850/execute-multiple-commands-with-1-line-in-windows-commandline

Why doesn't Crontab run the command inserted by python script?

I am trying to modify the crontab of the root user through a script of python (main.py).
This script will open the crontab for root user and will modify it inserting a new command to call single.py with a parameter. The command used is not the problem as I have run it from the command line and it works. When I run the main.py script I can see the new entry when I run the crontab -l command. Everything looks correct but it does not work when is the time (nothing has been executed).
Doing some checks, if I execute the crontab -e command and I insert the command manually; when I save and close it; it shows: **crontab: installing new crontab** and then it works.
main.py:
fname = "/var/spool/cron/crontabs/root"
f = open(fname,"w+")
f.write("24 19 16 01 * python /home/pi/single.py 2087111972\n")
f.close() # save and close cron file
BTW: As this script has to be recursive (it will call itself for rescheduling its execution by modifying the crontab), is there anyway to apply the changes on crontab through the script?

Python virtualenv / virtualenvwrapper workon xxx

Working a project with a development track that requires a 'workon xxx' to build the development track and a 'deactivate' to go back to the standard python to build the stable track. Using the Windows command line switching between python environment works great. However, when using 'workon xxx' from a batch script the it it doesn't return to execute the next line. It drops to the command line. I have tried 'call workon xxx' and the result is the same.
How can 'workon xxx' be called from a batch script and return to execute the next line?
call should work just fine:
c:\srv\tmp> cat workoncall.bat
#echo off
call workon dev
call cdsitepackages
echo %CD%
call workon pydeps
call cdsitepackages
echo %CD%
(pydeps) c:\srv\tmp> workoncall.bat
c:\srv\venv\dev\Lib\site-packages
C:\srv\venv\pydeps\Lib\site-packages

Running python scripts from a command line makes window pop and close immediately

I created a test file called pythonScript.py and I also created a batch file called pythonScript.bat. Both of these files are in my C:\Python Programs file that a created to store my scripts. The contents of pythonScript.bat is: #py.exe C:\Python Programs\pythonScript.py %*. When I press Ctrl+R and type pythonScript a command box pops up and then closes just as quick as it opened. I'm running Windows 10. Any advice?
You need to enclose the path to the Python script in pythonScript.bat within double-quotes:
#py.exe "C:\Python Programs\pythonScript.py" %*
This is because it contains a space. Without the "", py.exe receives two command line arguments C:Python and Programs\pythonScript.py.

Batch file runs with "not recognized...command", how to fix this?

I have a batch file which when executed sets PATHs, prompts user for input and loads a script via Python. The python script creates a grid with the size of each cell determined by the user input variable (cellsize). The following is from my .bat file:
#echo off
rem Root OSGEO4W home dir to the following directory
call "C:\OSGeo4W64\bin\o4w_env.bat"
rem List available o4w programs
rem but only if osgeo4w called without parameters
#echo on
set PYTHONPATH=C:\OSGeo4W64\apps\qgis\python
set PATH=C:\OSGeo4W64\apps\qgis\bin;%PATH%
#echo off
echo.
set /p cellsize="Enter cellsize: "
cellsize=1
cmd /k python "Script.py" %cellsize%
#echo on
The .bat works the way it's supposed to, I obtain the correct results, but I receive the following error:
'cellsize' is not recognized as an internal or external command, operable program or batch file
What simple mistake(s) did I make? I am a beginner but still learning.
The line must read:
set cellsize=1
but surely this line seems more useful before the set /p line as an initialization, since otherwise it cancels the effect of that line.
#echo off
echo.
set /p cellsize="Enter cellsize: "
set cellsize=1
cmd /k python "Script.py" %cellsize%
#echo on
you need set

Categories