I wrote a simple "git pull" script to be run on a Windows Server:
import logging
import subprocess
# Initialize logging
logger = logging.getLogger('autopull')
logger.setLevel(logging.INFO)
log_file = logging.FileHandler('autopull.log')
log_file.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(message)s")
log_file.setFormatter(formatter)
logger.addHandler(log_file)
# Main part
logger.info('Started')
try:
subprocess.check_output("C:\Program Files\Git\cmd\git pull")
logger.info('Pulled repo successfully')
except:
logger.exception("Pulled failed")
I used a logger because the scheduler always told me that it executes with exit code 0 and I wanted to double check whether it was the case.
When I call the script by hand, it is working fine and the logger writes in the autopull.log. However, when it is called by the scheduler, the logger writes nothing. The task runs with the "Run with highest privileges" and "Run whether user is logged or not".
Is there something I did wrong ?
Edit 1 : Added Scheduled Task parameters
General :
"Run whether user is logged on or not" and "Run with highest privileges".
Configure for : Windows Server 2012 R2
Triggers:
Trigger : One time
Details : At 12:30 PM on 6/26/2017 - After triggered, repeat every 1 hour indefinitely
Status : Enabled
Actions:
Action : Start a program
Details : D:\Anaconda3\python.exe D:\projects\foo\autopull.py
Conditions:
"Start the task only if the computer is on AC power" and "Stop if the computer switched to battery power".
Settings:
"Allow task to be run on demand" and "if the running task does not end when requested, force it to stop".
#Dr Mouse, try this. I was able to run your code (while logged off). FYI, I am running Anaconda on Windows as well.
Once in the Edit Action window (in your actions, that you showed above, click on the edit button), in the program/script, be sure that it is pointing to the actual instance of your Python (Anaconda).
Put double quotes around the Program/Script
"D:\Anaconda3\python.exe"
Do not copy and paste, type it in.
In the add arguments optional:
-u "D:\projects\foo\autopull.py"
In Start in (optional) add the folder location to the script without quotes:
D:\projects\foo
If the above doesn't work, for troubleshooting purposes:
Does it work when you click on run (which runs it immediately) in task scheduler (naturally while logged in)?
In task scheduler, did you look through the history of the task to see where the scheduler is failing?
EDIT: Thinking more about it, I remember there was some issue with running python directly using the "Run whether user is logged on or not".
If the above does not work, try this:
create batch file (.bat) and add this line in it:
D:\Anaconda3\python.exe D:\projects\foo\autopull.py %*
point the task scheduler to this batch file (instead of pointing it to the python script directly).
Related
I'm currently working on a task to send information from a handheld via a Windows Server to a printer.
For this I use a batch-file that is starting a python-file.
title "RestApi"
call D:\ProgramData\Miniconda3\Scripts\activate.bat
start "Rest" D:\ProgramData\Miniconda3\python.exe D:\path\Rest.py
in the py-file there is a part included to open a picture in paint to print it onto envelope #9 paper.
cmd = f'mspaint /pt ' + r'card.png ' + '"' + printer + '"'
print(cmd)
p = subprocess.Popen(cmd, shell=True)
And here there is the problem. If I manually start the batch-file when logged in on the server as admin, everything is fine and the card is printed as expected.
However, if I automate the task via Windows task scheduler there is another print size given. Means, not all of the information is visible anymore.
Task is running with "SYSTEM" account
Start parameters for the script
Does anybody has an idea why this failure happens and how to solve it?
I would really appreciate your help!
Thanks and best regards
Chris
Already tried to directly execute the py-file by opening python.exe in task scheduler combined with argument (py-file) and started in the path where the py-file is stored.
Seems, it doesn't work :-(
I havn't done any admin on windows for a long time, but in the past years there was an option in the task scheduler for running the task as another user (a desktop user)
my guess here is that you indeed run the scheduled task under the system account (a special builtin account) that does not have all the environment and access necessary for your use case (which is inded a desktop task). So ticking this option and select your account (or a service account created for the script) can help.
I have a task to write a Python script which has to parse a web-page once a week. I wrote the script but do not know how can I make it to work once a week. Could someone share an advice and write possible solution?
Have a look at cron. Its not python, but fits the job much better in my opinion. For example:
#weekly python path/to/your/script
A similar question was discussed here.
Whether the script itself should repeat a task periodically usually depends on how frequently the task should repeat. Once a week is usually better left to a scheduling tool like cron or at.
However, a simple method inside the script is to wrap your main logic in a loop that sleeps until the next desired starting time, then let the script run continuously. (Note that a script cannot reliably restart itself, or showing how to do so is beyond the scope of this question. Prefer an external solution.)
Instead of
def main():
...
if __name__ == '__main__':
main()
use
import tim
one_week = 7 * 24 * 3600 # Seconds in a week
def main():
...
if __name__ == '__main__':
while True:
start = time.time()
main()
stop = time.time()
elapsed = stop - start
time.sleep(one_week - elapsed)
Are you planning to run it locally? Are you working with a virtual environment?
Task scheduler option
If you are running it locally, you can use Task scheduler from Windows. Setting up the task can be a bit tricky I found, so here is an overview:
Open Task Scheduler > Create Task (on right actions menu)
In tab "General" name the task
In tab "Triggers" define your triggers (i.e. when you want to schedule the tasks)
In tab "Actions" press on new > Start a program. Under Program/script point to the location (full path) of your python executable (python.exe). If you are working with a virtual environment it is typically in venv\Scripts\python.exe. The full path would be C:\your_workspace_folder\venv\Scripts\python.exe. Otherwise it will be most likely in your Program Files.
Within the same tab, under Add arguments, enter the full path to your python script. For instance: "C:\your_workspace_folder\main.py" (note that you need the ").
Press Ok and save your task.
Debugging
To test if your schedule works you could right click on the task in Task scheduler and press on Run. However, then you don't see the logs of what is happening. I recommend therefore to open a terminal (eg cmd) and type the following:
C:\your_workspace_folder\venv\Scripts\python.exe "C:\your_workspace_folder\main.py"
This allows you to see the full trace of your code and if its running properly. Typical errors that occur are related to file paths (eg if you are not using the full path but a relative path).
Sleeping mode
It can happen that some of the tasks do not run because you don't have administrator privileges and your computer goes in sleeping mode. What I found as a workaround is to keep the computer from going into sleeping mode using a .vbs script. Simply open notepad and create a new file named idle.vbs (extension should be .vbs so make sure you select all programs). In there paste the following code:
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
Do While True
objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}")
Wscript.Sleep (60000)
Loop
I'd like to schedule a python script to run weekly under Windows 7.
If I chooses "run only when user is logged on" and manually starts the task, the command window for the script would show up and it would work as expected.
If I chooses "run whether user is logged on or not" (and inputs the neccessary user information) and manully starts the task, nothing happens and it looks like the script didn't do anything. Also, the running result would be 0x1.
Any suggestion would be appreciated.
ps. "c:\python27\python.exe" is used as the command line and path_to_script.py as arguments. Also, I've specified "c:\python27" in the "start in".
Update 05/02/2017: The cause to the problem is said in Map a network drive to be used by a service, which means I shall not access network mapped drive directly in this mode.
We're running a Python script (which uses multithreading) to do some work on an Amazon-EC2 based Windows Server 2008 machine. When the machine starts, I can see that it starts executing the Python script, and then I start seeing messages like the following in the event log:
Windows detected your registry file is still in use by other applications or services. The file will be unloaded now. The applications or services that hold your registry file may not function properly afterwards.
DETAIL -
19 user registry handles leaked from \Registry\User\S-1-5-21-2812493808-1934077838-3320662659-500_Classes:
Process 2872 (\Device\HarddiskVolume1\Python27\python.exe) has opened key \REGISTRY\USER\S-1-5-21-2812493808-1934077838-3320662659-500_CLASSES
Process 2844 (\Device\HarddiskVolume1\Python27\python.exe) has opened key \REGISTRY\USER\S-1-5-21-2812493808-1934077838-3320662659-500_CLASSES
Process 2408 (\Device\HarddiskVolume1\Python27\python.exe) has opened key \REGISTRY\USER\S-1-5-21-2812493808-1934077838-3320662659-500_CLASSES
What exactly does this mean, and how do I stop Windows from killing some of the threads?
When a scheduled task is configured to run as a particular user, that user's account is logged on non-interactively in order to run the task. When the task is finished, the user's registry hive is unloaded. For some reason, this is happening prematurely.
From your description, you have a single scheduled task, which launches various subprocesses. It seems likely that the parent process is exiting before the subprocesses are finished, and that this is causing the user's registry hive to be unloaded. You can verify this theory by turning on auditing for process creation and termination (in Group Policy under Advanced Audit Policy Configuration) or by using a tool such as Process Monitor (available from the MS website).
Assuming this is the cause, the fix is for the parent process to wait for the subprocesses to exit before itself exiting; alternatively, depending on your circumstances, it may be sensible for the parent task to simply never exit.
If you don't have direct control over the relationship between the parent process and the subprocesses then you'll need to create a new parent process to launch the script for you, and then either wait for all subprocesses to complete or sleep forever, as appropriate.
It may be that some your files are corrupted. Try the following:
Perform SFC(System file Checker) scan and see if it helps.
Press Windows key + X.
Select Command Prompt(Admin).
Type sfc /scannow and hit enter.
Also perform a chkdsk:
Press Windows Logo + C to open the Charms bar.
Now click Settings and then More PC Settings.
Now click General and then click Restart Now under Advanced Startup.
Now Click Troubleshoot.
Now click Advanced options and select Command prompt.
Type chkdsk /r and hit enter.
Last but not least, if the above doesn't work, you can perform a startup repair:
Press Windows logo + W to open the search box.
Type Advanced Startup options, hit enter.
Then Click Restart Now under Advanced Startup.
Now Click Troubleshoot.
Then click Advanced options and then Automatic Repair.
Hope it helps.
I have written a python script and wanted to have it run at a set period everyday with the use of Task Scheduler. I have had no problems with Task Scheduler for running programs while logged off, before creating this task.
If I select "Run only when user is logged on" my script runs as expected with the desired result and no error code (0x0).
If I select "Run whether user is logged on or not" with "Run with highest privileges" and then leave it overnight or log off to test it, it does not do anything and has an error code of 0x1.
I have the action to "Start a program" with the Details as follows:
Program/script: C:\Python27\python2.7.exe
Add arguments: "C:\Users\me\Desktop\test.py"
I think it has to do with permissions to use python while logged off but I can't figure this one out. Wondering if anyone has suggestions or experience on this.
This is on Windows 7 (fyi)
Thanks,
JP
I think I have found the solution to this problem. My script is used to create a powerpoint slide deck and needs to open MS PPT.
I stumbled upon a post from another forum with a link to MS's policy on this. It basically boils down to the following:
"Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behaviour and/or deadlock when Office is run in this environment.
Automating PowerPoint from a scheduled task falls under the unsupported scenario when scheduled task is run with the option "Run whether user logged on or not". But, using it with "Run only when the user is logged on" option falls under the supported category."
From here
I would try it with the script not in your Users directory
I have experience supporting PowerPoint automation under the Task Scheduler by way of a C++ app called p3icli (available on sourceforge). This is the approach I successfully used:
1) Add a command-line (-T) switch that indicates p3icli will run under Task Scheduler.
2) The command-line switch forces p3icli to start an instance of powerpnt.exe using CreateProcess() and then wait X milliseconds for that instance to stabilize.
3) After X milliseconds elapse, p3icli connects to the running PPT instance created in step 2 and processes automation commands.
I would guess that a similar approach can be used with Python.
Task Scheduler compatibility is easily the most troublesome feature I ever added to p3icli. For example, manipulating multiple presentations by changing the active window simply does not work. And as I'm sure you've discovered, debugging problems is no fun at all.
NB: Your python solution must include code that forces PowerPoint to unconditionally close when your python script is complete (modulo a python crash). Otherwise, orphaned instances of PowerPoint will appear in Task Manager.
Click the link for some thoughts on the Task Scheduler from a p3icli point of view.