Windows Task Scheduler stuck by consent window - python

I am using Windows Task Scheduler to extract data from Bing Ads API
This process requires providing code on the first run, it automatically opens a browser and require copy code from url to the running program. After that, the process can run successfully without any input.
I set up a .bat file to run python script like python C:\abc.py then used window scheduler to open the .bat file to trigger the script.
I did test it by open .bat file directly. It worked.
However, It did not work by triggering window scheduler. The process is stuck at running status. I doubt that it is stuck at consent window input like it was running the first time.
Is there any way to verify that?
If it's true, Is there a way to solve my problem?

Related

Scheduled Python script in task scheduler not working

I have a python script that I am trying to schedule to run in the task scheduler in my VM but it doesn't seem to be running, it returns (0x2) for last run result. I am able to run the script manually and it works. I even created a batch file to execute the script which works and tried scheduling that in Task Scheduler but it also gave the same error. My only guess is that it's not working because it uses the Google Sheets API and reads the credentials from a JSON file in the project folder but I'm still unsure as to why it wouldn't run when scheduled. If you have any ideas I would greatly appreciate it. In the task scheduler, I am using the path Z:\Python\PythonGSAPI\executePy.bat to execute the batch file. The content of the batch file is
#echo off
"C:\Python27\python.exe" "Z:\Python\PythonGSAPI\TF_Invoice.py"
pause
This occur due the PATH Enviroment variable, for exemple, if you use Anaconda Python it's needed to choose the first option during the installation or even configure this after.
enter image description here

Python scripts involving selenium behaving differently when called from task scheduler, but working as intended when run from spyder or command line

I have created a python program which uses selenium and chromedriver. I cannot successfully run this script (or any others using selenium) from the TaskScheduler in any way. However, it runs perfectly fine and does all tasks I need when I run it from Spyder. It also runs perfectly while logged in when I call it via the command line.
What does the program do when working as intended:
Launches a chrome browser.
Automates clicks and page requests.
Downloads a file.
-does stuff with files that is irrelevant to this post-
What does the program do when called from the TaskScheduler:
Starts chrome but it does not appear (no visible browser, but task manager recognizes the chromedriver and chrome being kicked off and running persistently after the script is called)
All of my clicks are on elements by full xpath so I thought maybe the invisible browser wouldn't break it, but it does indeed fail, never fetching the file download.
Possibly relevant information:
My chromedriver is not on path, but is set via
driver = webdriver.Chrome(r'F:\chromedriver.exe') and this works absolutely fine when run by Spyder or command line.
Task Scheduler input
Action: Start a program
Program/Script: C:\ProgramData\Anaconda3\python.exe
Add arguments (optional): "C:\Users\[My_redacted_name]\.spyder-py3\[Client's_redacted_name]\[redacted_task].py"
What I know: The working directory as suggested in Python script not running in task scheduler does not fix anything.
Running from the command line
C:\ProgramData\Anaconda3\python.exe C:\Users\[My_redacted_name]\.spyder-py3\[Client's_redacted_name]\[redacted_task].py
yields the exact results as intended
No other programs I have made have had this kind of issue, and I have dozens of programs running via TaskScheduler with similar functionality to all the components OTHER than selenium / chromedriver.
I actually have two scripts using selenium that both encounter the same issue when running from command line. Their tasks are more or less the same, so solving ONE should solve the other, but it should be noted that the issue is not unique to a single script, but instead unique to scripts using selenium and running from task scheduler
I also see Selenium - Using Windows Task Scheduler vs. command line and am attempting to see if the single response with 0 votes could help, but I'm not sure if the issue is truly the same given it was for IE and on java.
My initial workaround for running the script on a set schedule was utilizing datetime variables and while True loops, then switched to apscheduler for convenience. Switching back to the workaround again ran the script flawlessly.
Reference code:

Linux - Open chromium from a script that runs on startup

I have a bash script that I've defined to run in startup, which runs a python script that waits for a command from another process, and when it gets it, it should open a chromium window with a certain URL.
When I run this script manually it works fine, but when the script runs from startup, I get an error (displayed in syslog):
Gtk: Can't open display
I guess that's because it's running in a startup mode so it doesn't actually have a display to "lean" on...
I was wondering if there's any way to get this work, anyway?
Thanks in advance
In your script that runs on startup try DISPLAY=:0 <command> &
To clarify DISPLAY=:0 simply sets which monitor your window opens on with 0 representing the first monitor of the local machine.

Is it Possible to Run a Python Code Forever?

I have coded a Python Script for Twitter Automation using Tweepy. Now, when i run on my own Linux Machine as python file.py The file runs successfully and it keeps on running because i have specified repeated Tasks inside the Script and I also don't want to stop the script either. But as it is on my Local Machine, the script might get stopped when my Internet Connection is off or at Night. So i couldn't keep running the Script Whole day on my PC..
So is there any way or website or Method where i could deploy my Script and make it Execute forever there ? I have heard about CRON JOBS before in Cpanel which can Help repeated Tasks but here in my case i want to keep running my Script on the Machine till i don't close the script .
Are their any such solutions. Because most of twitter bots i see are running forever, meaning their Script is getting executed somewhere 24x7 . This is what i want to know, How is that Task possible?
As mentioned by Jon and Vincent, it's better to run the code from a cloud service. But either way, I think what you're looking for is what to put into the terminal to run the code even after you close the terminal. This is what worked for me:
nohup python code.py &
You can add a systemd .service file, which can have the added benefit of:
logging (compressed logs at a central place, or over network to a log server)
disallowing access to /tmp and /home-directories
restarting the service if it fails
starting the service at boot
setting capabilities (ref setcap/getcap), disallowing file access if the process only needs network access, for instance

Run Python CGI without waiting

I've seen write-ups but am not sure how to apply this to my example. I want to be able to push a button on my webpage, run a piece of python code -- and within the python code it runs another process (executable). The executable takes time, so I want the webpage to not wait until the executable is done running.
I have seen things on daemon and forks, but am not sure how to implement them. I am running an Apache Server with Python CGI on a Windows 8 machine.
Currently, my python code runs and the executable runs fine, but the web page waits until the exe is done processing before updating with an alert window that the "Request has been submitted". Here is my popen code as it is now:
p = subprocess.Popen(['test_sendemail.exe',cmd],shell=False,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
Again, everything works fine here, but I need the python script to end even though the exe is running so that it can update the webpage and move on. (once the exe is done running, it fires an email off with results -- which is why I don't want to wait on the webpage for it to finish).
Any thoughts? Thanks!

Categories

Resources