Solved

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

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

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 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Expert Comment

by:Ben Personick
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

Expert Comment

by:Ben Personick
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

Expert Comment

by:Ben Personick
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

Expert Comment

by:Ben Personick
ID: 33730830
Doesn't really matter, SC works on a remote computer.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

One of the features I've come to appreciate about Windows 7 and Windows Server 2008 R2 is the ability to pin applications to the task bar. As useful a feature as I've found this, it does have some quirks.  For example, have you ever tried pinning an…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

743 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

14 Experts available now in Live!

Get 1:1 Help Now