Batch file to restart a service. Windows

131,858
net stop <your service> && net start <your service>

No net restart, unfortunately.

Share:
131,858
Tony
Author by

Tony

Software Engineer. Mobile development. Ruby On Rails.

Updated on February 01, 2020

Comments

  • Tony
    Tony over 4 years

    How can I restart a windows service using a .bat file? I am on windows server.

  • vcsjones
    vcsjones over 12 years
    I think you mean only one &.
  • vcsjones
    vcsjones over 12 years
    Hm. I didn't know that worked. Brilliant.
  • Ry-
    Ry- over 12 years
    @vcsjones: "Brilliant?" :D It's not really that different... but thanks.
  • vcsjones
    vcsjones over 12 years
    To me it is interesting, it short circuits. I never considered that a batch script could do that. The right doesn't execute if the %errorlevel% of the left is none zero. If you use a single &, then both execute regardless of the result of the left.
  • Ry-
    Ry- over 12 years
    @vcsjones: Okay, well I didn't know that :)
  • codea
    codea over 10 years
    What is the difference if you put it on two separate lines ?
  • Ry-
    Ry- over 10 years
    @codea: If you put it on two separate lines, if the first one fails, the second one will still go through. (And then it’s also not a one-liner!)
  • Predrag Stojadinović
    Predrag Stojadinović over 9 years
    Why not just: net stop <your service> <newline> net start <your service> ? Just curious as to the meaning of the & there...
  • Ry-
    Ry- over 9 years
    @PredragStojadinović: The && means that if the net stop fails, the net start doesn’t run.
  • Predrag Stojadinović
    Predrag Stojadinović over 9 years
    @minitech: Awesome. Thanks.
  • Leonardo Galdioli
    Leonardo Galdioli over 4 years
    how to handle errors that may happen?