Automating the service restart from another server in Windows server 2016 I want to restart the services on Application server from DB server depending on the output of query.
Ex: In DB server i have written one procedure which will generate automated mail if no transaction inserted in one table.
If no transaction inserted from last 3 minutes , i want to restart the dependent service which is on another app server
Although you could restart the remote application service from T-SQL using a command invoked via xp_cmdshell, I suggest this be done with a PowerShell command instead.
The PowerShell script below calls a proc and restarts a service when the return code is 1, indicating no transactions were processed. This script can be included in a SQL Server Agent PowerShell job step running under an account with permissions needed to execute the proc and restart the remote service.
try {
    $connection = New-Object System.Data.SqlClient.SqlConnection(&quot;Integrated Security=SSPI&quot;)
    $command = New-Object System.Data.SqlClient.SqlCommand(&quot;YourDatabase.dbo.usp_CheckNoTransactions&quot;, $connection)
    $command.CommandType = [System.Data.CommandType]::StoredProcedure
    $returnValueParamter = $command.Parameters.Add(&quot;@ReturnValue&quot;, [System.Data.SqlDbType]::Int)
    $returnValueParamter.Direction = [System.Data.ParameterDirection]::ReturnValue
    $connection.Open()
    [void]$command.ExecuteNonQuery()
    $connection.Close()
    if($returnValueParamter.Value -eq 1) {
        Write-Output &quot;No transactions processed - restarting application service...&quot;
        Get-Service -ComputerName &quot;YourApplicationServer&quot; -Name &quot;YourAppServiceName&quot; | Restart-Service
    }
}
catch {
    throw
}

Sample proc:
CREATE PROC dbo.usp_CheckNoTransactions
AS
IF NOT EXISTS(SELECT 1 FROM dbo.Transactions WHERE CreateTimestamp &gt; DATEADD(minute, -3, GETDATE()))
BEGIN
    --application has stalled - send email and return 1 to indicate a service restart is needed
    EXEC msdb.dbo.sp_send_dbmail ...
    RETURN 1;
END;
GO