Automatically run program on Windows Server startup
Asked Answered
D

4

25

I am trying to create and install a .NET app on a windows server that is always on, but I am running into problems. I have heard that I should not make it a Windows service (and would personally rather have a dialog app so I can see progress, etc) so I am trying to use the task scheduler. However, the task scheduler keeps trying to close my app prematurely, when it should only open it and leave it open.

Any ideas how I could have my dialog app run on startup (and stay running) on my server?

Dovetailed answered 13/5, 2011 at 2:15 Comment(1)
Ok so I've gotten it to work by creating a program that opens my main program, and having task scheduler open the first one. Somehow I don't think this is the best way of doing it, any other suggestions (other than making a service)?Dovetailed
P
11

You should make a Windows Service; it's the only reliable way to do this.
Otherwise, you will run into problems if, for example, the user logs off.

If you want UI, you can make a separate GUI that communicates with the service (probably using WCF).

Psychosurgery answered 13/5, 2011 at 2:17 Comment(9)
How could I still have the service provide feedback, and be able to turn it on/ off on the fly?Dovetailed
Could he make a Windows Service that ensured his dialog app was always running?Illuminating
Write a GUI that communicates with (using WCF) and controls (using ServiceController) the service.Psychosurgery
@panamack: That's an even worse idea.Psychosurgery
Is there any way I could get scheduler to do it? I really don't think a service is necessaryDovetailed
Perhaps, but I recommend against it. For one thing, it will be more difficult to maintain.Psychosurgery
How would it be more difficult to maintain? We are constantly going to be editing the program so a service seems a bit overkillDovetailed
Wow you're right, I'm really starting to run into log on issues (apparently user interfaces cannot load unless a user is logged on). What a pain in the ass... I think I'm going to take your suggestion and just make a service, unless someone else has another suggestion. What happened to just putting a program into the startup folder, or changing some settings in the registry?Dovetailed
@Rob: Those are for logged in users.Psychosurgery
T
33

You can do it with a Scheduled Task setup to run at startup time or on a schedule so there wont be problems with users logging on/off.

  1. Open Task Scheduler, Windows Key + R

Taskschd.msc

  1. Click Action menu > Create Task

enter image description here

  1. Change the User to a Administrator or preferably a Service Account and note the option to Run whether user is logged in "or not":

enter image description here

4a. Set At startup - nothing to do with Users logging in or off

enter image description here

4b. Or set At Log On - depends on Users logging on

enter image description here

4c. Or set On a schedule to work regardless of users being logged in:

enter image description here

  1. Set a program to start:

enter image description here

NOTE: Please use a Service Account to do this, if you use your Admin account then it will FAIL after changing password,

NOTE: For unattended applications choose a Console app as some user interfaces apps can have issues unless a user is logged on, it depends YMMV. In general don't keep windows alive, ie dont spawn MsgBox's - kill all windows after use or only have the one console app running in the background unattended. The last thing you want is to crash the computer with too many handles and GDI Objects left undisposed.

start /B your_console_app.exe

The /B flag tells start to run the application in the background without creating a new window.

enter image description here

Tameratamerlane answered 18/5, 2017 at 2:30 Comment(3)
This only works on user's log-on. So in case server was rebooted for some reason, this task will never run until you log on.Mosira
@Mosira see step 3. Run whether user is logged in or notTameratamerlane
This was perfect - the main point being it runs at computer startup, not user login.Observant
A
13

In case if you are looking for "automatically run after login" here is answer:

In the "Run" prompt type shell:Startup

enter image description here

This will open a window in the otherwise hidden AppData path for the Startmenu/Programs/Startup -> Add a shortcut to the executable for the program you want to start.

This will of course only apply to the logon account that you're using at the time.

For all users type shell:Common Startup

Alarm answered 13/8, 2016 at 22:29 Comment(4)
"The filename, directory name, or volume label syntax is incorrect.". Does this only work in non-Server Windows? (I'm using Server 2016).Titular
this works only on user login, not the system boot, bad namingCalyx
@SteveSmith Not sure why in command prompt Shell command stopped working now. Thanks for pointing out. Updated the answer accordingly.Alarm
@ArtemKozlenkov Thanks for pointing out. Updated the answer accordingly.Alarm
P
11

You should make a Windows Service; it's the only reliable way to do this.
Otherwise, you will run into problems if, for example, the user logs off.

If you want UI, you can make a separate GUI that communicates with the service (probably using WCF).

Psychosurgery answered 13/5, 2011 at 2:17 Comment(9)
How could I still have the service provide feedback, and be able to turn it on/ off on the fly?Dovetailed
Could he make a Windows Service that ensured his dialog app was always running?Illuminating
Write a GUI that communicates with (using WCF) and controls (using ServiceController) the service.Psychosurgery
@panamack: That's an even worse idea.Psychosurgery
Is there any way I could get scheduler to do it? I really don't think a service is necessaryDovetailed
Perhaps, but I recommend against it. For one thing, it will be more difficult to maintain.Psychosurgery
How would it be more difficult to maintain? We are constantly going to be editing the program so a service seems a bit overkillDovetailed
Wow you're right, I'm really starting to run into log on issues (apparently user interfaces cannot load unless a user is logged on). What a pain in the ass... I think I'm going to take your suggestion and just make a service, unless someone else has another suggestion. What happened to just putting a program into the startup folder, or changing some settings in the registry?Dovetailed
@Rob: Those are for logged in users.Psychosurgery
K
5

I prefer to create a task in the Task Scheduler and set the trigger as "On Startup", but you can also use Startup Scripts.

You can also use the Local Group Policy Editor (executing gpedit) and add a Startup script. Copying the steps from http://technet.microsoft.com/en-us/library/cc770556.aspx so that if they take it down, it still can be found at SO.

To assign computer startup scripts

  1. Open the Local Group Policy Editor.
  2. In the console tree, click Scripts (Startup/Shutdown). The path is Computer Configuration\Windows Settings\Scripts (Startup/Shutdown).
  3. In the results pane, double-click Startup.
  4. In the Startup Properties dialog box, click Add.
  5. In the Add a Script dialog box, do the following:
    • In the Script Name box, type the path to the script, or click Browse to search for the script file in the Netlogon shared folder on the domain controller.
    • In the Script Parameters box, type any parameters that you want, the same way as you would type them on the command line. For example, if your script includes parameters called //logo (display banner) and //I (interactive mode), type //logo //I.
  6. In the Startup Properties dialog box, specify the options that you want:
    • Startup Scripts for <Group Policy object>: Lists all the scripts that currently are assigned to the selected Group Policy object (GPO). If you assign multiple scripts, the scripts are processed in the order that you specify. To move a script up in the list, click it and then click Up. To move a script down in the list, click it and then click Down.
    • Add: Opens the Add a Script dialog box, where you can specify any additional scripts to use.
    • Edit: Opens the Edit Script dialog box, where you can modify script information, such as name and parameters.
    • Remove: Removes the selected script from the Startup Scripts list.
    • Show Files: Displays the script files that are stored in the selected GPO.
Kassiekassity answered 17/12, 2013 at 17:1 Comment(2)
This applies only to: Windows 7, Windows 8, Windows Server 2008 R2, Windows Server 2012Mosira
yeah, this was 2013... I'd say it ages pretty pretty wel :)Kassiekassity

© 2022 - 2024 — McMap. All rights reserved.