What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Can I automatically restart a Windows service with PRTG?

Votes:

0

I would like to remotely restart a Windows service on a target machine in the network of my PRTG core server. How can I do this automatically?

automation exe-notification notfication restart service windows wmi

Created on Sep 3, 2012 11:45:11 AM by  Daniel Zobel [Product Manager]

Last change on Sep 3, 2012 12:05:11 PM by  Daniel Zobel [Product Manager]



7 Replies

Accepted Answer

Votes:

0

This article applies to PRTG Network Monitor 12 or later

Automatically Restart Windows Services

This functionality is not build into PRTG by default, but you can achieve an automatic Windows service restart by using a custom script in combination with PRTG's Execute Program notification.

  1. Write a script that is capable of restarting the desired Windows service on the target computer.
    • Adapt the script to suit your needs. You can also use placeholders. For details, please see What placeholders can I use with PRTG?
    • Note: Notification scripts can only be executed on the machine running the PRTG core server, never on a Remote Probe!
  2. Save the script to the Notifications\EXE sub folder of your PRTG program directory. Make sure the Windows user account running your PRTG core server has sufficient rights to execute the script. By default, this is the "System" local Windows user account.
  3. In PRTG, create a new notification (see PRTG Manual: Account Settings—Notifications). Enable the Execute Program section and select your newly saved script. Save your settings.
  4. (Optional) Test the notification by clicking on the Test link in the list of notifications.
  5. Now you need to set a notification trigger to execute the script on a certain condition. For example, navigate to Notifications tab of a sensor, then add a trigger for the status condition Down which executes your "Execute Program" notification. The script will then be executed if the sensor changes to a red "down" status. For details about notifications triggers, please see PRTG Manual: Sensor Notifications Settings

See Also

Created on Sep 3, 2012 12:03:55 PM by  Daniel Zobel [Product Manager]

Last change on Apr 6, 2017 6:56:07 AM by  Luciano Lingnau [Paessler]



Votes:

0

Under step 1, stopping and starting a service can also be done with a simple batch file using the SC command.

@echo off
rem stop service
SC \\ServerName stop ServiceName > nul
rem wait 3 seconds
ping 1.1.1.1 -n 1 -w 3000 > nul
rem start service
SC \\ServerName start ServiceName > nul

Created on Sep 4, 2012 12:38:04 PM



Votes:

0

I believe this post has now been superseded, as within a 'wmiservicesensor' you can now choose two options under "If Service is Not Running"? - namely are:

"Start/Restart Service" "Do Nothing"

I assume this means it will do what it says on the tin? (i.e. restart the service), but I wondered what the frequency of the attempted restarts are? / how you can get notified of restarts? etc.

Sorry if this is the wrong place to put this question, but I can't find any information on this new feature within the product.

Any help appreciated!

Created on Jun 12, 2013 3:50:33 PM



Votes:

0

That is correct, it will try to restart the service. The sensor will try to restart the service once. A notification would be triggered by defining a trigger for the "When condition clears..." entry (seeing as the sensor would switch from an error to an up state).

Created on Jun 14, 2013 12:53:15 PM by  Patrick Hutter [Paessler Support] (7,225) 3 3



Votes:

0

Hi,

We would like to restart a Windows Service based on a specific criteria; ex. high memory consumption. We've created the powershell script to be used as a custom notification with parameters %host and %ServiceDisplayName%, but we can't figure out the placeholder that refers to the Service Display Name as detected by the WMI Service Monitor.

As a reference:

# Restart Windows Service
# 
# The Parameter section consists of 2 parameters: ComputerName, DisplayName
# 

if ($Args.Count -lt 2)
{

    Write-Host "Not enough Arguments. ComputerName and DisplayName must be specified."
    exit 1;
}
elseif ($Args.Count -eq 2)
{

    $service = Get-Service -ComputerName $Args[0] -DisplayName $Args[1] -ErrorAction SilentlyContinue

    if ($service.Length -gt 0)
    {
        # Service exists
        Try
        {
            (get-service -ComputerName $Args[0] -DisplayName $Args[1]).Stop()
            (get-service -ComputerName $Args[0] -DisplayName $Args[1]) | Set-Service -Status Running
            exit 0;
        }
        Catch
        {
            exit 2;
        }
    } 
    else
    {
        Write-Host "Service not found"
        exit 2;
    }
} 
else
{
    Write-Host "Too many arguments."
    exit 2;
}

Created on Jun 8, 2015 3:20:23 PM

Last change on Apr 5, 2017 1:49:24 PM by  Luciano Lingnau [Paessler]



Votes:

0

Simply put the friendly name in the sensor comments and use %comments as the placeholder :)

Created on Jun 9, 2015 7:48:33 AM by  Stephan Linke [Paessler Support]



Votes:

0

Here's a different take on the Powershell variant, this does a remote connection via Invoke-Command and runs the other commands locally. The advantage of this is approach is that you can provide credentials when starting the Invoke-Command.

# Restart Service on Remote Computer
# $server - The server on which the server should be restarted, use FQDN for reliability: 'server.domain.tld'
# $account - The account to be used to connect via powershell remoting, use 'domain.tld\account' format
# $password - The password of the account
# $serviceName - The displayname of the Service, For instance 'SNMP Service'
# Parameters in PRTG can be: -server 'server.contoso.com' -account 'contoso.com\john.doe' -password 'mysafepassword' -serviceName 'PRTG Probe Service'

param(
    [string]$server = 'server.contoso.com',
    [string]$account = 'contoso.com\john.doe',
    [string]$password = 'mysafepassword',
    [string]$serviceName = 'PRTG Probe Service'
)

$pass = ConvertTo-SecureString -AsPlainText $password -Force
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList $account,$pass

Invoke-Command -ComputerName $server -Credential $credential -ArgumentList $serviceName -ScriptBlock {

    param(
    [string]$serviceName
    )

    try {
        Stop-Service -DisplayName $serviceName -WarningAction SilentlyContinue
        Write-Output "Stop service $($serviceName): OK"
    }catch{
		Write-Output "Stop service $($serviceName): Failed - $($_.Exception.Message)"
	}

    try {
        Start-Service -DisplayName $serviceName -WarningAction SilentlyContinue
        Write-Output "Start service $($serviceName): OK"
    }catch{
		Write-Output "Start service $($serviceName): Failed - $($_.Exception.Message)"
	}
Exit 0
}

Created on Apr 6, 2017 6:45:20 AM by  Luciano Lingnau [Paessler]

Last change on Apr 6, 2017 6:55:26 AM by  Luciano Lingnau [Paessler]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.