I noticed a rather interesting problem the other day.
I have a windows scheduled task on Windows server 2008 RT. This task runs a batch file which runs a python script I've built. Within this python script there is a subprocess.Popen call to run several other batch files. However for the past couple days I've noticed that the task has successfully run however the secondary batch files did not. I know the python script ran successfully due to the logs it created and all the files it makes that the secondary batch files use are all there. However the completed files are not.
If I just run the batch file by itself everything works perfectly. Does Microsoft's task scheduler not allow a program to open additional batch files and is there a workaround for this?
I had exactly the same problem when task scheduler tries to call a .bat which runs a python script.
I found the trick is to put the path of the bat file in Start in (optional)
Thanks to answer from Jordan8307
https://community.spiceworks.com/topic/531774-task-scheduler-fails-executing-bat-file-but-i-can-run-it-manually
Same situation: taks -> batch script -> Python process -> subprocess(es), but on Windows Server 2012
I worked around the problem by providing the absolute path to the script/exe in subprocess.Popen.
I verified the environment variables available inside the Python process and the script/exe is in a directory on the PATH. Still, Windows gives a FileNotFoundError unless I provide the absolute path.
Strangely, calling the same script/exe from the batch script is possible without providing its absolute path.
Related
I am working on python project, where at particular time (8 pm) a random number will be displayed. I have converted .py file to .exe using pyinstaller. Now, I want my .exe file to run automatically and display the number, even if I won't open .exe file at 8pm.
Since you're referring to .exe I assume you're on Windows.
You should use the Task Scheduler in Windows to schedule your application to run.
For that you could run it with full path to your python.exe with the .py file as parameter, so conferting to .exe isn't really necessary.
To automagically run this on time when the system is switched off, you will need to check the BIOS settings of your PC at startup to see if you can make the computer switch on all by itself at a certain time. Make sure to set this automatic power on a few minutes before the scheduled task to allow Windows to start up first.
I was working on a python script that I want to run via the task manager. I created a batch file like this:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe" "\\insert_path_here\project_directory.py"
pause
When I have the project_directory.py as a path on my hard drive, it works fine with either the task manager or manually opening the batch file. When I put the .py file on a remote drive, the batch file still executes correctly when I manually open it. When the task manager tries runs the .bat file, I get a message asking which application I want to use to run the program:
What do?
Thanks!
Try using cmd.exe. on default you should find it here: c:\windows\system32\cmd.exe.
On the side note explore option of converting .py to .exe, you can start here: https://www.simplifiedpython.net/convert-python-to-exe-tutorial/
I feel like it would be better option for you.
It looks like it is trying to run the python.exe and then the project_directory.py file separately. Try putting those in as one command:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe \insert_path_here\project_directory.py"
You probably have *.py files already associated with Python on your local machine which is why it works there.
I wrote a script with a bunch of modules to convert txt files to pdfs. There's a watchdog module which checks a certain folder for newly created txts, python siphons the data into a latex template which is being compiled into a pdf.
I created my abomination in PyCharm and if I start the script within PyCharm, it works flawlessly. I compiled it as exe via 'pyinstaller --onefile' and it works flawlessly.
However, I need to run it as a windows service or at least without the window existing in the task bar because... reasons. I tried:
installing it as service via non-sucking service manager
starting it as hidden programme via a vbs script, powershell script and nircmd
and nothing helped. I'm at a loss here, there's no interaction whatsoever with the programme so it should work, also scripts are enabled on the machine and the process is visible in the task manager. I somehow suspect that it has problems calling other python scripts while it's a service or hidden programme but it doesn't seem to be logical and I have no means to verify anything whatsoever that I'm aware of.
My last idea is to somehow let it exist as a task bar tray thingy but that would be more than a shabby solution for this whole ordeal. Could anyone help me out here? Thanks
I currently have a Python script that scrapes some data from the internet, then saves it as a pickle file. When running this from a terminal with python filename.py it saves correctly, since the Date Modified field of the pickle file changes. However, when running with the built-in scheduler it doesn't actually save the pickle file, since the Date Modified isn't changed, despite seeing the Python script being executed (terminal opens up and I see the script running).
I ticked the Run with highest privileges box in the scheduler, and despite that it doesn't save the pickle file. I thought it had to do with it not having write permission, but if it has the highest priviliges, surely it can save a file?
At the scheduled time a terminal opens, so I know it is actually being executed (print a message to make sure), but it doesn't show an error about the fact that it couldn't save the file or anything like that. The only reason I know it's not working is the Date Modified field not changing. How can I fix this?
Windows Task Scheduler has a default working directory of C:\Windows\System32. If you set a relative path to the file you are trying to write, it will likely be written into that directory. If you open a Command Prompt in the directory of your script and run it, the relative path will be that directory. So, you actually have two copies of the pickle file.
If you set an absolute path in your script to the file you want to write to, both methods of running your script will write to the same file.
so my ultimate goal is to use python to read a specific application's windows event log when triggered by a file update.
Here is my problem, python I believe does not have access to the event logs stored in C:\Windows\System32\winevt\Logs. Whenever I try to read the files I get the following error:
WindowsError: [Error 2] The system cannot find the file specified
I tried every form of escaping, string split/join and using quotes on the file path and I always get the same error. I even cheaply used the os.system('dir "C:\Windows\System32..."') command in the python command prompt to list directories higher in the path for the log to verify access and I receive similar errors up to the C:\Windows\System32 directory, that one will list just fine.
Example:
C:\Windows\System32\winevt\Logs - File not found
C:\Windows\System32\winevt - File not found
C:\Windows\System32 - Lists files
I know these directories exist. So from here I figured I could use a bash script to copy the event log into a temp folder for python to read. I wrote a real simple bash script containing:
xcopy /Y "C:\Windows\System32\winevt\Logs\XXXXXXX" c:\Temp
(XXXXXXX) being the name of the log I want to copy for the python script.
The bash script runs fine on its own but when called by my python script it fails, refuses to copy the file because it can't find it. I have even tried using py2exe to create an exe to then run in administrator mode. This failed too. With similar errors of the file not found. I'm not sure I understand the permissions python uses to call the script and why the bash script cannot copy the file even when it can do it in a normal command prompt.
Any suggestions or flaws you can point out in my logic would be great. I am very stuck.
You are using a 32bits python on a 64bit install of windows.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64
You can use os.listdir("c:\windows\sysnative\winevt\logs") as a workaround to read from the real system32 dir from a 32bit python interpretter runing on a 64bit windows...
Sources:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
http://support.microsoft.com/kb/942589