start a windows service on a python script that has a space in its name, using NSSM

21 views Asked by At

So I am wondering why this doesn't work.... My issue is as title says. I have a python script, when I run it as is via windows service, it works, but if I take this same script with a space in it, then it fails.

I have re-created a simple example, if anyone has a way to fix this, it's be really great, cause I have no idea anymore on how to make it work : Basically a python script that creates a test.txt file in D:/ if none exists, else it destroys it (easy way for me to see if the service runs or not):
test.py:

    import time
    from pathlib import Path
    
    if Path("D:/test.txt").exists():
      Path("D:/test.txt").unlink()
    else:
      with open(Path("D:/test.txt"), 'w') as f:
        pass
    
    while True:
      time.sleep(1)

test.ps1 (the one that launches the service):

    $nssmPath = "'C:\NSSM\nssm.exe'"
    $agentServiceName = "atestservice.py"
    $agentFolderPath = "D:/"
    $script = "test.py"
    $pythonPath = (Get-command python).Path
    Write-Host  "& $nssmPath install $agentServiceName ""$pythonPath"" ""$script"" "
    Invoke-Expression "& $nssmPath install $agentServiceName ""$pythonPath"" ""$script"" "
    Invoke-Expression "& $nssmPath set $agentServiceName AppDirectory ""$agentFolderPath"" "
    Start-Service $agentServiceName
    
    $myInput = Read-Host -Prompt "Please enter your input:"
    
    Stop-Service $agentServiceName
    sc.exe delete $agentServiceName

So the code above works. Now if you put a space in tes t.py and run this, it won't work (changing it in the ps1 script and renaming the python script itself as well). Why ?

0

There are 0 answers