Click here to Skip to main content
15,886,058 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to create a .bat file, which can be executed on a time schedule to start and stop a Winservice. I created a bat file like that below:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\installutil.exe C:\myFolder\WindowServices\WinServiceProject\WinServiceProject\bin\Debug\WinServiceProject.exe
Start C:\myFolder\WindowServices\WinServiceProject\WinServiceProject\bin\Debug\WinServiceProject.exe
pause

Then, I create a basic task using Task Scheduler, and then run the .bat file. But I got installation failed. Of course, the exe can't be started. What's wrong in my .bat file for the installation? Appreciate if you can point it out.
Posted
Updated 22-Aug-14 8:50am
v2
Comments
[no name] 22-Aug-14 16:07pm    
If you are using a batch file to stop and start a service why have a service at all? Services are supposed to run all the time not on a scheduled basis.
Yvan Rodrigues 22-Aug-14 19:22pm    
Not necessarily true. The purpose of an NT Service is to execute a conforming application in the context of the SYSTEM account (back in NT, more options now) without a user logged in and without user intervention.

A service can be designed to execute once and terminate. The most common example of this is services that need to execute once when the system is booted, but never again until the next boot.
[no name] 23-Aug-14 8:03am    
Just because you can abuse a form of technology to do things beyond its intended purpose does not mean you that you do crazy things to abuse the technology.

C:\Users\Yvan>schtasks /?

SCHTASKS /parameter [arguments]

Description:
    Enables an administrator to create, delete, query, change, run and
    end scheduled tasks on a local or remote system.

Parameter List:
    /Create         Creates a new scheduled task.

    /Delete         Deletes the scheduled task(s).

    /Query          Displays all scheduled tasks.

    /Change         Changes the properties of scheduled task.

    /Run            Runs the scheduled task on demand.

    /End            Stops the currently running scheduled task.

    /ShowSid        Shows the security identifier corresponding to a scheduled t
ask name.

    /?              Displays this help message.

Examples:
    SCHTASKS
    SCHTASKS /?
    SCHTASKS /Run /?
    SCHTASKS /End /?
    SCHTASKS /Create /?
    SCHTASKS /Delete /?
    SCHTASKS /Query  /?
    SCHTASKS /Change /?
    SCHTASKS /ShowSid /?
 
Share this answer
 
v2
Comments
s yu 22-Aug-14 14:26pm    
Hard to follow your solution. I have little knowledge on this, and just read a reference at http://www.thewindowsclub.com/how-to-schedule-batch-file-run-automatically-windows-7. Could you detail how to do if you can? Thanks.
Yvan Rodrigues 22-Aug-14 14:49pm    
Write your batch file:

DOSTUFF.CMD:
@ECHO OFF
ECHO Starting service!
SC START MYSERVICE

Figure out when you want to run it:
EVERY MONDAY AT 10AM.

Read the manual:
SCHTASKS /Create /?

Schedule it:
SCHTASKS /Create /SC WEEKLY /D MON /ST 10:00 /TR DOSTUFF.BAT
s yu 22-Aug-14 15:05pm    
In my Service List, the name of the WinService is 'My WinService'. I tried the bat file like that:
DOSTUFF.CMD:
@ECHO OFF
ECHO Starting service!
SC START My WinService
From task Scheduler, it only showed the Task registered and the Status is 'Running'. However, the service is not started through the view on Service List. I don't know what's wrong here. Could you help? Thanks.
Sergey Alexandrovich Kryukov 22-Aug-14 18:43pm    
You perfectly right. This answer is irrelevant to your question. I'll answer...
—SA
Yvan Rodrigues 22-Aug-14 19:06pm    
What is the SERVICE_NAME shown by SC QUERY
The Windows Services are hosted by the Service Controller. The Service Controller can be used programmatically, or through its command-line control utility called the "Service Control" utility. And, as with any command-line application, you can use it in your batch file. (One alternative way would be using PowerShell.) Please see:
http://en.wikipedia.org/wiki/Windows_service#Sc.exe[^].

For basic help run "SC.exe" interactively and follow the instruction for getting help. To do so, first run "CMD.exe", it will show you the command interpreter (without it, SC.exe will be closed on every your command), from the interpreter, run SC.exe and type 'Y' for help topic.

You can also find the help on MSDN. Start, for example, here: http://technet.microsoft.com/en-us/library/bb490995.aspx[^].

—SA
 
Share this answer
 
v4
Comments
s yu 25-Aug-14 9:18am    
SA: Thanks for your responses. Your recommendations are very valuable. Thanks.
Sergey Alexandrovich Kryukov 25-Aug-14 11:53am    
You are very welcome.
Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900