Start an application at system start without login

2019-05-06 15:18发布

问题:

We have a new server running and we got some new programs doing import routines. So far so good... But there is one program that is put into autostart folder. So it doesn't run until admin logs in and it stops if we logout.

I'd like to put this one into a seperate session so it may work without any interaction by simply starting it with the task scheduler at startup. Is this the right way to do this? Is it safe if I log in later and log out?

Many thanks!

Edit: The applications shows as a symbol in the task bar if running, it can be configured by this. Anything I must know about this if I change?

Edit: It is not my application, I cannot rewrite it as a service.

回答1:

You need to separate your application in two.

To allow it to run without a user session, you need a windows service. That should handle all the background stuff. You can then register the service and set it to start when the system starts.

To allow it to have a UI, and show up in the notification area, you need a windows application. This will be in autostart as usual, and will communicate with the service - for example, over named pipes.

While it is still (barely) possible to run an UI application without a user session, it's only maintained for backwards compatibility, and already shows a lot of problems. It will likely be removed altogether in the future, because it breaks quite a few contracts. Do not rely on hacks like this.



回答2:

I successfully added the application by using task schelduler on startup. Login and logout will not quit the application but no symbol is shown. Please add details to my side questions and I'll mark your answer as the accepted one.

Edit: Ended up using this one. If I have to configure, I stop the application in task manager and start it again by link. After that I quit the application and restart it by task scheduler manual start.



回答3:

You need to run your program as a Windows Service. One way of doing it is using the sc.exe program:

> sc create <new_service_name> binPath= "c:\myapp\myapp.exe"

You can read about it here.



回答4:

I could not get the "sc create" command to work. Instead I manually edited the registry using regedit. I added a new key in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services.

I used the following page to figure out required parameters and their values. Note that the names do not map.

https://docs.microsoft.com/en-us/windows-hardware/drivers/install/inf-addservice-directive



回答5:

Old question, but for anyone that stumbles here. Use srvany to set the program as a custom service.

Note that when you do this with for example dropbox, googledrive, etc., you will need stop the service, then open the program normally to make changes like password, updates, etc.

below is a well enough intro.

https://www.iceflatline.com/2015/12/run-a-windows-application-as-a-service-with-srvany/

Download the tool kit here

https://www.microsoft.com/en-us/download/details.aspx?id=17657