0

I would like to run a python script everytime the computer boots, without having to login.

To do so, I tried to create a windows service that will first run a powershell script (to do some basic check configurations) and that will run a simple python command line like this:

python ./app.py

But like I said I would like this script to run at startup and for this I've created a windows service. The problem is that even though the powershell script runs, the python command (and only this command) won't work.

Some usefull information

If I start the powershell script from a terminal: it works.

If I start the python script directly from a terminal: it works.

If I start the python script as a background process with pythonw: it works.

I know the powershell scripts runs at startup because I asked him to create folders (as a test) to show me it works.

The python program is using multi threading and a network connection. I don't know if that's relevant but that just to say it's a pretty big program that doesn't need any human interaction and that works on its own. Maybe Windows is blocking something about that ?

Other information: the powershell script (and so the python script) both need to run as Administrator. I know it already does because with the following condition it creates a folder.

if (([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    mkdir C:\Users\Desktop\Test2
}

What I've tried

I first created a windows service in C# that runs with full privileges with this basic code:

var ps1File = @"C:\\path\\to\\powershell\\script\\start.ps1";

            var startInfo = new ProcessStartInfo()
            {
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -NoExit -ExecutionPolicy unrestricted -file \"{ps1File}\"",
                UseShellExecute = true
            };
            Process.Start(startInfo);

The service does seem to works like I said, it really is the python command that won't.

I also tried to create a service with NSSM and with AlwaysUp too but nothing works.

Toto
  • 89,455
  • 62
  • 89
  • 125
maximerx
  • 42
  • 2
  • The usual suspects are the _user privileges_ (because the service may run under a different account than yours) and the _environment_. You can use your C# service to print out PATH, PYTHONPATH, etc in the service context and compare to your own context. – joao Jul 07 '21 at 10:44
  • @joao Thanks for your answer. You made me realize that PYthon wasn't installed on the system but only for my local account. It would have been a problem at a certain point so thanks. But unfortunately the service still won't run, I'll look more into the user privileges but if you have anywhere else to look, please be my guest – maximerx Jul 07 '21 at 12:04
  • In the past if you wanted a service to run a command line program like the Python interpreter then it needed the special privilege *Interact with desktop*. But that privilege is now viewed as a security risk and has been largely nerfed. You can write a service directly in Python as explained in Hammond & Robinson, *Python Programming on Win32*, O'Reilly, ISBN 978-156592621-9 (https://zulfahmed.files.wordpress.com/2019/01/markhammond-andyrobinson-pythonwin32.pdf) Chapter 18. That book was written for Python 2 but AFAIK it is still the best resource and covers all the plumbing needed. – BoarGules Jul 07 '21 at 13:27
  • If it just needs to run once at startup instead of run constantly in the background, I recommend adding your script to your startup programs instead of creating a service for it. There are some oddities, but either this answer or the one below it about a simple task scheduler entry will have what you need: https://stackoverflow.com/a/32189430/7411885 – Cpt.Whale Jul 07 '21 at 15:34

0 Answers0