Windows bat as service

Windows bat as service

Сообщения: 3486
Благодарности: 506

——-
Просьба обращаться на «ты».
Посты на абракадарском языке в игноре .

» width=»100%» style=»BORDER-RIGHT: #719bd9 1px solid; BORDER-LEFT: #719bd9 1px solid; BORDER-BOTTOM: #719bd9 1px solid» cellpadding=»6″ cellspacing=»0″ border=»0″>

Сообщения: 130
Благодарности: 3

. Вот для наглядности скопируй этот тектст и сделай батник из него —
Замени только (myserver\Share\) на свой, и все. Мож чего получится.
IP заведомо не правильный, для того чтобы выполнялось условие создания отчета.

@Echo Off
TITLE PING AND MONITORING
@Echo Off
:begin
set /A count=count+1
echo Pinging number %count%
@echo
ping -n 4 127.0.0.254
rem if errorlevel 1 echo PING NO SUCCESSFULL %time%>> \\myserver\Share\%DATE%.txt
echo.
echo —————————————————————————-
if errorlevel 1 echo.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo PING AND MONITORING>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo Date %date:

6,4% Time %time%>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 @echo
if errorlevel 1 @echo
if errorlevel 1 @echo
if errorlevel 1 @echo
if errorlevel 1 echo request timed out.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo request timed out.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo request timed out.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo *************************************************************>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo Pause: 30 seconds.>> \\myserver\Share\%DATE%.txt
if errorlevel 1 echo *************************************************************>> \\myserver\Share\%DATE%.txt
@Echo Off
Echo Pause 30 seconds. Please Wait.
ping -n 30 127.0.0.1 >nul
cls
goto begin

Вот теперь я его в ехе-шник сделал и запустил как сервис. А где и как здесь прописывать доменного юзверя — ума не приложу.
МОЖ КТО ПОДСКАЖЕТ.

ДА, ЗАБЫЛ — ОБЯЗАТЕЛЬНОЕ УСЛОВИЕ — НУЖНО ЧТО БЫ ЭТО ВСЕ РАБОТАЛО БЕЗ ВХОДА В СИСТЕМУ НА XP.

Последний раз редактировалось registeruser1, 27-12-2009 в 07:54 . Причина: исправление

Execute bat file as a windows service

I have a windows batch file which i want to execute as a service. I have found app like alwaysRun but i want to use windows in-build app for this purpose. Can anyone please suggest.

My Use Case is : — I have a batch file which will be executing after every 10 secs. So i have created a normal batch file which calls this bat file and sleeps for 10 secs. So i want to make this second bat file as a service. So that it is called once and when the windows reboots.

This file should be called as a service.

3 Answers 3

I would not do that, it is useless. You could run the first batch file from a scheduled task.

OR

if you want run it at startup.

As an example, on Windows 8

Create a VBS file that will completely hide your batch file.

hideme.vbs

then open Start / Run and type shell.startup and press enter. Paste a shortcut of the VBS file here.

This will let VBS file call the second batch file in hidden mode each time the PC starts up.

Читайте также:  Сторонний редактор реестра windows 10

EDIT In order to kill it, you need to create another batchfile which you can run to find the cmd.exe that is running in the background.

In your original batch file, create a title at the very beginning after @echo off

Now in your new batchfile, let’s call it killLOOP.cmd you add this:

This will search for a process with Window title LOOP, then kill it. Just run it when you want to close it. now in your

Run .bat as a Windows Service

I’m here again with another case that is getting me out my mind.

So, this is happening, I’m trying to run an executable java class(.jar) as a Windows Service, and all my attempts failed so far. To make it a little easier, I turned my jar into a batch file, wich only executes the jar in background, here is the code:

The batch works fine. However I have tried to install this batch as a service by using the next line in cmd:

The Service installs correctly, but as soon as I try to start it, it pops up an error (The code error is 1053, says something about the service did not start correctly)

