-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
68 lines (59 loc) · 2.07 KB
/
Copy pathProgram.cs
File metadata and controls
68 lines (59 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
namespace RestartJenkins
{
class Program
{
static void Main()
{
var protectedProcessList = new List<string>
{
"nunit-agent-x86",
"nunit-agent",
"nunit-console-x86",
"nunit-console",
"accurev",
"nuget",
"msbuild",
"cmd",
"ccrewrite"
};
var processlist = Process.GetProcesses();
foreach (var theprocess in processlist.Where(theprocess => protectedProcessList.Contains(theprocess.ProcessName.ToLower())))
{
Console.WriteLine("{0} is running.", theprocess.ProcessName);
Environment.Exit(1);
}
Console.WriteLine("Restarting Jenkins");
RestartService("Jenkins", 500);
}
/// <summary>
/// Method taken from http://www.csharp-examples.net/restart-windows-service/
/// Author: Jan Slama, 08-May-2008
/// </summary>
/// <param name="serviceName"></param>
/// <param name="timeoutMilliseconds"></param>
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
var service = new ServiceController(serviceName);
try
{
var millisec1 = Environment.TickCount;
var timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// count the rest of the timeout
var millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2 - millisec1));
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
}
}
}
}