Stop, Start and Restart a Windows Service with UrbanCode Deploy

In this post I’m going to show you how to create a Generic uDeploy Process to manage Windows Services and how to use this Process as Template for Application Processes.

What we need is:

Below the Generic Process Windows Service Stop/Start/Restart flow:

For this process I created two Process Properties:

  • action
  • service.name

see fig. below:

I used some of the following “Service Control Manager Automation Plugin” steps:

  • Check If Service Exists
  • Stop Service
  • Start Service
  • Check Service Status

For each of these steps I used the property: ${p:service.name}

The first step checks if Service exists.

Then, the switch step is used to create a branch in the process, based on the value of the “action” property that can be: stop, start or restart.

If the “action” value is stop or restart the Stop Service will be executed, otherwise Start Service will run.

Another Switch step is added to create another branch between stop or restart action.

For the second step I added a Post Processing Script, called “Check Stopped Service”

Post-processing scripts must set the Status property to specify the status of the script.

scanner.register("Service is stopped.", function(lineNumber, line) {
    var value = line.replace("Service is stopped.", "0");
    properties.put("Value", value);
});

scanner.scan();

var exit = properties.get('Value');


if (exit == 0) {
    properties.put('Status', 'Success');
    commandOut.println("Service is Stopped.")
}
else {
     properties.put('Status', 'Failure');
}

The post-processing script examines the Check Service Status step’s output log and run commands based on the result. In the above code, scanner.register() registers the strings “Serviice is stopped”, then runs commands if this strings is found.

So, in case of Service Stopped the Status property is set to Success and the step will succeed accordingly.

Finally we can use this Generic Process for an Application.

Suppose we have a Windows Application who needs a Stop/Start Apache before deployment of some artifacts.

We create a component and three Component Processes:

Stop Apache
Start Apache
Restart Apache

Each process has a single step: Run Generic Process like in fig . below, where “Stop Apache” is shown:

Then we have our Windows Application:

and our Application Process: Deploy Application

who has Stop Apache, deploy war and Start Apache:

The Generic Process: Windows Service Stop/Start/Restart is created once and is available for all applications who need it.

Cheers!

Operations Engineer working with Cloud Infrastructure, Containers, Kubernetes, CI/CD pipelines, CDRA processes, GitOps.