Scheduled Task to Ensure that a Windows Service is Running

Some applications running under Windows services do not always remain in the running state, even when there is no apparent reason that they should stop. This is often due to a bug in the application, or perhaps the connection with the database that was being used by the application was temporarily broken.

In my case, a third-party application that I support (with no control over its source code) has a service that stops without warning to the user from time to time. Regardless of the reason as to why the stop control was sent by the application, the service needs to be running all the time. If this service were stopping due to a crash in the service, Windows has options in the services dialog box that can automatically restart a crashed service, or restart the server itself. These options do not help me, as it is the application sending a stop command to the service. I needed a simple way to ensure that the service is always running. The following batch file, using the actual Service Name (not the Display Name) of the service in place of “ServiceName”, will start the service if it is not running, and do nothing otherwise.

@ECHO OFF
FOR /F "tokens=3 delims=: " %%H IN ('SC QUERY "ServiceName" ^| FINDSTR "        STATE"') DO (
  IF /I "%%H" NEQ "RUNNING" (
   NET START "ServiceName"
  )
)

By saving this as a batch file and setting up a Scheduled Task to run the file on a regular interval such as every five or ten minutes, this will minimize the necessity of manually restarting the service when it is shut down unintentionally, that is, without the intention of the application administrator. The task should be disabled when one needs to have the service shut down for maintenance or troubleshooting.

Moving Multiple Scheduled Tasks into a Folder on Windows Server 2008

Let’s say you have a whole lot of scheduled tasks you want moved into a subfolder in the Task Scheduler.  Since you can’t just drag and drop the tasks, and must export the jobs into XML files to be imported, we will use similar commands as yesterday’s post.

For this, you will need an empty folder.  I use C:temp for this example.  If your folder does not already exist, create one.  Also, you will need a folder in the Task Scheduler GUI to exist.  Create it using the Task Scheduler GUI.

Export all jobs you want to move into XML files:

  1. Open the Command Prompt, and change to the C:WindowsTasks folder.
  2. Type the following command and press Enter: FOR /R . %F in (*.job) do schtasks /Query /TN “%~nF” /XML > “C:temp%~nF.xml”

This will create all XML files in the C:temp folder.  You can then delete from that folder any XMLs for jobs that you don’t want moved.

Next, delete the jobs in the original folder either using the command line or through the GUI.

Lastly, enter the following command and press Enter:

FOR /R c:temp %F in (*.xml) do schtasks /Create /S <<server name>> /RU <<username>> /RP <<password>> /XML “c:temp%~nF.xml” /TN “<<Task Scheduler folder name>>%~nF”

This will recreate the job files and put the tasks into the folder (in the GUI) of your choosing.

 

Importing Multiple Scheduled Tasks from Windows XP/Server 2003 to Windows 7/Server 2008

On Windows XP and Windows Server 2003, Scheduled Tasks are stored as binary files with the “.job” extension. They are also stored in this manner on Windows Server 2008. Also, the jobs are stored in the same location in both operating systems, namely, “C:WindowsTasks”. So if you want to copy jobs from one to the other, you should just be able to copy the files, right? No, that would be too easy. For whatever reason, Microsoft chose to make it much more difficult to migrate jobs than this.

To successfully import jobs, several steps are required.

First of all, copy all “.job” files on the XP/2003 box into a folder of your choosing on the 2008 box. I’ll create “C:temptasks” for this example.
Next, copy two files – schtasks.exe and schedsvc.dll – from C:WindowsSystem32 on the source box into the C:temptasks folder on the destination box.
Thirdly, copy (don’t move) the job files from the C:temptasks folder into the C:WindowsTasks folder on the destination box.

Now for the fun part. To import a job, the following command must be issued from the Command Prompt:
schtasks /change /TN <<Scheduled Job Name>> /RU <<Username>> /RP <<Password>>

This command will only import one job – the one with whatever name you put in the command.  If you have many jobs to import, this will not be practical.  You’ll need a script to do this, but fortunately the script is simple.

By using a for loop and a basic regular expression, this can be done at the command line:

c:temptasks>FOR /R . %F in (*.*) do schtasks /change /TN “%~nF” /RU <<Username>> /RP <<Password>>

This will import all of your job files into the Task Scheduler. The tasks may be in an enabled state, so be sure to check this if you don’t want them to run yet.