Solved

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

Posted on 2010-09-20
13
2,359 Views
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

Thanks.
0
Comment
Question by:nieblertech
[X]
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
13 Comments
 
LVL 5

Expert Comment

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

Author Comment

by:nieblertech
ID: 33722053
I don't want to disable UAC.  I actually like UAC in Win7.
0
 
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
0
Industry Leaders: 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!

 

Author Comment

by:nieblertech
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.
0
 
LVL 14

Assisted Solution

by:Erik Pitti
Erik Pitti earned 100 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.
0
 
LVL 11

Accepted Solution

by:
Ben Personick (Previously QCubed) earned 350 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.

@ECHO OFF
SC Stop CarboniteService > nul
Ping -n 3600 -w 1000 169.254.254.254 > nul
SC Start CarboniteService > nul

Open in new window

0
 
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).
0
 
LVL 11
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.

~Q
0
 
LVL 11
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.
0
 
LVL 21

Assisted Solution

by:AmazingTech
AmazingTech earned 50 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.
0
 
LVL 11
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.
0
 
LVL 21

Expert Comment

by:AmazingTech
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.
0
 
LVL 11
ID: 33730830
Doesn't really matter, SC works on a remote computer.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will teach you how to change your appearance and customize your Windows 7 interface to your unique preference. This will be demonstrated using Windows 7 operating system.

726 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