Link to home
Start Free TrialLog in
Avatar of jaedenone
jaedenone

asked on

How do I set a Windows service to start automatically after a reboot even if the "Startup Type" is set to Manual?

I use Shavlik for patching and have a few web servers (Windows Server 2008) configured to automatically deploy security patches and reboot. The only problem is that we configure service account tied into the Jboss service to "Manual". So, if after the reboot, the Jboss service will not start automatically. How would I configure a manual service to start automatically after a system reboot? Of course I can always change the service to start automatically, but because the programmers in my company set it to start manually, I cannot change that.
Avatar of bbao
bbao
Flag of Australia image

the following command starts a service manually.

NET START servicename

just add this into your startup batch file.
Avatar of jaedenone
jaedenone

ASKER

bbao,

I'm not a script guy. Can you explain in details how to accomplish this?
When would you expect the script to be automatically executed? After restarting the server without logging on, or after logging with a specific user?
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
bbao,

I want it to start the service whenever it reboots, without any need to log in. By the way, these will only be set on a few servers, which are not joined to a domain.
Then the second approach of my comment is appropriate. Set up a Scheduled Task to start at boot. The setup can be done with the following batch file:
@echo off
for %%C in (server1, server2, server3) do (
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc /TR "C:\Windows\System32\sc.exe start TheServiceName" /delay 0005:00
)

Open in new window

This will create the task on 3 machines, triggered 5 minutes after boot.
Qlemo,

I tried the batch script but it does not seem to work. Maybe you can write it step-by-step for me?

Let's use an example:

1. Let's execute this for a server name "ksolpc01"
2. Let's setup a manual Windows service (attached) as the service that I want to bring up after the system reboot.

Please write the script as is. Where do I save the script? Say I want to execute this to the server from a remote machine, where would I store the script? On the target machine? Does it matter where I store it (e.g. - Desktop)? Also, where you indicated the delay 0005:00, is that the time given for the server to fully reboot before it trigger the auto start of the manual service?
Capture.PNG
Ok, as an examnple you want to start the Application Layer Gateway service at "ksolpc01".

Script name, location and execution doesn't matter, the script is thought to be run remotely, once or more often for any server to set up. The only requirement is that the script extension is either .cmd or .bat, and the target server is reachable from the execution location.

To make the changes obvious, I've used as much as possible of above script:
@echo off
for %%C in (ksolpc01) do (
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc /TR "C:\Windows\System32\sc.exe start ALG" /delay 0005:00
)

Open in new window

And yes, the 005:00 corresponds to "5 minutes after boot".
I followed the script above, but it did not execute. I saved it as a .bat file and saved it locally on the actual server itself, and ran it from the actual server. If this is a scheduled task, it should be stored somewhere under Task Scheduler. Can you tell me where I can confirm if this scheduler is set?
Will this script work if the service is not located in the C:\Windows\System32 directory? What if I want to do this for a Jboss or Tomcat service in the D:?
For test, let's assume you are on PC1, and the batch file is c:\Scripts\startService.cmd .

Open a command prompt on PC1, then start the batch by typing in the full path and name.
You should see a success message, or some error. Errors might arise from authentication (log in) issues; I presumed your account exists and has admin privileges on the remote machine ksolpc01.

If you can't see an error, go to ksolpc01, start Scheduled Tasks, and you should see the task "StartSvc" created directly under "Task Library" (or similar - no English system here).
You can also do the check from PC1 in Scheduled Tasks by connecting to ksolpc01 by right-clicking on the "(local)" line left, and connect to the remote PC, if that is easier.

---------

It doesn't matter where a service's binaries are located. That paths are stored in the registry. As long as you can see the service as such in the Service management console, all is fine. What you see as path in the batch is the for the controlling software, sc.exe, which can be used to create, start, stop, query, ... services.
Here is the path to the manual Jboss service that I want to automatically start after a reboot.

Service Name: ATM Jboss 4.2.3
Path to executable: D:\ServerApps\ATM\wrapper\wrapper.exe -s D:\ServerApps\ATM\wrapper\wrapper.conf

How can this script be written as a batch?
You only need the service name, "ATM Jboss 4.2.3". But since it contains spaces, we have to enclose it in double quotes for commands, and that makes it difficult as we already have to enclose the complete command in double quotes. The resulting command looks strange and incorrect, but should work (it does for me):
@echo off
for %%C in (ksolpc01) do (
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc /TR "C:\Windows\System32\sc.exe start """ATM Jboss 4.2.3"" /delay 0005:00
)

Open in new window

Looks like it works! Thank you for that. Almost close to giving you max points, but just one more question (see attached screenshot). There are two services "Apache Tomcat NP" and "Apache Tomcat SCHDL" on ksolpc01 that I'd like to also set to restart after reboot. How would I modify the script (#2) on the screenshot to start both scripts after restart? Your help is much appreciated.
Untitled.png
Best way IMO is to set up one task per service. It would go with one task, but that's unnecessarily complex.
@echo off
for %%C in (ksolpc01) do (
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc-Jboss /TR "C:\Windows\System32\sc.exe start """ATM Jboss 4.2.3"" /delay 0005:00
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc-NP /TR "C:\Windows\System32\sc.exe start """NP"" /delay 0005:00
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc-SCHDLR /TR "C:\Windows\System32\sc.exe start """SCHDLR"" /delay 0005:00
)

Open in new window

That presumes there is no particular order needed to start the services. If you need to make sure one service is running before starting another, change the delay accordingly, e.g. by adding one minute per service.
Works like a charm!!! Your the best!
I tried to apply the script in Windows Server 2003, but it did not work. How do I verify the startup services in task scheduler in Win 2003 after I run the script? Can you briefly write the script for me in Win 2003? Thank you so much!
» have a few web servers (Windows Server 2008)
and for those I've created the commands. W2003 is different. Luckily only the /delay parameter will not work, so will have to remove that for W2003.

The tasks created are located in Scheduled Tasks in Control Panel. W2003 doesn't have a MMC for that, display and management is done via Explorer. A direct path to them is C:\WINDOWS\Tasks .
So will the script be something like this for W2K3?

@echo off
for %%C in (ksolpc01) do (
  schtasks /Create /F /S %%C /RU "System" /SC OnStart /TN StartSvc-SCHDLR /TR "C:\Windows\System32\sc.exe start """Jboss 4.2.3"" /0005:00
)

Another question is if I execute the .bat script, will it store that task in the C:\WINDOWS\Tasks? Or, will I have to create a scheduled task for it?

This script, if we remove the "/delay", is there no timer set for the service to start up after reboot?
/0005:00 needs to be removed, too, not only /DELAY ;-). There is no such feature for W2003 to allow to wait a certain time after reboot.

The batch file creates the task, and you can see that in the C:\Windows\Task folder.
Great! It works for W2k3.

One final question, then I won't bother you again. I just want to understand how that /0005:00 timer work for W2k8. Does that time start counting once the server is fully rebooted, and you get the "Ctrl+Alt+Del" prompt, or does it start counting as soon as you reboot and see the BIOS loading screen? Also, what will happen if you log in before the timer kicks in, and you go view the service, but it does not show started? If you refresh, will it then show started?
I don't know the exact timing, but technically the delay can start only from the time you see Windows booting, or later. I guess it starts before you see the login.

The service applet needs to be refreshed manually to see changes at any time.