![]() |
![]() OCAU News - Wiki - QuickLinks - Pix - Sponsors |
|
|||||||
| Notices |
|
Sign up for a free OCAU account and this ad will go away! Search our forums with Google: |
![]() |
|
|
Thread Tools |
|
|
#16 | |
|
Member
Join Date: Mar 2003
Location: Armadale, Melbourne
Posts: 1,653
|
Quote:
http://msdn.microsoft.com/en-us/libr...s.process.aspx This is the relevant section. In fact it's so easy i took the liberty: To use this, download c# express create a console app and paste this into program.cs ###Disclaimer### This is somewhat untested, don't go running in a production environment without at least doing a few tests. Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace RestartIfUnresponsive
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1) { Console.WriteLine("Usage: restartifunresponsive <processname>."); Environment.Exit(1); }
bool ExitPress = false;
Console.WriteLine("Press Space to Exit...");
Timer IntervalT = new Timer(Tick, args[0], 0, 900000); //15 minutes *60*1000
while (!ExitPress)
{
ConsoleKeyInfo KeyPress = Console.ReadKey();
if (KeyPress.KeyChar == ' ')
{
ExitPress = true;
}
}
}
private static void Tick(Object state)
{
Console.WriteLine("Checking Status of " + state.ToString());
Process[] TargetProc = Process.GetProcessesByName(state.ToString());
if (TargetProc.Length > 0)
{
if (TargetProc[0].Responding)
{
Console.WriteLine(state.ToString() + " is Responding.");
}
else
{
Console.WriteLine(state.ToString() + " is not Responding.");
ProcessStartInfo ProcRestart = TargetProc[0].StartInfo;
TargetProc[0].Kill();
Process.Start(ProcRestart);
}
}
else
{
Console.WriteLine("No Processes found matching " + state.ToString());
Environment.Exit(2);
}
}
}
}
__________________
f3n.org|systems admin, graphics & foss software Canon Eos 40D. Canon 50mm f1.8. Tamron SP AF28-75mm F/2.8 XR Di LD Aspherical (IF). Sigma EF 500 DG Super ETTL Flash(Broken! :/) Last edited by f3n1x; 19th July 2012 at 1:47 PM. |
|
|
|
|
| Join OCAU to remove this ad! |
|
|
#17 | |
|
Member
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,635
|
Quote:
Code of the gods my friend! Nah - what you've written looks elegant. C# is certainly more efficient then batch script. |
|
|
|
|
|
|
#18 | |
|
Member
Join Date: Mar 2003
Location: Armadale, Melbourne
Posts: 1,653
|
Quote:
The argument could be made that powershell is just as good as c# at this type of thing, but Im not very familiar with PS, so my decision was easy. I am however very familiar with bash under *nix, and it in the right hands is extremely powerful, because it's designed for that environment, and it's designed to do specific types of tasks, and designed to do them the unix way. Edit: On a side note, if i was to sit down and properly design that program, i'd have it just run, check if the program is responsive, and restart if required and quit, and design it to be launched from scheduled tasks, that seems more efficient.
__________________
f3n.org|systems admin, graphics & foss software Canon Eos 40D. Canon 50mm f1.8. Tamron SP AF28-75mm F/2.8 XR Di LD Aspherical (IF). Sigma EF 500 DG Super ETTL Flash(Broken! :/) Last edited by f3n1x; 20th July 2012 at 11:26 AM. |
|
|
|
|
|
|
#19 |
|
Member
Join Date: Jan 2008
Posts: 2,816
|
I don't like this as a test.
Many times services will hang without becoming "non responsive" Or, a service will become non responsive for a short period of time, while it is waiting for resources or a lock or similar as part of "normal" operation. I much prefer writing specific tests that test required functionality. ie. If I was checking if the exhange smtp server was up, rather than just looking at the service, I'd have a script that sent a test e-mail somewhere, and then checked that it arrived. It's not possible in all cases, but in many cases, with a bit of thought and googling, you can normally find a way. |
|
|
|
|
|
#20 | |
|
Member
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,657
|
Quote:
edit: Here we go, now waits 45 seconds for the program to become responsive. Code:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace RestartIfUnresponsive
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1) { Console.WriteLine("Usage: restartifunresponsive <processname>."); Environment.Exit(1); }
bool ExitPress = false;
Console.WriteLine("Press Space to Exit...");
Timer IntervalT = new Timer(Tick, args[0], 0, 900000); //15 minutes *60*1000
while (!ExitPress)
{
ConsoleKeyInfo KeyPress = Console.ReadKey();
if (KeyPress.KeyChar == ' ')
{
ExitPress = true;
}
}
}
private static void Tick(Object state)
{
Console.WriteLine("Checking Status of " + state.ToString());
Process[] TargetProc = Process.GetProcessesByName(state.ToString());
if (TargetProc.Length > 0)
{
if (TargetProc[0].Responding)
{
Console.WriteLine(state.ToString() + " is Responding.");
}
else
{
Stopwatch timer = new Stopwatch();
timer.Start();
Console.WriteLine(state.ToString() + " is not Responding, waiting for 45 seconds");
while(timer.ElapsedMilliseconds < 4500)
{
Thread.Sleep(100);
if(TargetProc[0].Responding)
{
Console.WriteLine(state.ToString() + " became responsive.");
return;
}
}
Console.WriteLine(state.ToString() + " did not respond after 45 second, restarting.");
ProcessStartInfo ProcRestart = TargetProc[0].StartInfo;
TargetProc[0].Kill();
Process.Start(ProcRestart);
}
}
else
{
Console.WriteLine("No Processes found matching " + state.ToString());
Environment.Exit(2);
}
}
}
__________________
I like to construct strong views on random things, and then argue for absolutely no reason about them. |
|
|
|
|
|
|
#21 |
|
Member
Join Date: Jan 2008
Posts: 2,816
|
So that covers false positives (where the application has not hung, but is showing unresponsive).
What about false negatives. Where the application has hung, but it is not showing as not-responsive? |
|
|
|
|
|
#22 |
|
Member
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,657
|
Depending on the application it may not be possible to test that.
__________________
I like to construct strong views on random things, and then argue for absolutely no reason about them. |
|
|
|
|
|
#23 |
|
Member
Join Date: Jan 2008
Posts: 2,816
|
If it does something, in response to something else, it should be possible to test it. But it's basically my point... You are better off writing test cases for real world scenarios than checking for Not-Responsive.
|
|
|
|
|
|
#24 | |
|
Member
Join Date: Mar 2003
Location: Armadale, Melbourne
Posts: 1,653
|
Quote:
People tend to be more open to things they think are their ideas, even if they've been spoon fed them. (Social Engineering 101 )
__________________
f3n.org|systems admin, graphics & foss software Canon Eos 40D. Canon 50mm f1.8. Tamron SP AF28-75mm F/2.8 XR Di LD Aspherical (IF). Sigma EF 500 DG Super ETTL Flash(Broken! :/) Last edited by f3n1x; 25th July 2012 at 11:41 PM. |
|
|
|
|
|
|
#25 |
|
Member
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,657
|
OP what is the program? there might be a way to do what pablo suggested.
__________________
I like to construct strong views on random things, and then argue for absolutely no reason about them. |
|
|
|
![]() |
| Bookmarks |
|
Sign up for a free OCAU account and this ad will go away! |
| Thread Tools | |
|
|