a service that run batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

a service that run batch file

#1 Post by abc0502 » 19 Aug 2012 17:19

I'm making a service that run a batch file, but when i type services.msc in run menu and go to that service "for testing" and make it start i get this message
Could not start the connection service on local computer
Error 1053: The service didn't respond to the start or control request in a timely fashion.


I use this command to create the service

Code: Select all

sc create connection type= own start= auto error= ignore binpath= "cmd.exe /c C:\batch_Tools\Check_connection.bat" displayname= "Connection Check"


This is the batch, it check for the internet connectivity if it is down it keep checking and when it find it, make vbscript that make the computer say a word

Code: Select all

@echo off
cls
mode 70,10
Title Connecting ...
Color 0c
:loop
If Exist "%temp%\voice.vbs" Del /Q "%temp%\voice.vbs"
ping www.google.com -n 1 -l 1 >nul
if %ERRORLEVEL%==0 (
Title Connected
call :voice
) else ( goto loop )

:voice
(
echo.Dim message, sapi
echo.message="Establishing  Enternet Connection"
echo.Set sapi=CreateObject^("sapi.spvoice"^)
echo.sapi.Speak message
) >>%temp%\voice.vbs
call "%temp%\voice.vbs"
ping localhost -n 1 >nul
del /Q "%temp%\voice.vbs" >nul


any idea why the service refuse to start, i don't want to use the registry to launch the batch

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: a service that run batch file

#2 Post by Liviu » 19 Aug 2012 17:47

abc0502 wrote:I use this command to create the service[code]
sc create connection type= own start= auto error= ignore binpath= "cmd.exe /c C:\batch_Tools\Check_connection.bat" displayname= "Connection Check"

That won't work, can't work. Services are special apps with specific requirements (http://msdn.microsoft.com/en-us/library/ms685967%28VS.85%29.aspx) which simply can't be satisfied with a batch file. There are wrappers which might technically launch almost anything as a service, but that's ill advised and frowned upon.

Why do you think you need a service vs. for example a regular app, or a scheduled task, or a startup item?

Liviu

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: a service that run batch file

#3 Post by abc0502 » 19 Aug 2012 17:59

thanks for the reply, i guess i will just use a start up folder as i used it before,

Why do you think you need a service vs. for example a regular app, or a scheduled task, or a startup item?


services work in the background,and has no taskbar tab so i thought the batch can do that too, instead making a vbscript run the batch hidden.

internet connection some times get lost, so i made the batch to tell me when it come back while i'm working or playing games.

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: a service that run batch file

#4 Post by Liviu » 19 Aug 2012 18:14

abc0502 wrote:services work in the background,and has no taskbar tab so i thought the batch can do that too, instead making a vbscript run the batch hidden.

I think you have the wrong picture of how services work. This being a batch forum, and not knowing your background in Windows internals, I won't even attempt to go into details.

abc0502 wrote:internet connection some times get lost, so i made the batch to tell me when it come back while i'm working or playing games.

FWIW the posted batch will execute ":voice" twice once the connection is established - first by call'ing the label, then just falling through.

Liviu

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: a service that run batch file

#5 Post by abc0502 » 19 Aug 2012 18:41

FWIW the posted batch will execute ":voice" twice once the connection is established - first by call'ing the label, then just falling through.

I missied the Goto :eof when copying the code from the batch :oops:

I think you have the wrong picture of how services work. This being a batch forum, and not knowing your background in Windows internals, I won't even attempt to go into details.

Well, Not every one is a computer genius like you :)
I will read more about it, thanks for the advice :wink:

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: a service that run batch file

#6 Post by Liviu » 19 Aug 2012 19:04

Sorry if I sounded condescending, didn't mean it in the least. Was just trying to say that batch files are simply not suited for implementing a service, and just explaining exactly why that can't work would take delving into boring technicalities.

It is, as I said, possible to write (or use an already written) wrapper to run (almost) anything as a service. But there's pitfalls with those, not always spelled out clearly. For your use case, I think it'd would be an overkill, and it makes most sense to keep it as a launch-on-demand script.

Liviu

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: a service that run batch file

#7 Post by abc0502 » 19 Aug 2012 19:19

Really no need for sorry, i was joking with you, that's all :)
beside your post was informative, I found some usefull topics,

http://msdn.microsoft.com/en-us/library ... 80%29.aspx
http://en.wikipedia.org/wiki/Windows_service

thanks,

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: a service that run batch file

#8 Post by Liviu » 19 Aug 2012 19:56

No worry ;-) ...Yet, and not trying to dissuade you from researching services in any way or depth, but before you spend too much time on it in relation to the stated topic here, consider the question "what could a service do that a regular app couldn't" in this case. Quoting from your first link above...
Introduction to Windows Service Applications wrote:These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account.

Your use case is an applet alerting the current user about some connection status change. It's neither user-less, nor different-user, least at all server, and the ":voice" part counts as "user interface". So it has none of the attributes which would make it a likely candidate for a service. Which is why I said that it makes most sense to leave it as a regular applet - also saving the housekeeping required of a service proper, like starting, pausing, servicing requests etc.

Liviu

Post Reply