Python to log in Windows automatically with task scheduler - python

I created a python script to automate a task.
I would like to run it every day on a hourly basis and for this I created a task in Windows Task Scheduler.
Is there a way to write a script to log in Windows automatically when my account is logged out, because my script fails if the Windows user is not logged in.
Thank you.

This is the wrong way to approach this. It's normal and expected that your computer will not always be logged in. You should not try to change that, as it would be very insecure.
The right thing to do is figure out why your script will not run when the system is not logged in. When setting your task up, you can set user credentials it should use when starting. Make sure your program is not making any incorrect assumptions about accessibility or security when it runs.

Related

Pyinstaller *.exe in TaskScheduler

i created a small python program and freezed it to an *.exe file with the --noconsole option. When I run the program normally it works. But when I put it in the task scheduler that runs it on startup it does not work anymore.
The program has no ui. I know that it is not working because it is not writing to a log file. The paths in the program are absolute.
Any ideas what can cause this problem?
You will need to do a little troubleshooting to narrow down the problem. I can share some tips.
Make sure you have an exception handler for uncaught exceptions (at the very top) so you are logging those errors to a file. If you don't have one you will never know why your program is crashing. Make sure if you add this you are re-throwing the exception after logging it so the os knows it crashed.
Open the task schedule and run the task manually. Make sure the task is allowed to be ran manually (it's in the configuration). If it is allowed to run manually, then it should have no trouble running automatically.
The user you're running the task with might be different from the user you are manually running it with. You can try removing the --noconsole option while you are trying to figure out the problem. If it is an issue with your logger not having write access where it needs to, you would not see anything in your logs but can see messages in the console.
Make sure the working directory of the task is correct. By default it should use the same directory the app is in, but if a path has been entered it can cause issues.
In addition to Timothy's ideas, make sure the option Run whether user is logged or not is activated, and if necessary, put the user password (when applying).
When you run a task on startup, the Windows session is not yet opened so the task may not run if Windows has no info how to open a session for it.
I was trying to get information of Duplicati (Backup Software), but the server does start only when somebody is logged in. So the problem was not my program but the start of this other program.
Solved it through logging my program.

Running python code as a service on windows server 2012 R2

I have a python script which collects data from Twitter and stores into MongoDb. I need to run this code on the server as a service.
However, only 2 users can remain logged into the server at any given time and therefore If I log out my account, the python script would stop working and data collection will stop.
My question is - How can I run this script as a service on the server such that it keeps running irrespective of whichever user is logged in?
Task Scheduler is the easiest solution I know of. You can use it to run the code at startup as the NT AUTHORITY\SYSTEM user and automatically restart on failure. In case you need it, there's a basic overview of Task Scheduler here.

Python Script Running in Windows Task Scheduler Completes with exit code 3221225477

I'm troubleshooting a python script that does 3 things.
Stops ArcGIS Server using subprocess.check_output('net stop "ArcGIS Server"', shell=True)
Runs some arcpy functions
Starts ArcGIS Server using subprocess.check_output('net start "ArcGIS Server"', shell=True)
The script runs fine when run from IDLE. It also runs fine as a scheduled task when I check the radio button that says "Run only when the user is logged on"
However, this task runs on a server and I don't want to have to be logged in for the task to run. So I check the radio button that says "Run whether user is logged on or not", and "Run with highest privileges".
The result is the script's log file is empty, and the script never completes. It returns the error code in the Task Scheduler, that I mentioned above.
More task details:
Action: Start a program
Program/script: C:\Python27\ArcGIS10.3\python.exe
Add arguments (optional): D:\ArcGISData\server-data\Python\copydata.py
Am I doing something wrong, or is there any other steps I can try to get this working?
Not sure why it wasn't working to be honest, but after going through the steps to recreate the task, it started working. Maybe I had just mistakenly entered the credentials of the user running the task or something like that.

Python Authlog Log File analysis

I recently started to play around with Python 3 which I enjoy a lot. I'm a sysadmin in apprenticeship so I have nearly no programming experience. I want to make a small program which is going to tell me everytime someone logs into my system via ssh. I'm going to use the espeak-python bindings. What is the best way to analys the log file in real time?
So everytime someone logs into the system via SSH I can hear it over my speakers :-).
I don't want a complete Solution just a few points so I know where to start...
Should the program run in a terminal or as a service? If run as a service you should consider looking at a library like python-daemon. If you only want to run it in a terminal a usual endless loop (which will then be aborted by a ctrl+c) should do fine.
For playing sound to the speakers you could look at PyAudio. I used it for my sound-related projects. Its website (with good examples) can be found here
To parse the file you can open it (maybe after opening do a read() to discard any log entries that was made before starting the program). Then you can do a readline() in every run of the loop. The retrieved line may be empty. If that's the case no login was attempt. If you got a line you only need to check if there's the word 'sshd' in the line and if yes someone logged in via ssh.

UI Automation Windows User login

I have a test case where i have to automate user action on windows UI,
The scenario is that windows password has expired, and when user tries to login to the
system, the reset password page appears and user inputs new password and login succeeds.
Now i have to do this through a script,
Good thing is i have a python service running on the machine to which i can pass commands if required prior to login.
I have Googled a lot but could not find anything similar.
Would be very glad if anyone can point me to anything specific to my problem,
What should be my reference point for this UI automation??
If you're trying to automate the Windows logon UI, I believe it's possible, but it requires that you use a Windows service to run your test code. Sounds like you may already be doing that, since you mentioned a Python service you can use to run commands while no user is logged in to Windows.
Anyway, Windows services are the only way I know to do this (without disabling UAC or otherwise modifying the default Windows security configuration).
See my answer to this question for some pointers:
UAC and remote control
If your Python code is running with the privileges of a Windows service, it should be able to interact with any UI on the system (or at least launch other processes that can interact with secure UI), including the Windows logon desktop.
The only headache may be that in order to interact with the Winlogon desktop, your test code may need to launch a separate program with the CreateProcessAsUser function (and pass "Winsta0\Winlogon" as the desktop param). Not sure, though.
This answer also looks like it might be helpful in your situation:
Running a process at the Windows 7 Welcome Screen

Categories

Resources