Also, I have try with a software called NSSM (non-sucking service manager) It installs fine too, but the service does not start either.

Do you guys know what am I missing?

By the way, I’m doing all this on Windows 7 Professional.

1 Answer 1

thanks for your comments Both tools didnt work for me, sadly. However I was able to do it with a software called Java Service Wrapper. This is not a free software, so I needed to buy a license to get it to work. The steps were simple:

1.-Create a folder with the name of the service, then inside add 4 folders: lib,bin,logs,conf

2.-On the lib folder you have to copy your jar and also the wrapper.jar and wrapper.dll (these last two are in the zip you download from the website)

3.-Copy 4 files to the bin folder: InstallApp-NT.bat.in, App.bat.in, UnintstallApp-NT.bat.in and wrapper.exe (this last one is the one that defines your license, if you can get a file wrapper.exe from somebody else who had paid a license it will work fine. These file also came in the zip from the website) Remove the .in from the batch files

4.-The most tricky step is this: You have to copy from the wrapper’s folder called conf a file called wrapper.conf.in Remove the .in extension and edit it with a tex editor. The most important lines you have to edit are:

Then just execute the InstallApp-NT.bat and start the service and your are done

It worked to me with absolute paths, however according to documentation it will work fine with relative path too. This is how I solved my problem and I hope someone with the same issue will find this helpful

How to create a service running a .bat file on Windows 2008 Server?

I’ve created the service using

But when I start it, it fails with the following error message:

On Win2k3 I used the srvany.exe from the Resource kit, but there’s no resource kit for win2k8. For the time being I’ve installed the srvany.exe on my machine, but I don’t think that’s the best way to do it.

10 Answers 10

Of course you can’t run a service like that. Did you try using srvany in server 2008? As far as I know there is no ‘official’ version of srvany for s2k8, but you can always try, can you not?

EDIT: Oh, I forgot! We also use FireDaemon to run non-service applications as services. Though, it is not free and its functionality is a bit too much for your needs.

Читайте также:  Windows vistas home premium

Use NSSM to run a .BAT or any old .EXE file as a service. It is almost a replacement for SRVANY and is a no frill no fuss application.

I had the same problem as you, and NSSM was the only utility that worked.

We use Task Scheduler to run task whenever the computer starts (regardless a user is logged in or not)

You can’t just run a .BAT file or any old .EXE file as a service. Windows services don’t work that you. You can use a utility like the non-Sucking Service Manager (NSSM) or Microsoft’s non-free SRVANY utility to run an arbitrary program as a service, but you may find that you don’t get very «graceful» stopping behavior.

The SC command just adds the required registry entries for a service — if the file you specify isn’t a valid service exe it won’t turn it into one.

Here’s what to do .

At the time of this posting, there is no Windows Server 2008 Resource Kit Tools, so get the «srvany.exe » from the «Windows Server 2003 Resource Kit Tools » and copy it to a suitable location on your Win2008 server (e.g. C:\Windows\System32\ ). Use «sc » to create a new service that launches «srvany » (e.g. sc create MyService binPath= C:\Windows\System32\srvany.exe DisplayName= «My Custom Service» ) Using RegEdit : create a «Parameters » key for your service (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Paramaters\ ) Using RegEdit : within the newly created «Parameters » key , create a string value called «Application » and enter the full path to the application you are wanting to run as a service. (No quotes required.)

And that should do it.

Note : some applications store their settings in a user hive, not under HKEY_LOCAL_MACHINE, so you will need to run the application to configure it, and then configure the service to run as that user.

try this. it worked for me..

I stumbled upon this thing. Here is the tool called Bat_To_Exe_Converter. It’s very very easy to convert any .bat files to .exe files. Try it. Download downloader first and it will download the Bat_To_Exe_Converter automatically then. It really helped me to create service which runs my .bat file through .exe. http://download.cnet.com/Bat-To-Exe-Converter/3001-2069_4-10555897.html?spi=eae5ab48292cc101b5f0ec370cf99ad0

