Link to home
Start Free TrialLog in
Avatar of Uraken007
Uraken007

asked on

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

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.
Avatar of brutaldev
brutaldev
Flag of South Africa image

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

Avatar of Steve Knight
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
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
Avatar of Uraken007
Uraken007

ASKER

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
Ok, sure then try:

@echo off
net stop imperoclientsvc
  cd /d "M:\resultsmanager\successmake\bin"
  "c:\resultsmanager\successmake\bin\login.exe"
net start imperoclientsvc
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
The first line says:
  "C:\ResultsManager\SuccessMaker\Bin\LOGIN.EXE C:\ResultsManager\SuccessMaker\Launch.txt"

The second line says
"M:\resultsmanager\successmake\bin"
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works brilliant thank you qlemo, now is there a way i can hide the script??? :)
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

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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?
It could be doable we are only looking at about 20 pcs here..
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
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.
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.
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?
If you do not want to decide which method to use, then yes, just split points as you see fit.
You guys were excellent today,really very helpful thanks very much
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