fully control windows service by Perl + sc.exe

Q: how do I monitoring my "Oracle*Report*ABC" service on windows box?
A: user Perl and sc.exe from windows resource kit is good idea.

You can use Win32::Service module of course, however it only allow you to query/start/stop windows service. Fortunately windows give us sc.exe (service control) to Create, Start, Stop, Query or Delete any Windows. And then? use Perl as wrapper to page you when it goes down (yes, it would failed and stopped there even if it was set ‘retry after failure’ in some case). That is the page for.
Keep it simple:
foreach (sort keys %$win32_services){
    $output=`sc query "$win32_services->{$_}"`;
    $up_down=($output=~/state.*?running/i);

Want to create/delete windows service? full command reference is in ss64 website.
Syntax
      SC [\server] [command] [service_name] [Options]
Some useful command in SC
          query         Show status
          start          START a service.
          stop           STOP a service
          pause          PAUSE a service.
          create         Create a service. (add it to the registry)
          delete         Delete a service (from the registry)

Important Note
the service name is NOT ALWAYS the same as display name, which you see in windows service list 🙂

Solution 1
– run  SC GetKeyName <DisplayName> to get the real service name from windows registry
– run SC against the real service name

Solution 2
– if you are familiar with windows registry, run regedit and go to HKEY_LOCAL_MACHINESYSTEMControlSet***Services,  get it there.

Resource kit is the key to figure out who is your serious windows SA. 🙂

Advertisements

About Pang

Oracle DBA working in database, fusion middleware and identity management and cloud.
This entry was posted in Perl. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s