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

Posted on 2010-09-20
Medium Priority
Last Modified: 2012-06-27
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

Question by:nieblertech
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
  • 5
  • 3
  • 2
  • +2

Expert Comment

ID: 33722035
Is it just a matter of bringing the UAC level down to "Never Notify"?  

Author Comment

ID: 33722053
I don't want to disable UAC.  I actually like UAC in Win7.
LVL 14

Expert Comment

by:Erik Pitti
ID: 33722148
Get sleep.exe from the WIndows 2003 resource kit.  Download it here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en 

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

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 33722157
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.
LVL 14

Assisted Solution

by:Erik Pitti
Erik Pitti earned 400 total points
ID: 33722362
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.
LVL 14

Accepted Solution

Ben Personick (Previously QCubed) earned 1400 total points
ID: 33722463
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

LVL 14

Expert Comment

by:Erik Pitti
ID: 33722649
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).
LVL 14
ID: 33725163
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.

LVL 14
ID: 33725190
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.
LVL 21

Assisted Solution

AmazingTech earned 200 total points
ID: 33730518
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.
LVL 14
ID: 33730609
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.
LVL 21

Expert Comment

ID: 33730630
Without all the details it's difficult to know whether the backup is locally on the windows 7 or being backed up remotely.
LVL 14
ID: 33730830
Doesn't really matter, SC works on a remote computer.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
Suggested Courses

765 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