i used Windows schtasks.

I found two other options that won’t have you reaching for your wallet.

You can have sc launch srvany which then points to your program. (there’s a forum post that I can’t link to because I don’t have enough rep here though I do on StackOverflow — grumble)

Here are the steps that the original poster used:

  1. Use sc to create a new service that launches srvany (e.g. sc create MyService binPath=C:\Windows\System32\srvany.exe DisplayName=»My Custom Service» )
  2. Using RegEdit: create a Parameters key for your service (e.g. HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService\Parameters\ )
  3. Using RegEdit: within the newly created Parameters key, create a string value called Application and enter the full path to the application you are wanting to run as a service. (No quotes required.)

If that seems like a bit of a hack, then you can build a .NET service to launch your process. If you want to skip a few steps, here’s an open source .NET service that can launch generic processes:

Tweak it to your needs but it’ll do most of what you want out of the box.

Run batch file as a Windows service

In order to run one application, a batch file has to be kicked off (which does things like start Jetty, display live logs, etc). The application will work only if this batch file is running. I am hence forced to have this batch file running and not logout from the Windows server.

Читайте также:  Введите код ключа windows

Can this batch file be run as a service? I am experimenting with one of the suggestions from a similar question.

8 Answers 8

NSSM is totally free and hyper-easy, running command prompt / terminal as administrator:

then a dialog will appear so you can choose where is the file you want to run.

Why not simply set it up as a Scheduled Task that is scheduled to run at start up?

There’s a built in windows cmd to do this: sc create. Not as fancy as nssm, but you don’t have to download an additional piece of software.

  • start=demand means you must start the service yourself
  • whitespace is required after =
  • I did encounter an error on service start that the service did not respond in a timely manner, but it was clear the service had run the .bat successfully. Haven’t dug into this yet but this thread experienced the same thing and solved it using nssm to install the service.

No need for extra software. Use the task scheduler -> create task -> hidden. The checkbox for hidden is in the bottom left corner. Set the task to trigger on login (or whatever condition you like) and choose the task in the actions tab. Running it hidden ensures that the task runs silently in the background like a service.

Note that you must also set the program to run «whether the user is logged in or not» or the program will still run in the foreground.

On Windows 2019 Server, you can run a Minecraft java server with these commands:

sc create minecraft-server DisplayName= «minecraft-server» binpath= «cmd.exe /C C:\Users\Administrator\Desktop\rungui1151.lnk» type= own start= auto

The .lnk file is a standard windows shortcut to a batch file.

— .bat file begins —

java -Xmx40960M -Xms40960M -d64 -jar minecraft_server.1.15.1.jar

All this because:

service does not know how to start in a folder,

cmd.exe does not know how to start in a folder

Starting the service will produce «timely manner» error, but the log file reveals the server is running.

If you need to shut down the server, just go into task manager and find the server java in background processes and end it, or terminate the server from in the game using the /stop command, or for other programs/servers, use the methods relevant to the server.

As Doug Currie says use RunAsService.

From my past experience you must remember that the Service you generate will

  • have a completely different set of environment variables
  • have to be carefully inspected for rights/permissions issues
  • might cause havoc if it opens dialogs asking for any kind of input

not sure if the last one still applies . it was one big night mare in a project I worked on some time ago.

While it is not free (but $39), FireDaemon has worked so well for me I have to recommend it. It will run your batch file but has loads of additional and very useful functionality such as scheduling, service up monitoring, GUI or XML based install of services, dependencies, environmental variables and log management.

I started out using FireDaemon to launch JBoss application servers (run.bat) but shortly after realized that the richness of the FireDaemon configuration abilities allowed me to ditch the batch file and recreate the intent of its commands in the FireDaemon service definition.

There’s also a SUPER FireDaemon called Trinity which you might want to look at if you have a large number of Windows servers on which to manage this service (or technically, any service).

Оцените статью