Announcement

Collapse
No announcement yet.

Running a program/batch as a service

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Running a program/batch as a service

    I added a windows service to run a custom program which is called from a batch file. So I added the service using sc and created it properly (I have done this before for other programs so I know the syntax).

    The problem I have is in starting the service, I always get the error:
    Code:
    The service did not respond to the start or control request in a timely fashion.
    Talk about an annoying and generic error...

    I have checked the program path and args are valid, even tried several different incarnations of it. Every incarnation works if run from cmd. The command line is:
    Code:
    d:\dir1\dir2\start_program.bat
    where the batch simply does a cd into it's current directory and then runs another program in that directory.

    The exact line given above works, I have even changed user accounts to get it to work, let it run as the system account, but the same result. If I runas the above command it works, so the account details cannot be wrong or have permissions problems.

    The thing bails out with an error immediately.

    The batch is written to run in the foreground, not to fork and not to exit unless the service exits and then it reaches the end of the batch file.

    All I can think of is that it is not valid to try to run a batch file as a service.

    Does it take a special kind of program to run as a service, perhaps one that responds to certain system calls or something?
    Or is there some similar limitation about the type of program or the stucture of a program that would stop it running as a service?

  • #2
    Re: Running a program/batch as a service

    Hi,

    Could you share the batch file..........?

    Regards,
    Kapil Sharma
    ~~~~~~~~~~~~~
    Life is too short, Enjoy It.

    Comment


    • #3
      Re: Running a program/batch as a service

      err, it's literally:
      Code:
      cd d:\dir1\dir2
      scriptfile.py somefile.cfg
      There isn't much to see really.

      Comment


      • #4
        Re: Running a program/batch as a service

        use the /d switch to set the drive and path as the active folder;
        Code:
        CD /D "d:\dir1\dir2"
        scriptfile.py somefile.cfg
        if this is what you want to accomplish;
        Code:
        (Set scriptpath=d:\dir1\dir2)
        "%scriptpath%\scriptfile.py" "%scriptpath%\somefile.cfg"
        Is D: a mapped drive?

        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Running a program/batch as a service

          err, yes sorry, ran that of the top of my head just now cos I'm not at work, the batch I wrote does indeed cd /d.

          I have run this batch from the command line and it works fully. It just doesn't work when run as a service with the same user account.

          Comment


          • #6
            Re: Running a program/batch as a service

            Hi,

            Does this user account have the permission to logon as service and logon as batch job on this box?

            Regards,
            Kapil Sharma
            ~~~~~~~~~~~~~
            Life is too short, Enjoy It.

            Comment


            • #7
              Re: Running a program/batch as a service

              yes when changing the user account in services, I get a pop up saying the user has now been granted this right.

              Comment


              • #8
                Re: Running a program/batch as a service

                No,

                Here it will be prompted if user has "logon locally".

                Just run rsop.msc on that machine to determine the below mentioned rights.

                You will find the same under Windows Settings\Security Settings\Local Policies\User Rights Assignment\

                Regards,
                Kapil Sharma
                ~~~~~~~~~~~~~
                Life is too short, Enjoy It.

                Comment


                • #9
                  Re: Running a program/batch as a service

                  well I've tried running it as the default system as well so I'm not sure that it's this.

                  I did definitely receive a message saying that the user has been granted the log on a service right or similar though when using a regular user account.

                  Comment


                  • #10
                    Re: Running a program/batch as a service

                    Just go run and and type rsop.msc and hit Enter.

                    Just refer to the attached pic to determine the permissions:

                    Regards,
                    Attached Files
                    Kapil Sharma
                    ~~~~~~~~~~~~~
                    Life is too short, Enjoy It.

                    Comment


                    • #11
                      Re: Running a program/batch as a service

                      You can not configure any executable to run as service. In order for it to be able to run as service it has to implement at least OnStart, OnStop methods.

                      There is a tool called srvany.exe that lets you run any executable as service. See the following KB for details: http://support.microsoft.com/kb/137890
                      Guy Teverovsky
                      "Smith & Wesson - the original point and click interface"

                      Comment


                      • #12
                        Re: Running a program/batch as a service

                        ah, thank you that is exactly what I needed to know. It doesn't work for batch files, but at least I know why now and perhaps I can come up with a workaround.

                        Comment


                        • #13
                          Re: Running a program/batch as a service

                          You could also get the sysinternals utility from MS which was previously divided into filemon and regmon. Run those when you are trying to get yout batch to work and see what it is bugging out on.
                          TIA

                          Steven Teiger [SBS-MVP(2003-2009)]
                          http://www.wintra.co.il/
                          sigpic
                          Iím honoured to have been selected for the SMB 150 list for 2013. This is the third time in succession (no logo available for 2011) that I have been honoured with this award.

                          We donít stop playing because we grow old, we grow old because we stop playing.

                          Comment

                          Working...
                          X