Solved

bat/script file that checks for running process

Posted on 2013-01-09
6
776 Views
Last Modified: 2013-01-24
I am a linux admin so please bare with me....

I have a Windows Server 2003 system that has been dumped in my lap until we can replace our Windows guy who recently left. This server runs a very specific application that occasionally crashes.  The problem is when this process stops it stops updating a fairly important SQL database.

What I need is a bat file, vbs script of some sort that will check if the process is running and take an action if it is not.

I would LOVE if it could send me an email if the process is not running.  This is very easy using linux but I dont know if it is as easy on Windows.  If that is not possible maybe it could just restart the process?
0
Comment
Question by:savone
6 Comments
 
LVL 1

Expert Comment

by:sarconastic
Comment Utility
I found this script, which might be at least part of what your doing. I have used the tasklist command at the top before, it work nice to print a report of running tasks.

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FINDSTR notepad.exe search.log > found.log

FOR /F %%A IN (found.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log
del found.log

Open in new window


I am sure someone could come up with code to drop an email provided you have an email smtp server available.
0
 
LVL 23

Author Comment

by:savone
Comment Utility
I do have an available SMTP server.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 333 total points
Comment Utility
You can shorten up the checking a bit with something like this:

tasklist /FI "IMAGENAME eq notepad.exe" | find /i "notepad" >NUL && set Found=Y || set Found=N
if "%Found% == "N" (
   REM perform logic here when not running
)

Open in new window

You can use BLAT to send an email from a BAT file, check out these links for the EXE and examples:

http://www.blat.net/
http://scripts.dragon-it.co.uk/links/email-from-batch?OpenDocument

~bp
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Assisted Solution

by:tighec
tighec earned 167 total points
Comment Utility
Slightly condensed to what is above;  tasklist combined with findstr searched for the process.
ErrorLevel is the return code, if findstr finds the process (in this case notepad.exe) it returns success (0), otherwise it returns 1.  If it reutns 1 then you can just start the server and take any other action you need, write to lo, send email etc.

tasklist |findstr "notepad.exe"
If %ERRORLEVEL% == 1 START Notepad.exe
0
 
LVL 51

Assisted Solution

by:Bill Prew
Bill Prew earned 333 total points
Comment Utility
Or even shorter as:

tasklist | findstr "notepad.exe" > NUL || start notepad

The downside of that is that if there is a task runing called mynotepad.exe it will think notepad.exe is running, which isn't desirable.  That's what I use the search capability of the tasklist command itself to find exactly the task we want by it's name.

~bp
0
 
LVL 23

Author Closing Comment

by:savone
Comment Utility
Not a perfect answer, but I arrived where I needed to be.

Thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
Learn about cloud computing and its benefits for small business owners.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now