python script doesn't work properly under scheduler - python

I have a program on Windows Server 2012 that:
-reads sql query from text file in the same location
-imports helper functions from file in the same location
-executes query on sql server (in the same network) and saves the results
-creates a google spreadsheet from the results (using API credencials that are in the same location)
When I log in to tthe server and execute the file in cmd: python myscript.py everything is fine. However when I try to do the same from Task Scheduler it fails. I get 0x1 error.
This is what I put in my Scheduler actions:
program/script - quoted full path to python.exe (which is in Anaconda folder)
Arguments - quoted full path to myscript.py
Start in - blank
I have tried running it as myself, SYSTEM, Administrators. Also tried Highest priveleges and user logged on or not options... Also followed another solution on SO that recommended running cmd and then "/c python full/path/to/myscript.py" But it's always the same.
It´s very frustrating. I realize it's not strictly coding related issue but I am sure many python programmers had it.

Have you got the same error when starting non-python tasks with scheduler?
If not - I would try to install clear Python and create new schedule .bat file with
powershell C:\Python27\python.exe C:\Python27\file.py
pause

Related

Automation of Powershell script, that invokes a Python script, doesn't work (via Task Scheduler)

I am trying to create a bunch of automations on my PC with Windows, and I encountered some obstacles while trying to automate a Powershell script with Windows Task Scheduler.
Right now, I have managed to set up a Task Scheduler to perform an actual script, but despite the whole script working as intended when I start it manually, it doesn't work right while invoked via task scheduler.
The Powershell script is very simple, it is meant to invoke a certain Python script. I also created a log transcript for testing purposes.
python dataset_creator.py --to_import yes --to_export yes
Start-Transcript -Path "<path>\transcript0.txt"
While invoked manually, the Python script works, but via Task Scheduler, the only working part is a transcript creation (ergo - no Python script is running). The Task Scheduler informs me itself that the task executed properly. This is how I set up the action:
Program: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File "C:\<path>\powershell_script.ps1"
As for now, I have set it on a SYSTEM account, I have tried to set it on my user admin account, but it doesn't work as well.
Could you suggest the potential issues? I scrolled through several articles and nothing works. I also tried to skip the Powershell part (aka - set up the task relying only on a Python task), but it didn't work so far, and I also initially wanted to insert a number of Python scripts invokations into a single Powershell file.
I am also open to some Windows Task Scheduler alternatives suggestions.

Running Python script as Executable

I have been attempting to run my python script as a service, and have followed the advice contained in several previous forum posts. However, these have not helped me thus far. Here is what I have attempted up until now:
Used an SMWinservice class that allowed me to install my Python script as a service. This solution, however, would not launch. If I would try to start it, I would receive an error message.
I have tried using NSSM with : nssm install myService pathToInterpreter PathToScript. Note, all files etc. can be found in the local directory of the PathToScript. For what its worth, I also tried using the GUI version of NSSM. When I rebooted my computer, it showed as "Paused" in task manager. I stopped it, and then tried to run it again, and I received an error. I have tried NSSM with both python.exe and pythonw.exe.
I suspect that a possible source of error is the fact that my program uses a text file as a configuration file. This configuration file has been coded as being in my local working directory. However, I wouldn't think this would be an issue with NSSM. I know for a fact that my program will completely exit, using the exit command, if the configuration file is not found.
I was thinking of doing a batch file and starting the batch file with the script and running it like that, but I prefer a service since it can monitor the process, can restart it, or windows can notify me via email if there is an issue with my service.
For completeness, I should also mention that the program runs without issue outside of a service. For the reason that the program runs as expected, I decided to not post the code, unless someone would like to see it for whatever reason.

Python Won't Run Even Through It Is In the Path

I have a SQL Server Agent job that executes some python scripts using CmdExec. Everything is set up with a proxy account as expected.
When I run the job I get:
Message
Executed as user: domain\proxyaccount. 'python' is not recognized as an internal or external command, operable program or batch file. Process Exit Code 1. The step failed.
I'm using Anaconda and Python is in the system PATH variable. When I run python from command line, it works. When I run python cutting and pasting the specific command from the job, it works. When I use runas to mimic the proxy account it works. The only place Python doesn't run is form inside the job.
What else do I need to look at to trouble shoot this issue?
You should restart SQL Server Agent after you installed Python on the server.
It is necessary for SQL Server Agent to load new environment variables, including the updated PATH with Python in it.
There are also suggestions to restart SQL Server too, but I believe restarting SQL Server Agent will be enough.

Executing complex task for python script using bash

I launch python script in bash:
python /dumpgenerator.py --index=http://*website* --xml --curonly --images --path=/*website*
In order to perform many tasks the script has to be launched in new windows with different parameters, I mean website
Can I launch at the same time python script which will "catch" parameters using bash commands from text file which contains website links? It's important that sessions have to be launched in new console windows (for every session there will be own bash and python processes)
There's also a problem to convert website link into applicable filesystem format when setting --path=/website. What regular expression should I use?
Example: the script is developed by https://code.google.com/p/wikiteam/. It doesn't let you to launch more than one wikis to archive them simultaneously. If you want more wikis to be archived, you have to copy paste (just change one parameter website) command in a new bash session.
It seems rather boring to open new terminal window and bash session 50 times. That's why I'm concerned how can I simplify this task.
I've found a solution. I just set up TMUX by this article and ran copies of python /dumpgenerator.py

Reading a Google Calender using a Cron-activated python script

being new with python on a raspberry pi, I downloaded the sample for accessing a google calender given here: https://developers.google.com/api-client-library/python/ and made it run. Just renamed the original file and wrote some code around it. Script works fine when launched from the command line.
But when calling the script hourly via cron, an additional (or new) authentication is required: I'm told to copy a link to the browser, get the 'success code' and copy this into the raw input line the script is intended to show me. The problem is, that this message is sent to my postbox by cron via email and the script is stopped. So I don't have the chance to enter the 'success code' and have it authenticated.
Any ideas about how to allow the cron-activated script reading my calender?
SOLVED!
I added several own log-commands to the scripts in order to trace 'Where am I and what values do my variables have'. After running through the scripts manually via command line and automatically via cron, I compared these logs and found out that when started by cron, several files couldn't be opened. The file names were given without any path, so they were expected to be in the path I called the first script from via commandline. When launched by cron, these files could not be opened, even though I added the relevant paths to PATH= and PYTHONPATH=
So what helped: try using absolute (and full) paths to any files you want to access.

Categories