[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

running a batch file from a windows service

Posted on 2008-01-29
28
Medium Priority
?
1,649 Views
Last Modified: 2013-12-03
i have a service that needs to run once in a while a batch file.
i tried winexec(), shellexecute(), shellexecuteex(), system() and createprocess(). they all return success but the batch file never gets executed.
yes, the service runs as local and it's set to SERVICE_INTERACTIVE_PROCESS.
and yes, the account is administrator

question: how to run a simple batch file from a service.

thanks
code is appreciated.

0
Comment
Question by:urif
  • 14
  • 11
  • 3
28 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 20767666
simply :

        system("script.bat");

??
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20767776
I suspect the problem is that a .bat file is not actually an executable but; rather, a batch file executed by the shell. Try passing the following to WinExec():

cmd.exe /c xxx.bat
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20767781
^^^ obviously xxx.bat should be substituted with the name of your script file.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 40

Expert Comment

by:evilrix
ID: 20773433
Ok, I finally got a chance to try this myself and I can confirm the following will open a bat file when just run from the command line.
This is the batch file
^^^^^^^^^^^^^^^^^^^^^^
@echo off
echo "hello world"
pause
 
This is the C++ code
^^^^^^^^^^^^^^^^^^^^^^
#include <windows.h>
 
int main()
{
	WinExec("c:\\temp\\test.bat", 0);
	return 0;
}

Open in new window

0
 

Author Comment

by:urif
ID: 20775357
ok, thanks for the answers, but as i stated, i tried all of them before, it's not a nomal command line, it's a windows service (as stated in the question)
i tried all of the variations, including cmd /c and they do NOT work.
thanks for trying.

by the way, my same code ran from a  normal command line program will work with no problems, ONLY when it's ran from a service it won't run.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20775403
>> by the way, my same code ran from a  normal command line program will work with no problems, ONLY when it's ran from a service it won't run

Ah, well you didn't state that before :)

>> i stated, i tried all of them before, it's not a nomal command line

I had no way to test this running as a service so I could only suggest. Sorry I was unable to provide a working solution.

-Rx.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20775482
I think the problem might be that your service is not allowed to execute processes.

Got to Start > Run, and type services.msc, then click OK. This will open service manager.
Find your service in the list, then right-click on it and choose Properties.
On the Log On tab, check that "Allow service to interact with desktop" is checked. If not, check it, and restart the service.
0
 

Author Comment

by:urif
ID: 20784127
thanks inifinity08, but this is what i wrote in my question:

"yes, the service runs as local and it's set to SERVICE_INTERACTIVE_PROCESS."

that means that it starts with Allow service to interact with desktop" already activated
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20784893
I seem to have missed that ... Sorry.

But allow me to continue this train of thought a bit further. Can you check this registry key :

        HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows

for the value of NoInteractiveServices ? If it has a value other than 0, then SERVICE_INTERACTIVE_PROCESS has no effect.

At the risk of giving you information that you already know, here's what MSDN has to say about interactive services :

        http://msdn2.microsoft.com/en-us/library/ms683502(VS.85).aspx
0
 

Author Comment

by:urif
ID: 20786378
thanks. i did in fact check the registry as well, the value is set correctly so no problem there.
i am puzzled everything is set so i don't understand why i can't run a simple batch file.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786397
>> i don't understand why i can't run a simple batch file.
JOOI: Does it work if you run an executable rather than a batch file? If so could you not just re-write the batch file as an exe?
0
 

Author Comment

by:urif
ID: 20786429
it does work with an exe, however the batch file gets written dynamically by another application and i don't have access to the source of that application.
basically a new ever bat file is thrown every 40 min or so and the services comes alone, picks it and handles it.
only there is no "handles it" at the moment...

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786438
What about if your service runs and exe that runs the batch file? A little messy but it might resolve your immediate problem and give you some breathing space to figure out how to do it properly -- if, in fact, it is possible.
0
 

Author Comment

by:urif
ID: 20786487
sounds like a good idea and that is they way is being handle at the moment. however since the services needs to be installed remotely on several systems i was asked to find a solution that can bypass that exe.
so, it is a good workaround but i need to find a way to run that batch file.

how hard can it be?

i've been reading all over different forums and people seem to encounter this problem quite often, and so far no solutions. i've seen this reported on win2k, winXP and 2003.
if it was an MS thing i'll leave it alone as something that cannot be done, but i've also seen people posting in those forum threads that they encounter no problems running a batch file from a service


go figure....

i must be missing something...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786502
>> how hard can it be?
I guess that very much depends on how easy it is to find the answer :) I did some searching on MSDN but could find nothing that I'd consider useful.

>>  must be missing something...
You and I both my friend.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786544
Incidentally, there is not reason why your service can't be the same exe that runs the batch file. It can invoke itself with a specific command line param just to execute the batch file in a separate process -- if you see what I mean.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786571
Also, I wonder if this article might help you diagnose the issue...
http://www.windowsitpro.com/Article/ArticleID/22888/22888.html
0
 

Author Comment

by:urif
ID: 20786574
ok, this will sound like an OS war thing, but i come from the UNIX world, i coded a lot there. in UNIX (Linux) we have daemons, pretty much what MS calls a service. now, a daemon can do anything a normal program does within the its user account, so if i run a daemon with my user account it could pretty much do anything i can do. including running programs, opening documents, etc.
everything is well documented, all the config files are pure ascii, and no complications.
MS however, everything is so closely guarded, config files are formatted, older versions of those formats are not valid on new versions of the same program... the registry is a bad idea, it gets corrupted and puff! everything is gone...

anyway, this is my frustration speaking...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786611
I also discovered this, what seems to apply to your problem.
http://www.experts-exchange.com/Programming/Languages/Scripting/Q_22524877.html
0
 

Author Comment

by:urif
ID: 20786628
you mean opening a copy of itself in another thread?
i miss fork() in windows....
0
 

Author Comment

by:urif
ID: 20786659
thanks, i saw that post when i googled for possible answers....
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786665
>> but i come from the UNIX world
Me too :)

>> anyway, this is my frustration speaking...
Go for it, it sometimes helps to vent.

The problem with Windows (if you want to call it that) compared to Unix is that granting privileges to users is a whole lot more complex because, by and large, Windows allows finer granular control. Access to resources is controlled by ACLs (Access Control Lists) and what you can do with them is controlled by the users privileges. I presume your service is running under the LocalSystem account? The default set of privileges can be fund here: -

http://msdn2.microsoft.com/en-us/library/ms684190.aspx

It's probably one of these not being set properly that is the problem. Did you try running the service as a more privileged user (as suggested in the EE link above) to see if that resolved the issue?
0
 
LVL 40

Accepted Solution

by:
evilrix earned 400 total points
ID: 20786686
>> you mean opening a copy of itself in another thread?
No I mean run itself as a new process but with a command line switch that tells it to run the batch file and terminate... so it runs as a normal exe and not as a service. If the service is currently an exe this is a viable solution (although a bit of a kludge).
0
 

Author Comment

by:urif
ID: 20786700
hmmm, i am running it as administrator...
0
 

Author Comment

by:urif
ID: 20786718
ok, i'll give it a try... running the exe as another process... i'll keep you posted. it'll be tomorrow tho

thanks for the help
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20786732
>> thanks for the help
You're welcome -- if you can call it that! To be honest I have no idea so I am just bouncing ideas now :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20801513
Hi urif,

How did you get on with this?

-Rx.
0
 

Author Closing Comment

by:urif
ID: 31425966
i still haven't figured it out, however this one was a good "close to" solutions so the points go to you.
thanks so much for the help
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

608 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