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

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.
LVL 1
bigrickyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Problem here is "waiting for exe to finish" part. Whcih OS should the batch run on?
0
SteveGTRCommented:
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
SteveGTRCommented:
Modify the for statement to do a case insensitive match:

for /f %%a in ('tasklist ^| findstr /i program.exe') do goto CHECKAGAIN
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

bigrickyAuthor Commented:
XP Professional
0
ksmith247Commented:
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
bigrickyAuthor Commented:
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
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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
bigrickyAuthor Commented:
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
sirbountyCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
@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
sirbountyCommented:
Works fine for me when calling an xls spreadsheet...can't see why it'd be different with a doc...
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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
bigrickyAuthor Commented:
/wait command did exactly what I needed... thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.