Observe the following Python script, "Script.py":
import subprocess
src_directory = 'Z:\z_7z\Some_Directory'
zip_file_name = 'Test.7z'
cmd = ['7z', 'a', zip_file_name, src_directory, '-mx9']
subprocess.Popen(cmd, stderr = subprocess.STDOUT, stdout = subprocess.PIPE)
My intent is to schedule a Python script using Windows Task Scheduler. I have successfully done this using other Python scripts before. However, I am unable to execute the script shown above via scheduling. I am unsure as to whether this is a Windows Task Scheduler problem or a Python problem, but here is what I know:
"Script.py", as shown above, is a script for running a 7zip compression on a "Some_Directory" directory. The script itself and the 7z.exe application which it is invoking are both stored in the "Y:\z_7z" directory.
The script seems to be working fine when executed manually. I can double-click on the script and it will execute properly. Also, I can execute the script from the command line via the following command:
Y:\z_7z\Script.py
However, I cannot execute the script manually by navigating to the "C:\Python27" directory and attempting the following:
python Y:\z_7z\Script.py
This yields the following error:
Line 5 in module subprocess.Popen(cmd, stderr = subprocess.STDOUT, ...)
WindowsError: [Error 2] The system cannot find the file specified
Provided all of that information, the real problem I am having is that Windows Task Scheduler cannot execute this script (Last Run Result = 0x1). I have tried various Windows Task Scheduler configurations, including the one which seems to be ideal which goes as follows:
Program/script: "C:\Python27\python.exe"
Add arguments (optional): "Y:\z_7z\Script.py"
Run whether user is logged on or not
Again, I have scheduled other Python scripts before which have run successfully. This Windows Task Scheduler task seems to be configured properly. I browsed through some of the more advanced settings and did not find anything suspicious with this particular task.
Don't just launch 7z. Provide the full path to the executable.
cmd = [r'C:\Program Files\7zip\7z.exe', 'a', zip_file_name, src_directory, '-mx9']
Would work, considering that C:\Program Files\7zip\7z.exe is the executable path.
Try not running the python process with the script as an argument. Run the python script itself.
Your zip_file_name is relative. I'm not sure the argument is a filename. It may be a path. In that case, the .7z file may be created on C:\Windows\System32. To fix it, set zip_file_name to be a full path.
Related
My goal is to schedule the execution of a simple python script in Linux with the at command.
Problem: My python script is not executed at the specified time. Nothing happens, no error messages.
I have the following simple helloworld file:
#!/usr/bin/python3
print("hello world")
To schedule the job I type:
at now + 1 min
I provide the file I want to execute:
./helloworld
Typing atq I see that the job was scheduled nicely.
But it just happens...nothing.
I work on Kali Linux
I have given execution permission to the root user for that file
I can run the helloworld file from the command line
the root user has permission to run at commands (not listed in /etc/at.deny)
I can schedule the job echo "hello world" > message.txt. That works fine.
What I am not sure about:
is there something wrong with the shebang line?
I have checked /usr/bin/: python3 is in there and is linked to python3.9.
at does not write to the terminal (which may not even exist when the command runs). Instead,
The user will be mailed standard error and standard output from his commands, if any. Mail will be sent using the command sendmail(8). If at is executed from a su(1) shell, the owner of the login shell will receive the mail.
So if your command only writes to standard output, you'll need to check the local mailbox for the output of the command, using mail or some other mail client.
Otherwise, you can have your command explicitly write to some other known file to look for later.
It is better to keep the extension .py for your script since it is python.
Your file should be executable
$ chmod +x ./helloworld
For your needs, you can just type the full path to your script when running at.
If you want to use helloworld command, the folder that contains this script should be in PATH environment variable. Otherwise, the system just doesn't know where to find it. If you do this, you don't need to type ./ before the command or any path at all.
Nice article about PATH https://opensource.com/article/17/6/set-path-linux
I have a batch file to execute Python and run a Python script that works fine when run manually. But when run using Windows Task Scheduler, the Python Script gives me a traceback because it can't find a .TXT file that's in the same file as the Python script.
There are many flavors of the "batch file works manually but not with Task Scheduler" problem. I think my problem may be related to the fact that my Python script is in a virtual environment. It may also be related to Windows user accounts and permissions. Past posts about Task Scheduler have been resolved with changing settings related to user accounts but so far that hasn't worked for me.
More details on Windows Task Scheduler settings:
Here are the settings I've chosen for the task:
My task gets closest to working when I use the "run only when user is logged on option. If I choose "run whether user is logged on or not" the task opens a Command Prompt but doesn't appear to execute anything.
More details about batch file
My batch file looks like this:
"C:\Users\MyPathtovirtualenvironment\virtualenv\Scripts\python.exe" "C:\Users\fried\Desktop\calendaralert\court_calendar_alert1.3_automatic.py"
pause
When run manually, it does what it's supposed to: it executes Python from within the virtual environment, then loads the Python script, which runs correctly except when it's run from Windows Task Scheduler.
More details on Python script
The Python script works fine when run by itself in the command prompt and as a batch file, so I don't think there's anything directly wrong with it. Here's the part of the script that causes the traceback.
with open('replacements.txt') as f:
replacements = dict(x.rstrip().split("!") for x in f)
The replacements.txt file is in the same folder as my Python program. I also tried putting copies of the file within the Virtual Environment folder and the /scripts subfolder without success.
Just came across this problem, while running a python script on a windows ec2 instance. The problem got solved by adding this at the top:
import os
path = "your-directory-path"
os.chdir(path)
Little background: Code::Blocks is an IDE with a C++ integrated compiler. When creating a C++ project, it creates a .exe file so you can run the project.
So now I want to run that executable file using a Python script (Using VSCode). I tried subprocess.call(), subprocess.run() and subprocess.Popen(), and all of them start the background process, but it doesn't compile, so it just keeps running on the Task Manager. If I run it manually (by double-clicking it) then it opens, it closes and I get my correct answer on the output file.
This is the C++ project folder for the problem "kino" :
This is a photo with the .exe on the Task Manager :
And this is my Python code:
process = subprocess.run([r'C:\Users\Documents\kino\kino.exe'], shell = True)
If you still don't understand my problem, here is a video describing it.
Use pywin32 to get it done.
Something like this will solve your issue
import win32com.client
app = win32com.client.Dispatch("WScript.Shell")
app.Run('Path/Yourexe.exe')
subprocess.run would not capture the output of the spawn process, however there is no reason for it to keep running in background. Try the following example, I just checked this in my linux(with a simple 'hello world' program) so if it does not works for you then it could be OS specific issue.
p = subprocess.Popen(['C:\Users\Documents\kino\kino.exe'], stdout=subprocess.PIPE)
#out, err = p.communicate()
print(p.stdout.read())
The way I solved my problem was by running the executable using the ./ command. So, I did something like
process = subprocess.run('./kino.exe', shell = True)
Is it feasible to run a program in Python's subprocess module, but with files from Terminal?
So I want to run the following program from within Python:
myProgram -a myArg
However, suppose that the above program requires the file myFile in the current directory, and it doesn't take the required file as an argument. So, if you run the above program in the directory where there is myFile, the program succeeds in processing. However, if you run it in the directory where there is NOT myFile, the execution fails.
And when I tried to execute the program from within Python's subprocess.Popen(), with shell=True, the program doesn't work and it looks like the reason it failed is the program wasn't able to read myFile when executed from within Python.
So, is there any way to run it successfully from within Python?
subprocess.Popen('myProgram -a myArg', cwd='/folder/with_myFile/')
Similar Question: Python specify popen working directory via argument
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.
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
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.
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.
Brad's answer is right. Subprocess needs the shell context to work and the task manager can launch python without that. Another way to do it is to make a batch file that is launched by the task scheduler that calls python c:\path\to\script.py etc. The only difference to this is that if you run into a script that has a call to os.getcwd() you will always get the root where the script is but you get something else when you make the call to cmd from task scheduler.
Last edit - start
After experiments... If you put there full path to python program it works without highest privileges (as admin). Meaning 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 script uses subprocess and multiple threads.
Last edit - end
What I did is I changed task settings: checked Run with highest privileges. And task started to work perfectly while running python [script path].
But keep in mind, that title contains "Administrator: " at the begining... always...
P.S. Thanks guys for pointing out that subprocess is a problem. It made me think of task settings.
I had similar problem when one script is running from Windows Task Scheduler, and another one doesn't.
Running cmd with python [script path] didn't work for me on Windows 8.1 Embedded x64. Not sure why. Probably because of necessity to have spaces in path and issue with quotes.
Hope my answer helps someone. ;)
Create a batch file add your python script in your batch file and then schedule that batch file .it will work .
Example : suppose your python script is in folder c:\abhishek\script\merun.py
first you have to go to directory by cd command .so your batch file would be like :
cd c:\abhishek\script
python merun.py
it work for me .
Just leaving this for posterity: A similar issue I faced was resolved by using the UNC (\10.x.xx.xx\Folder\xxx)path everywhere in my .bat and .py scripts instead of the letter assigned to the drive (\K:\Folder\xxx).
I had this issue before. I was able to run the task manually in Windows Task Scheduler, but not automatically. I remembered that there was a change in the time made by another user, maybe this change made the task scheduler to error out. I am not sure. Therefore, I created another task with a different name, for the same script, and the script worked automatically. Try to create a test task running the same script. Hopefully that works!
For Anaconda installation of python in windows below solution worked for me
create a batch file with below
"C:\Users\username\Anaconda3\condabin\activate" && python "script.py" &&
deactivate
Setup task to run this batch file