Problems running python script using windows task scheduler that runs pscp

Not sure if anyone came across this, but I will offer troubleshooting tips and / or alternative methods.

I have a Windows 2008 server on which I run several scheduled tasks. One of these tasks is a python script that uses pscp to enter the linux window, check for new files and, if there is anything new, copy them to a local directory on the C: drive. I put some entries in the script at key points, and I use logging.basicConfig(level=DEBUG) .

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

Now here is the weird part. If I run the script manually from the command line, it works fine. New files are uploaded and processed. However, if the task scheduler runs the script, new files are not loaded. The script runs under the same user, but it gives different results.

According to the log files created with the script and in the linux window, the script successfully enters the linux window. However, the files do not load despite the presence of new files. Again, when I run it through the command line, the files are downloaded.

Any ideas? suggestions, alternative methods?

Thanks.

+8
python scp scheduled-tasks
source share
6 answers

I had the same problem when trying to open an MS Access database on a Linux virtual machine. Running the script on the Windows 7 command line worked, but it did not run on the Task Scheduler. With the help of the task scheduler, he will find the database and check its existence, but will not return the tables to it.

The solution was for the Task Scheduler to run cmd as a / Script program with / c arguments python C: \ path \ to \ script.py (in the Add arguments section (optional)).

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

+12
source share

You can use the Windows Task Scheduler, but be sure to fill in the "optional" field "Start In".

In the Task Scheduler application, add an action that tells your python file to start doSomeWork, and fill in the Start input (optional) using the directory in which the file is located. For example, if you have a python file in:

 C:\pythonProject\doSomeWork.py 

Would you enter:

 Program/Script: doSomeWork.py Start in (optional): C:\pythonProject 
+4
source share

I have the same problem. When testing, I found that any type of call with a subprocess stops the python script when it starts in the 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 launched on the command line, this produces:

 Start First call finished 

When starting from the task scheduler, the output is:

 Start 

To get the result from the task scheduler, I run a python script from a batch file as follows:

 python test.py >> log.txt 

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

+3
source share

Brad's answer is right. A subprocess needs a shell context to work, and task manager can start python without it. Another way to do this is to create a batch file that is launched by the task scheduler, which calls python c: \ path \ to \ script.py, etc. The only difference is that if you come across a script that has a call to os.getcwd (), you always get the root directory where the script is located, but you get something else when you call the cmd call from the task scheduler.

+1
source share

Last edit - start

After the experiments ... If you specified the full path to the Python program, it will work without the highest privileges (as an administrator). The value of task settings like this:

 program: "C:\Program Files\Python37\python.exe" arguments: "D:\folder\folder\python script.py" 

I have no idea why, but it works even if the script uses a subprocess and multiple threads.

Last Edit - End

What I did, I changed the task settings: I selected Run with highest privileges . And the task started to work fine when python [script path] started. But keep in mind that the heading contains "Administrator:" at the beginning ... always ...

PS Thank you guys for pointing out that the subprocess is a problem. It made me think about task settings. I had a similar problem when one script is run from the Windows Task Scheduler and the other is not. Running cmd with python [script path] did not work for me on Windows 8.1 Embedded x64. Not sure why. Probably due to the need to have spaces in the path and a problem with quotes. Hope my answer helps someone. ;)

0
source share

Create a batch file, add a Python script to your batch file, and then schedule the batch file to work. Example: suppose your python script is in the c: \ abhishek \ script \ merun.py folder. You must first go to the directory using the cd command. So your batch file will look like this:

cd c: \ abhishek \ script python merun.py

it works for me.

0
source share

All Articles