Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems running python script by windows task scheduler that does pscp

Not sure if anyone has run into this, but I'll take suggestions for troubleshooting and/or alternative methods.

I have a Windows 2008 server on which I am running several scheduled tasks. One of those tasks is a python script that uses pscp to log into a linux box, checks for new files and if there is anything new, copies them down to a local directory on the C: drive. I've put some logging into the script at key points as well and I'm using logging.basicConfig(level=DEBUG).

I built the command using a variable, command = 'pscp -pw xxxx name@ip:/ c:\local_dir' and then I use subprocess.call(command) to execute the command.

Now here's the weird part. If I run the script manually from the command line, it works fine. New files are downloaded and processed. However, if the Task Scheduler runs the script, no new files are downloaded. The script is running under the same user, but yet yields different results.

According to the log files created by the script and on the linux box, the script successfully logs into the linux box. However, no files are downloaded despite there being new files. Again, when I run it via the command line, files are downloaded.

Any ideas? suggestions, alternative methods?

Thanks.

like image 262
user1070061 Avatar asked Nov 25 '13 14:11

user1070061


People also ask

Can Task Scheduler run a Python script?

Yes, you can execute a Python script with Windows Task Scheduler. If your script works using the command prompt, you can schedule your script to run at a specific time and date.

How do I make a Python script run automatically daily?

Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…' Type a name for your task (you can also type a description if needed), and then press Next. For instance, let's name the task as: Run Hello World. Choose to start the task 'Daily' since we wish to run the Python script daily at 6am.


3 Answers

I had the same issue when trying to open an MS Access database on a Linux VM. Running the script at the Windows 7 command prompt worked but running it in Task Scheduler didn't. With Task Scheduler it would find the database and verify it existed but wouldn't return the tables within it.

The solution was to have Task Scheduler run cmd as the Program/Script with the arguments /c python C:\path\to\script.py (under Add arguments (optional)).

I can't tell you why this works but it solved my problem.

like image 187
Brad Posthumus Avatar answered Oct 05 '22 08:10

Brad Posthumus


You can use the windows Task Scheduler, but make sure the "optional" field "Start In" is filled in.

In the Task Scheduler app, add an action that specifies your python file to run "doSomeWork" and fill in the Start in (optional) input with the directory that contains the file.. So for example if you have a python file in:

C:\pythonProject\doSomeWork.py

You would enter:

Program/Script: doSomeWork.py

Start in (optional): C:\pythonProject 
like image 22
Erkin Djindjiev Avatar answered Oct 05 '22 08:10

Erkin Djindjiev


I'm having a similar issue. In testing I found that any type of call with subprocess stops the python script when run in task scheduler but works fine when run on the command line.

import subprocess

print('Start')
test = subprocess.check_output(["dir"], shell=True)
print('First call finished')

When run on command line this outputs:

Start
First call finished

When run from task scheduler the output is:

Start

In order to get the output from task scheduler I run the python script from a batch file as follows:

python test.py >> log.txt

I run the script through the batch file both on command line and through task scheduler.

like image 27
Steve Avatar answered Oct 05 '22 08:10

Steve