Batch file to start a service if the service is not running

beidog

New member
Local time
10:13 AM
Messages
25
Here what I want :

if servicename is running, do nothing.
if servicename is not running, then start servicename, then record the date and time.


based on my search [COLOR=blue ! important][FONT=inherit ! important][COLOR=blue ! important][FONT=inherit ! important]online[/FONT][/FONT][/COLOR][/COLOR], here is what I have so far:


net start | find "servicename" > nul 2>&1
if not .%errorlevel%.==.0. goto startservice


:startservice
net start "servicename"
echo service restarts @ %date% %time% >> C:\checklog.txt

pause




My problem is:
It looks like that the If statement is not working. Every time I run this batch file, it runs through all the command lines.
and I got a error message said the service is already running.
Please advice.
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
Why not just start it. If it's already running, nothing will happen. For instance, if I try to start BITS and it's already running, this is what I get:

net start "Background Intelligent Transfer Service"
The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

If I run this from a batch file:

net start "Background Intelligent Transfer Service"
echo %errorlevel%


I get:

net start "Background Intelligent Transfer Service"
The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

C:\Users\rrkurtz>echo 2
2

So if %errorlevel% == 2 then it's already running. So the batch file could look like this:

net start "Background Intelligent Transfer Service"

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET date=%mm%%dd%%yyyy%
Rem ERRORLEVEL 1 checks all levels > 0
if ERRORLEVEL 1 echo "Started BITS" %date% > logfile.txt
 
Last edited:

My Computer My Computer

At a glance

Windows 10 Pro X64Intel Quad Core i7-4770 @ 3.4Ghz16.0GB PC3-12800 DDR3 SDRAM 1600 MHzIntel Integrated HD Graphics
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
I don't not want to just run start service before we know it's not running, it may create another kind of issue.
anyone know what's wrong with my code or the if statement?
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64
OS
Windows 7 Ultimate x64
This works for me:

Rem This one is running
net start | find "Task Scheduler" > nul 2>&1
if %ERRORLEVEL%==0 echo "Task Scheduler is running"
if %ERRORLEVEL%==1 echo "Task Scheduler not running"

Rem This one is not running
net start | find "Task SchedulXr" > nul 2>&1
if %ERRORLEVEL%==0 echo "Task SchedulXr is running"
if %ERRORLEVEL%==1 echo "Task SchedulXr not running"

Note that if %ERRORLEVEL%==1 will be true for any value of 1 or greater.
 

My Computer My Computer

At a glance

Windows 10 Pro X64Intel Quad Core i7-4770 @ 3.4Ghz16.0GB PC3-12800 DDR3 SDRAM 1600 MHzIntel Integrated HD Graphics
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
Here what I want :

if servicename is running, do nothing.
if servicename is not running, then start servicename, then record the date and time.


based on my search [COLOR=blue ! important][FONT=inherit ! important][COLOR=blue ! important][FONT=inherit ! important]online[/FONT][/FONT][/COLOR][/COLOR], here is what I have so far:


net start | find "servicename" > nul 2>&1
if not .%errorlevel%.==.0. goto startservice

:startservice
net start "servicename"
echo service restarts @ %date% %time% >> C:\checklog.txt

Unless I'm missing something.. This will ALWAYS try to start the service. If the if not... test returns 0 the processing will drop to the :startservice section... Won't it?

Would this have worked?

net start | find "servicename" > nul 2>&1
if not .%errorlevel%.==.0. goto startservice
goto end

:startservice
net start "servicename"
echo service restarts @ %date% %time% >> C:\checklog.txt

:end
Pause
 

My Computer My Computer

At a glance

Linux Mint 17 Cinnamon | Win 7 Ult x64Intel I7-3770K @ 4.2ghz32GB G-Skill C10QEVGA GTX 670 2GB SC
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Home Made
OS
Linux Mint 17 Cinnamon | Win 7 Ult x64
CPU
Intel I7-3770K @ 4.2ghz
Motherboard
ASRock Extreme 4
Memory
32GB G-Skill C10Q
Graphics Card(s)
EVGA GTX 670 2GB SC
Sound Card
Creative Fatality ExtremeGamer
Monitor(s) Displays
LG E2742V x 2
Screen Resolution
1920x1080
Hard Drives
256GB Vertex 4 SSD
2TB Seagate ST2000DM001
1TB Seagate ST1000DM003
PSU
Corsair HX 650
Case
HAF 932 advanced
Cooling
Corsair H100i liquid cooler
Keyboard
Logitech Wireless
Mouse
Logitech Wireless
Internet Speed
OptusNet NBN 100/40
Antivirus
Malwarebytes
Browser
Firefox 30
Other Info
Router: Sagemcom F@st 3846 Crippled by Optus.
I'm attempting to make a simple batch file one line, to stop the spooler service. I then try to verify through the GUI and hit refresh only to find that the although the cmd line says it stopped, it is still running accourind to the GUI.

I've tried;

elevate net stop spooler

as well as

elevate sc spooler stop

Niether will succeed.
 

My Computer My Computer

At a glance

Windows 7, 64 bit (on my 'main' computer)
OS
Windows 7, 64 bit (on my 'main' computer)
What is elevate? It's not part of my Win 7 system.

How about creating the batch file without elevate, then right click on the batch file and select Run as Administrator? Then creat4e a link to it and when you run the link, the batch file should run with Administrator privileges.
 

My Computer My Computer

At a glance

Windows 10 Pro X64Intel Quad Core i7-4770 @ 3.4Ghz16.0GB PC3-12800 DDR3 SDRAM 1600 MHzIntel Integrated HD Graphics
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
Back
Top