Stop, wait, and start service using a scheduled task in Windows 7

Hello all,

I need a script that I can schedule to run at the same time everyday that will Stop a service, wait an hour, then restart the service again.  I'm used to DOS batch scripts in WinXP.  Win7 has a level of security I can't seem to get through.

The goal will be to Stop CarboniteService for an hour so that the Win7 backup can run, then starting CarboniteService back up.

The simple-minded DOS batch equivalent is:
net stop CarboniteService
<wait 1 hour>
Net start CarboniteService

Who is Participating?
Ben Personick (Previously QCubed)Connect With a Mentor Lead Network EngineerCommented:
you don;t want to use net start and stop commands inn win7 anyway it's better to use SC commands as attached.

I don;t personally run UAC on my Win Vista or Win7 machines (though granted it's definitely conceivable to do that in win7) so I'm not certainn on how to do this 9googlinng time! BRB) but there is a way to make a command run inn a script run with elevated credentials..

Okay I looked into it, and it appears the way to set a scheduled task to run without causing theUAC prompt is to log inn as a local administrator with UAC enabled.

Open the task schedualer, accept the UAC prompt, and click "Create Task.." NOT "Create Basic Task"

check the radio button for "Run whether the user is logged in or not" and then most importantly make sure you check the Checkbox for "Run With Highest Privileges"

Then just set the batch file you created to be the task it runs.  that should settle things for you.

SC Stop CarboniteService > nul
Ping -n 3600 -w 1000 > nul
SC Start CarboniteService > nul

Open in new window

Is it just a matter of bringing the UAC level down to "Never Notify"?  
nieblertechAuthor Commented:
I don't want to disable UAC.  I actually like UAC in Win7.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Erik PittiCommented:
Get sleep.exe from the WIndows 2003 resource kit.  Download it here: 

Then your batch file will look like the following:

net stop CarboniteService
REM Sleep for 1 hour (60 minutes *60 seconds)
sleep 3600
net start CarboniteService
nieblertechAuthor Commented:
Thanks chakote,

I have that on my system someplace but couldn't remember the name of it when writing the post.  The real issue is getting the Net Stop/Net Start commands (or something equivalent) to work in Windows 7.
Erik PittiConnect With a Mentor Commented:
I had a similar setup for clearing disconnected terminal services connections on a WIndows 2008 server (the task was running from my windows 7 workstation with UAC enabled).

You need to specify the user account the task will run as, and it needs administrator rights to the computer. In the task scheduler UI its listed as "When running the task, user the following user account:" in security options on the general tab.  Also be sure to check the "Run with highest privileges checkbox"

From the helpfile:
If you select the checkbox labeled Run with highest privileges, Task Scheduler will run the task using an elevated privileges token rather than a least privileges (UAC) token. Only tasks that require elevated privileges to complete their actions should run with elevated privileges. For more information, see User Account Control.
Erik PittiCommented:
I have two comments for @QCubed.  

1 Did you read the thread history at all? The  "Run with highest privileges" was covered already.
2. SC is overkill where net stop/net start will do just fine.  Also if you have a service with dependencies you can do net stop <servicename> /y and the net command will stop all dependent services.  For SC to do this, you would need to query the service's config and stop each depented service individually. Also, the SC command has always been available (since NT4 at least)  in the Windows Resrouce Kit, however its not always the best fit for every situation. Only on WIndows 7 has it been included (as have a lot of other former resource kit only tools).
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Heh answered slightly out of order ^^ oh morning time brain function.  (I would cut and paste and make it the right order, but that gives me issues on EE so please bear-with it instead.  ~Q)

Yes, it became included as a standard tool in windows 7 because it's more versatile than NET Start/Stop.

However, you're right SC will NOT stop a service if other running services are dependent on it.  However NET will NOT start the dependent services the stop command forced to a halt either, so one way or another if dependent services are in play you have to do more to handle them correctly.

  If the Author really has services dependent on Carbonite he either has to know what all of them are to begin with to start them all back up at the time of or he has to run sc EnumDepend and use it's out-put to run SC Stop and SC Start commands, but then I guess he could run SC EnumDepend and use it;'s output to run NET start and stop commands, I prefer SC though because it can be used remotely and offers much more functionality (in my opinion).

  In fact Before I went to bed I made a mental not to suggest that the author instead of running the task locally could instead run it from a Windows 2008 server and use the remote credentials instead.

No, I did not read the history in detail I only skimmed it.
 However when I began writing my post, your last couple of posts WERE NOT present when I began writing mine, only the author's post existed.

  So please don't jump to the conclusion that I'm re-posting your last post or something of the sort. ^^  It just takes me a while to write these sometimes especially since I picked up and readied for bed mid-post, and finished it after.

Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Oh, PS:  and I prefer NOT to use 3rd party or non included in the OS utilities if at all possible, so that is why i use the ping to sleep for an hour instead of downloading the sleep command from one of the old resource kits.
AmazingTechConnect With a Mentor Commented:
Why use ping or even sleep?

Use 2 scheduled tasks 1 to stop at say 1:00 and one to start at 2:00.

Having the CPU loop for nothing would slow down your backup.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
AmazingTech, you are quite right, I'm so used to needing a pause of only a few moments, that the obvious answer flew right by. Without a doubt that would be the more correct method to wait an hour.

However I only just realise as writing that that we are all in correct.

The best script would be one which stopped the service, ran the NT backup jo, and then started the carbonite service.  To ensure the carbonite service never steps on the toes of Windows backup, and that the least chance of leaving the machine without backup process running.
Without all the details it's difficult to know whether the backup is locally on the windows 7 or being backed up remotely.
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Doesn't really matter, SC works on a remote computer.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.