Solved

stop a service when program runs and then restart after program stops

Posted on 2011-09-30
21
281 Views
Last Modified: 2012-06-27
Hi all i am trying to write a batch file (or similar) that will allow me to stop a troublesome service only whilst a particular program is run. here is what i have;
begin code:
@echo off
net stop imperoclientsvc
"C:\Documents and Settings\All Users\Desktop\SuccessMaker Enterprise.lnk"
net start imperoclientsvc
end code;

Now i know that all this does is stop the service, launch the program but then restart the program straight away. What i require is for the program to work with the service stopped but be restarted when i finish using the program. The other thing i should add is that it will be a student logging on and running the program so they will have restricted access, so if can't be done via a bacth file but could be done with some magic within the program shortcut icon that would be great too.

Please help if you can. As always thanks in advance.
0
Comment
Question by:Uraken007
  • 9
  • 7
  • 4
  • +1
21 Comments
 
LVL 11

Expert Comment

by:brutaldev
ID: 36890531
From the batch file you can start the program with the /wait option so it will only continue the batch when the app closes:
 
@echo off
net stop imperoclientsvc
start /wait "C:\Documents and Settings\All Users\Desktop\SuccessMaker Enterprise.lnk"
net start imperoclientsvc

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36890550
OK, what does the lnk file point to.  hat you need to do it:

@echo off
net stop imperoclientsvc
  "c:\program files\yourprogram.exe" parameters etc.
net start imperoclientsvc

or you can do:

START /WAIT "title to window" "c:\program files\yourprogram.exe" parameters etc.

The problem at the moment is that your are running the link I think.

If running the program with .exe specified directly does not wait still then maybe it stops the main exe and starts something else so the calling batch file does not know.

If so we can, perhaps, check for the existence of a file that is there when the program is running -- maybe it creates a temp file and then deletes it? Or we could check using tasklist that your exe is still running.

Give the easy option a try first!

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36890565
BTW the firs option will NOT work a LNK file still as it returns back straight away.  Also it will not work with "" around the filename (which is needed) with START as it has a quirk that the first bit in quotes is seen as a window title, so that:

start "c:\program files\whatever\somthing.exe"

actually starts a new cmd empty .exe window with that as the title, not run the exe.

Steve
0
 

Author Comment

by:Uraken007
ID: 36890684
Hi guys thanks fot the swift replies, steve your're right the first one does not work bu the second one does not either, i think the problem may be related to the contents of the shortcut file that we would
Ordinarily  click to start the program, the link (the details of which are included in the image file attached). I think i need to be able to include the 'start in ' box contents in the script for the actual application to launch, hence why i tried initially call the shortcut file in the batch file.

It would be useful if the LNK files had a box that allowed you to run coommands before and after....
ssm.bmp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36890772
Ok, sure then try:

@echo off
net stop imperoclientsvc
  cd /d "M:\resultsmanager\successmake\bin"
  "c:\resultsmanager\successmake\bin\login.exe"
net start imperoclientsvc
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36890784
I'm assuming there that the first line says login.exe on the end.  If it calls another batch file or VBS file then can we see that please?

thanks

Steve
0
 

Author Comment

by:Uraken007
ID: 36890805
The first line says:
  "C:\ResultsManager\SuccessMaker\Bin\LOGIN.EXE C:\ResultsManager\SuccessMaker\Launch.txt"

The second line says
"M:\resultsmanager\successmake\bin"
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 36891223
I would still use start to make sure the batch is waiting for termination of the exe called:
@echo off
net stop imperoclientsvc
start "Login" /wait /D M:\ResultsManager\SuccessMaker\bin LOGIN.EXE ..\Launch.txt
net start imperoclientsvc

Open in new window

0
 

Author Comment

by:Uraken007
ID: 36891313
This works brilliant thank you qlemo, now is there a way i can hide the script??? :)
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36891316
Think I missed a letter off in copy/paste of path before but what I did should be OK.  Just paste into Notepad, save As, all files, call it "runme.cmd" or whatever and off you go.

As has been said it MAY work better using START / WAIT but same sort of difference.

Likewise if neither of these wait then we need to know some process that sticks around in task manager, or a file to watch for.

Steve
@echo off
net stop imperoclientsvc
  cd /d "M:\ResultsManager\SuccessMaker\Bin"
  C:\ResultsManager\SuccessMaker\Bin\LOGIN.EXE C:\ResultsManager\SuccessMaker\Launch.txt
net start imperoclientsvc

Open in new window

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 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 36891344
Hmm, I did actually say that in http:#36890550 just before we knew the path!

if you add /MIN to beginning of the START command it will go minimized which is about the best you can do since you want it to carry on afterwards.
Is this a console application (i.e. in the text mode window) or a GUI one btw?  If it was a text mode one then add /B to the START command and it won't make an extra window.

Steve
@echo off  
net stop imperoclientsvc  
  cd /d "M:\ResultsManager\SuccessMaker\Bin"  
  START /MIN /WAIT "Please do not close" C:\ResultsManager\SuccessMaker\Bin\LOGIN.EXE C:\ResultsManager\SuccessMaker\Launch.txt
net start imperoclientsvc

Open in new window

0
 

Author Comment

by:Uraken007
ID: 36891437
thanks Steve my bad for not putting up the path to begin with i do have another problem now though, non-admin users can't stop services so the script won't workj unless they are admin unless i'm missing something?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36891552
No, you are not missing anything here. There are ways to grant users the privileges to start/stop a specific service (subinacls). Is setting that up on each computer for each user or group acceptable?
0
 

Author Comment

by:Uraken007
ID: 36891625
It could be doable we are only looking at about 20 pcs here..
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36891631
You can use runas or a scheduled task most easily:

runas /user:localadminuser /savecred c:\yourbatch.cmd
or
runas /user:domain\adminuser /savecred c:\yourbatch.cmd

And it will prompt to have password on first use and then save it.

The scheduled task way you can create a scheduled task to run the commands and then trigger it to run using

schtasks /run xxxxx

It is possible to fiddle with the permissions of an individual service if needed though.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36891659
Oops, crossed posts there sorry.  50:50 then really whether to run the script with more privleges or amend the service permissions.  The latter might be better frankly for multiple users/profiles etc.  Presume Qlemo will post syntax in a minute - was subinacl.exe resource kit Qlemo?  Have it on most of my older machines from setting owners etc. in the past but can;t remember where it was from specifically.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36891704
The Scheduled Task option is IMHO the best choice here. You can create the task remotely, using and providing a privileged account; you can bind the task to an event trigger or schedule, or trigger it manually as said before.

If we follow the subincals approach:
  subinacl /service imperoclientsvc /GRANT=CurrentAccount=TO
TO is start (T) and stop (O)
subinacls can be downloaded from http://www.microsoft.com/download/en/details.aspx?id=23510
Of course the command needs to be run by a privileged user, and you need to replace CurrentAccount with the user or group name, including the domain if required.
0
 

Author Comment

by:Uraken007
ID: 36891922
ok i will give that a go i think the original questioned has benn covered jointlky by you both is it fair enough if i split the award of points to the two of you?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36891959
If you do not want to decide which method to use, then yes, just split points as you see fit.
0
 

Author Closing Comment

by:Uraken007
ID: 36891981
You guys were excellent today,really very helpful thanks very much
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36891992
No problem either way.... Another time if you wish you can always ask a related question if an issues is solved but then a related one crops up.

Steve
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

21 Experts available now in Live!

Get 1:1 Help Now