Skip to content

Instantly share code, notes, and snippets.

@alcides
Forked from tommorris/gist:145333
Created July 11, 2009 22:54
Show Gist options
  • Save alcides/145420 to your computer and use it in GitHub Desktop.
Save alcides/145420 to your computer and use it in GitHub Desktop.
I want to build a really simple script to use on Windows. I know
nothing about Windows programming.
Scenario:
I've got a shitty DSL line that doesn't have much bandwidth.
I want to prioritise certain applications. I've got some services
on my Linux box that I want to turn off when a user on a particular
Windows machine is running certain applications.
Plan:
I'd like to write a script that runs in Windows that monitors what
applications are running every so often and if certain apps *are*
running, it would trigger various commands which would be sent to
the Linux box.
I'm thinking of writing this in something like IronRuby.
Then all I'd have to do would be to poll the .NET framework's
System.Diagnostics.Process.GetProcesses() class method which
the documentation says should return an array of Process objects
which I could iterate through and match the title to the ProcessName
object property.
I was then planning to have it so that if it matches, the script
would trigger off a System.Net.HttpWebRequest instance to POST back
to the server a message saying that the app is running. Or maybe just
use the standard Ruby net/http class.
>> I'd go for the .NET version. Not everything that's expected in CRuby is available in IronRuby since they haven't reached 1.0. But I'm not sure about net/http.
I'm hoping to deploy this to a Windows XP machine. I'm not sure
what version of .NET is running on the machine.
Doesn't really matter unless is 1.0. 3.0 includes 2.0, so they are the same. 4.0 works also.
Questions:
1. Am I on the right track?
>> Yes, you could also do that in CRuby/CPython and ask that stuff via the win32 API, but that sucks. .NET is much better instead.
2. Is IronRuby a sensible choice (considering I use Ruby and JRuby
on Mac and Linux)? I can do IronPython if that's better. I haven't
really got time to learn C# or VB.NET, and I'd rather do this in
Vim than have to install Visual Studio on a borrowed Windows machine
(did I mention that I don't have a working Windows machine of my own?)
>> IronPython is in a more advanced state than IronRuby, but for what you need, any of them will work. (IronPython has more examples around, but they are easily converted to IronRuby).
3. Is there already an app that would do this without me having to bother?
>> Not that I know of
4. How do I package this up? Can I just have it floating around in, say,
C:\Documents and Settings\$username\script.rb (or whatever)?
>> Floating around, but in C:\Program Files\BandwithIronRubyScript\script.rb.
5. What's the .NET equivalent of crontab? I want to be able to set this
script to run every, say, ten or fifteen minutes. How would I do that
in Windows.
>> It's not .NET, but Windows. Control Panel - Administrative Tasks - Scheduled Tasks. And add it there. It's a GUI interface. I remember a CLI one, but that was not documented enough for me to get it to work.
Feel free to e-mail me any advice <[email protected]> or just 'fork'
this Gist. I'm rather looking forward to stepping outside my Mac/Linux
comfort zone and writing some code to run on Windows. And .NET looks like
it might not be a total trainwreck like the usual Windows stuff which I
am unfortunately familar with. ;) -tm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment