Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We're mostly a Linux shop, but we have a print server running Windows Server 2008, and use an application called Print Helper to print invoices. I need to find a way to check that this application is running, and automatically restart it if it's not.

On Linux I'd probably do this with a small shell script and a cron job, but I'm not sure how to accomplish this on Windows Server. I'm fairly confident I could do it in Perl using Proc::Background, but I'm reluctant to install Perl just for one script, and while I'm sure it can be done using something like PowerShell, it's not really a worthwhile use of my time to learn PowerShell for one small task. Tasklist seems to do some of what I want in that it can let you know if a specific process is running or not, but I'm not sure how I could go from there to automatically restarting the application if it's crashed.

Any help gratefully received!

share|improve this question
    
IMO. It's worth it to learn powershell. But it depends on your available time. Actual answer below. –  Joseph Kern Oct 20 '11 at 13:18

3 Answers 3

up vote 5 down vote accepted

Wow. I just answered a different question.

What you should do is "daemonize" the Print Helper process by using sc.exe or srvany.exe (which I believe is depreciated). Then you can utilize the builtin service "Recovery" options to handle failure events (including, I believe, executing a script, sending an snmp trap, sending an email, and, of course, restarting the service).

As given:

sc create printhelper binpath= "c:\program files\Print Helper\phelper.exe" start= auto depend= Spooler/lanmanserver DisplayName= "Print Helper"

This will create a service named printerhelper, with a display name of Print Helper, executing "c:\program files\Print Helper\phelper.exe" automatically, with the dependencies of the Print Spooler and SMB/CIFS Server service, running as NT AUTHORITY\SYSTEM builtin user.

share|improve this answer
1  
Yeah srvany shouldn't be used for at least 2008 or greater. –  Joseph Kern Oct 20 '11 at 13:25
    
That seems like the best solution, but I can't seem to get it to work for some reason. I've tried entering the following in the terminal: sc create printhelper binpath="C:\Program Files (x86)\Print Helper\PrintHelper.exe" start=auto DisplayName="Print Helper" but it doesn't get accepted - I get the usage and options back, suggesting that I may have gotten the syntax wrong, but I can't see anything wrong with what I've entered. –  mattbd Oct 20 '11 at 13:49
1  
In examples, there is a space between the option and it's argument as listed above (weird I know). Try that and let us know. –  mbrownnyc Oct 20 '11 at 14:29
    
@mbrownnyc That's done it - there was no space in start=auto, but entering it as start= auto did the trick. Thanks very much! –  mattbd Oct 20 '11 at 14:35
    
No problem. Feel free to accept my answer with the shiny green check under the up/down vote piece next to my answer. –  mbrownnyc Oct 20 '11 at 14:39

It sounds like you want to spend as little time as possible on this task. We all hate print servers equally. Using only the preferences built into Windows you can create a service, automatically recover the service, and implement rudimentary monitoring of the service.

Step 1 Create a Service

(You can skip this step if there's already a service entry in the Services control panel)

Use sc.exe to create a new service.

sc.exe create PrintHelper start=auto binPath="C:\<Print Helper Path and Flags>" DisplayName="Print Helper"

Step 2 Configure your service

In the services control panel right click on your new service select properties and choose the Recovery tab. Configure as needed.

There are many options here, what to do on the first failure, second failure, you can even run another application on failure.

Step 3 Monitor Your Service

Either configure the Event Viewer to send an email on the specific event failures for Print Helper (find the event right click on it, Select Attach Task for this event).

Or configure the Service to send an email on failure, using an external program as the recovery mechanism.

Disclaimer

Of course I would recomend using something like nagios or SCOM for a real monitoring solution. But that's an entirely new problem in itself.

share|improve this answer

If you're using Nagios, you can do it with NSClient++ addon.

On monitoring server, define a service:

define service{
   use                 generic-printer
   host_name           hostname
   service_description appname
   check_command       check_nt!PROCSTATE!-d SHOWALL -l appname.exe
   contact_groups      admin-sms
   event_handler       autostart_appname!hostname
   }

The autostart_appname is defined in commands.cfg:

define command { 
    command_name    autostart_appname
    command_line    $USER1$/eventhandlers/autostart_appname.sh $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$ $HOSTADDRESS$
}

The event handler script autostart_appname.sh:

#!/bin/sh

HOSTADDRESS=$4

case "$1" in
OK)
    ;;
WARNING)
    ;;
UNKNOWN)
    ;;
CRITICAL)
    case "$2" in
    SOFT)
    ;;  
    HARD)
        /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c autostart_appname
        ;;
    esac
    ;;
esac
exit 0

On the Windows server, define a command in NSC.ini:

[NRPE Handlers]
autostart_appname=C:\Program Files\NSClient++\scripts\autostart_appname.cmd

and batch script is simple like that:

net start "Application Name"
share|improve this answer
    
Where "Application Name" is the Display Name for a define service. Or your batch can just execute the EXE. –  mbrownnyc Oct 20 '11 at 12:58
    
I was tempted to use Nagios, but at present it would be overkill - we only have a handful of servers and just the one running Windows Server –  mattbd Oct 20 '11 at 13:55
    
@mbrownnyc: yes, it's the display name. It can monitor any service in the services.msc list. –  quanta Oct 20 '11 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.