Overclockers Australia Forums

OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 19th July 2012, 1:36 PM   #16
f3n1x
Member
 
f3n1x's Avatar
 
Join Date: Mar 2003
Location: Armadale, Melbourne
Posts: 1,653
Default

Quote:
Originally Posted by mp2031 View Post
ok it seems that the application will only restart the process if ended.. & runnning deadline as a service you must be on a domain both of these options are no good

could anyone tell me what is wrong with my script?

thanks in advance!

miles
Batch scripting is very clunky way to handle applications in modern windows OS, especially services, i would recommend you look into doing it with C#, you can very easily do this with C# express and probably with less code than with a batch script.

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);
            }

                        
                    
        }

    }
}
Then just compile and run with the process name as the parameter.
__________________
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.
f3n1x is offline   Reply With Quote

Join OCAU to remove this ad!
Old 19th July 2012, 10:13 PM   #17
Tekin
Member
 
Tekin's Avatar
 
Join Date: Nov 2002
Location: Elsewhere.
Posts: 3,635
Default

Quote:
Originally Posted by f3n1x View Post
Batch scripting is very clunky way to handle applications in modern windows OS, especially services, i would recommend you look into doing it with C#, you can very easily do this with C# express and probably with less code than with a batch script.
Hating on batch scripts now?? Code of the gods my friend!

Nah - what you've written looks elegant. C# is certainly more efficient then batch script.
Tekin is online now   Reply With Quote
Old 20th July 2012, 10:34 AM   #18
f3n1x
Member
 
f3n1x's Avatar
 
Join Date: Mar 2003
Location: Armadale, Melbourne
Posts: 1,653
Default

Quote:
Originally Posted by Tekin View Post
Hating on batch scripts now?? Code of the gods my friend!

Nah - what you've written looks elegant. C# is certainly more efficient then batch script.
I love batch/shell scripts but they simply don't suit the modern windows environment like they once did.

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.
f3n1x is offline   Reply With Quote
Old 20th July 2012, 8:45 PM   #19
PabloEscobar
Member
 
Join Date: Jan 2008
Posts: 2,816
Default

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.
PabloEscobar is online now   Reply With Quote
Old 24th July 2012, 5:59 PM   #20
Foliage
Member
 
Foliage's Avatar
 
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,657
Default

Quote:
Originally Posted by PabloEscobar View Post
I don't like this as a test.

Many times services will hang without becoming "non responsive".
Just put a timer on it, if it is non responsive for 45 seconds then restart it.

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.
Foliage is offline   Reply With Quote
Old 25th July 2012, 9:10 AM   #21
PabloEscobar
Member
 
Join Date: Jan 2008
Posts: 2,816
Default

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?
PabloEscobar is online now   Reply With Quote
Old 25th July 2012, 3:20 PM   #22
Foliage
Member
 
Foliage's Avatar
 
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,657
Default

Quote:
Originally Posted by PabloEscobar View Post
Where the application has hung, but it is not showing as not-responsive?
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.
Foliage is offline   Reply With Quote
Old 25th July 2012, 6:15 PM   #23
PabloEscobar
Member
 
Join Date: Jan 2008
Posts: 2,816
Default

Quote:
Originally Posted by Foliage View Post
Depending on the application it may not be possible to test that.
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.
PabloEscobar is online now   Reply With Quote
Old 25th July 2012, 11:39 PM   #24
f3n1x
Member
 
f3n1x's Avatar
 
Join Date: Mar 2003
Location: Armadale, Melbourne
Posts: 1,653
Default

Quote:
Originally Posted by PabloEscobar View Post
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.
Well, you could spend all day trying to convince the op to do that approach or you could spend 15 minutes writing up a hundred odd lines of code to do what he thinks he wants, and let him come to said realisation on his own.

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.
f3n1x is offline   Reply With Quote
Old 25th July 2012, 11:57 PM   #25
Foliage
Member
 
Foliage's Avatar
 
Join Date: Jan 2002
Location: Sleepwithyourdadelaide
Posts: 23,657
Default

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.
Foliage is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 3:00 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!