Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

create a script or batch file to stop a service, execute an exe, then start the service

Posted on 2007-03-22
13
374 Views
Last Modified: 2010-04-16
I need to create a batch or script that does the following...

STOP a service

Launch an EXECUTABLE

Then RESTART the service backup but ONLY AFTER the EXE has been exited / closed out

I remember doing this years ago but I just can not for the life of me remember how I accomplished the task.
0
Comment
Question by:bigricky
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 69

Expert Comment

by:Qlemo
ID: 18774139
Problem here is "waiting for exe to finish" part. Whcih OS should the batch run on?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18774189
You start the service then do something like this:

Start you program. Make a note of the name. That's what you'll check in the for statement below.

:CHECKAGAIN

REM ** Will want to pause here. You can download a sleep utililty or use ping

for /f %%a in ('tasklist ^| findstr program.exe') do goto CHECKAGAIN

restart your service.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18774191
Modify the for statement to do a case insensitive match:

for /f %%a in ('tasklist ^| findstr /i program.exe') do goto CHECKAGAIN
0
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
LVL 1

Author Comment

by:bigricky
ID: 18774247
XP Professional
0
 
LVL 2

Expert Comment

by:ksmith247
ID: 18774266
If you know how long it takes the exe to run you could use 'sleep' to pause the script for 'n' number of seconds before proceeding.

example:

net stop <service name>
c:\someexecutable.exe
sleep 60
net start <service name>

this will stop the service, run the exe, wait for 60 seconds then start the service.

Now if the exe takes longer than the sleep time that could be a problem. I can not think of a way in a BAT file to perform a ProcessWaitClose type of action.
0
 
LVL 1

Author Comment

by:bigricky
ID: 18774297
the exe is a user program like word or excel for example, where the time of use is really an unknown.  the idea is that as they close out the program, the service fires and starts back up.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 18774368
You do not need a for for the loop, just something like

net stop ....
:loop
tasklist | findstr /i program.exe >nul && goto loop
net start ...
0
 
LVL 1

Author Comment

by:bigricky
ID: 18774458
another idea I'm thiking about as I google this too... is to create a service for the exe.  then make the service that I am stopping and starting dependent on the newly created service being stopped.  my concern though is, does the newly created service actually stop when the program would be closed.
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 50 total points
ID: 18774505
Why not simply

net stop servicename
start /wait c:\yourapp.exe
net start servicename

Execution remains on line #2 until that app is closed... : )
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 18774997
@sirbounty:
that does not always work, for example when using
start "" "c:\doc\doc.doc"

But with winword.exe or excel.exe a start /wait is ok.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18775139
Works fine for me when calling an xls spreadsheet...can't see why it'd be different with a doc...
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 18775610
sirbountry, you're right, must have been some typo when I was testing ... I tried different cases, and it worked always. But I'm sure there are cases when start /wait would not work ... (e.g. using psexec :-) )
0
 
LVL 1

Author Comment

by:bigricky
ID: 18884830
/wait command did exactly what I needed... thank you!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Event ID 1054 Userenv 2 33
LOGINSERVER and nltest /dsgetdc 3 40
Batch File- Finding Drive Description 11 19
2008 R2 export VM includes VHD 1 6
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

860 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