Python cron job under windows7 - python

I have created a simple cron job under windows to run a very simple python scrape:
scrape webpage
store data into csv file
close file
Cron job under Windows worked fine-without any probs. All of sudden, that cron job stopped working, my output file is not getting updated. When I run the scrape manually (double click on python file or via python IDE) I would typically get my debug output "everything ok" in a windows dialog window and the target file gets updated. During the cron job I can see that debug output window popping up and printing the same "everything ok" debug, but the file is just not getting updated.
I tried rebooting my machine, close/open all program. Created a new task in the scheduler, it still wouldn't work. Any suggestions?
Thanks
Peter

After playing around with all potential settings in windows and many wasted hours I found a workaround.
My initial setup in the task manager was calling the python.exe and my python.py file was passed as an argument. Just as described here: http://blogs.esri.com/esri/arcgis/2013/07/30/scheduling-a-scrip/. The difference at the end that made my cron job work again was that I deleted passing the python file as an argument and now calling python file directly. Not sure why the initial setup stopped working - but this has helped. Hope you don't get into the same time wasting situation.

Related

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.

How to Resume Python Script After System Reboot?

I'm still new to writing scripts with Python and would really appreciate some guidance.
I'm wondering how to continue executing my Python script from where it left off after a system restart.
The script essentially alternates between restarting and executing a task for example: restart the system, open an application and execute a task, restart the system, open another application and execute another task, etc...
But the issue is that once the system restarts and logs back in, all applications shut down including the terminal so the script stops running and never executes the following task. The program shuts down early without an error so the logs are not really of much use. Is there any way to reopen the script and continue from where it left off or prevent applications from being closed during a reboot ? Any guidance on the issue would be appreciated.
Thanks!
Also, I'm using a Mac running High Sierra for reference.
You could write your current progress to a file just before you reboot and read said file on Programm start.
About the automatic restart of the script after reboot: you could have the script to put itself in the Autostart of your system and after everything is done remove itself from it.

using flask in spyder stuck in local server

I am trying to create a web application using flask. I have already gotten somewhat comfortable with using python, and have done so using spyder, inside of Anacanda Navigator. Now I am playing around with flask doing basic functions and have successful so far by testing it out in local server 127.0.0.1:5000. The problem I am having is that I cannot stop the server once I run the script in spyder. I have stopped the script and run other scripts through the console, but the local server remains the same.
The reason this is a problem for me is because when I try to change files and run a different flask script, the server does not update with the new information. For example, if I run a flask script that returns "Hello World" on the main page, and then I stop that file, open a new file that has a different flask script that returns "The sky is blue" the server does not change when I check it on chrome or any other browser. It will onyl return "Hello World"
I have been able to fix this problem by completely restarting my computer, but I am wondering if there is another way, just to restart the local server, 127.0.0.1:5000. Thank You!
Also I am using windows
I do : "Run > Configuration per file > Execute in an external system terminal",
then when you run your .py containing the app.run, it will be launched in an external console. If you close the console the server will be closed too.
To Kill the local server, you may use Ctrl+C command and not any other command. This command is also mentioned when the server is up and running.
I've been having this precise issue and have been smashing my head against the wall for a couple of hours. I posted the referenced StackOverflow question (my first actually) and it seems that running a script from inside Spyder is the wrong way to go as it leaves runaway background processes running, even after restarting Spyder.
I got the recommendation to only launch my *.py code from the command prompt. Furthermore I was told to do this:
set FLASK_APP=main1.py then set FLASK_DEBUG=1 then flask run
though I'm not sure what that does, so I will investigate. I was about to restart my computer as a last ditch effort until I looked in my Windows Task Manager and found some Python tasks running. After [end task] them both I was able to launch the updated webpage on my local host.

python script doesn't work properly under scheduler

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

python script via windows 7 scheduler still running

I'm a noobie to python and have over the past few days tested and created a .PY file that runs perfectly from the command prompt either with the D:\Python34 or just as
Its pretty basic. Gets info from web and places it in a spreadsheet - I import requests, BeautifulSoup and openpyxl. The last statement is to save the workbook. I'm figuring that Python closes itself as does the workbook.
I wish to run it daily. So I used the Windows Task Scheduler on my Windows 7 Ultimate. I have it running only when I am logged in.
I have tried the following:
1) using python directly with complete paths
2) using only the full path and the
3) using a batch file that does #1 above and has an EXIT
4) using a batch file that does #2 above and has an EXIT
When running the batch from the command file OR Task Scheduler, the command window opens, it does its thing and the command window closes.
The problem is when I run it via the Task Scheduler in any of the above ways, it does its thing, but continues to say "RUNNING" in the scheduler.
I have tried to run the Windows Process Monitor using some filters I found on the web, however nothing shows up.
Extremely frustrating --Any suggestions would be appreciated.
Jerry
While I had the Task Scheduler open, the screen was showing "running". After I closed the screen and let it run the next day, everything worked fine. It must be something to do with the refresh on the task scheduler.
So my solution was to close the Task Scheduler and let it run as scheduled.

Categories