Cmd line window not closing after batch file completes - windows

From what I understand, the command line should close automatically after a batch script finishes.
Mine is not closing - which means that it is getting caught up on some task.
I'm not sure what's wrong though - because apart from not closing - the script is working fine!
I'm on a windows 2003 server, this is the batch:
TASKKILL /IM rfbase.exe /F
PING 1.1.1.1 -n 1 -w 3000
cd /D C:\Documents and Settings\All Users\Desktop
CALL service_restart.bat
cd /D E:\Program Files\Accellos\Accellos One Warehouse\RbHandHeld
RFBASE.EXE
EXIT 0

The most likely explanation is your script is waiting for RFBASE.EXE to terminate - though I can't be sure. If this is the cause, then you should be able to fix the problem by invoking RFBASE via START:
TASKKILL /IM rfbase.exe /F
PING 1.1.1.1 -n 1 -w 3000
cd /D C:\Documents and Settings\All Users\Desktop
CALL service_restart.bat
cd /D E:\Program Files\Accellos\Accellos One Warehouse\RbHandHeld
start RFBASE.EXE
EXIT 0

Using the CALL command gives control to the "service_restart.bat" file. Control won't return to the CALLing batch file until the called batch file completes or the EXIT command is encountered in the called batch.

Related

Need a batch file to start, delay a close, and restart another batch file.

I have searched and searched and this is the closest code I have found:
#echo off
:loop
C:\CryptoCurrency\nexus_cpuminer\start.bat
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
A few things: notice the start.bat. The .exe I need to launch has to start via the .bat file because the .bat file contains information the .exe needs.
Secondly, the .exe launches a CMD prompt window which shows me what's going on.
(keep this in mind because this is not your normal .exe, I WANT that CMD prompt window to close when it's KILLED)
I am aware I have it set for 30 seconds. I'm just testing right now. I'd like to set it for 4 hours before the kill command is called. Also, I'd like to set a "delay" of 30 seconds before the whole process starts over. I am running Windows 7 x 64.
You must change the name of the second Batch file to other name (i.e. starter.bat) and execute it via the start internal command in order to execute it in parallel:
#echo off
:loop
start "" cmd /C "C:\CryptoCurrency\nexus_cpuminer\starter.bat"
timeout /t 30 >null
taskkill /f /im nexus_cpuminer.exe >nul
goto loop
The last line in starter.bat file must be the execution of nexus_cpuminer.exe, so when it is killed via taskkill, the .bat file ends immediately.
Another simpler approach is to directly execute nexus_cpuminer.exe in this Batch file, via start "" cmd /C nexus_cpuminer.exe command, so this process be opened in its own cmd.exe window.
If you CALL start.bat, it will return to your 'calling' script.
If you give start.bat a TITLE, you can /FIlter your TASKKILL command to EQ that WINDOWTITLE

Error level not changing when file is ran with psexec

I have one piece of code that is behaving differently when I ran it on server and when I ran it with psexec. I'm actually trying to determent is computer offline, but that is not question here. Problem is that when I run this command directly on server errorlevel is changing to 1, because pc is online. But when I use psexec to run file with same commands on that server errorelevel is not changing it stays 0. I cant find any explanations on internet.
echo %errorlevel%
ping -n 1 machine | findstr "not" > nul
IF %errorlevel%==0 (
echo test
)
echo %errorlevel%
pause
What you show is not a command, but a batch file, say ping_machine.cmd. To call it from psexec the command line would be something like psexec \\server cmd /c ping_machine.cmd. If I guessed wrong you may stop reading the rest of this answer now (and next time post enough relevant context so that one doesn't have to guess).
Problem is that cmd /c returns the exit code of ping_machine, but the batch file does not explicitly set an exit code, so it returns 0 by default. This can be verified at the cmd prompt with the following 2 runs - note that inside the batch file you see the same/correct errorlevels, but cmd /c returns 0.
C:\etc>ping_machine
0
1
Press any key to continue . . .
C:\etc>echo %errorlevel%
1
C:\etc>cmd /c ping_machine
0
1
Press any key to continue . . .
C:\etc>echo %errorlevel%
0
To have cmd /c behave as you expect (and in turn psexec as well), add the following line at the end of the batch file to return the respective errorlevel (this works because neither echo nor pause modify the errorlevel, otherwise you'd have to save it in a temp variable for later use).
exit /b %errorlevel%
There it is. When I run this batch file locally errorlevel is changed to 9009. When I use psexec \\computername -f -c pathToFile , error code is staying 0.
#echo off
echo %errorlevel%
pin 234343
echo %errorlevel%
pause

Batch file to commit changes to Embedded XP and then restart pc

I would like to create a batch file to run a cmd command to commit changes to a windows embedded pc from a USB drive and restart the PC to make the changes active.
The cmd line I use is:
ewfmgr -commit c:
But I need to open the cmd prompt and run the command then once it has run, restart the PC
This is what worked
#echo OFF
:reboot
c:\windows\system32\ewfmgr.exe C: -commit
echo Rebooting...Please Wait
c:\windows\system32\xpepm.exe -restart
pause
shutdown /? could give some hints. Then your batch file might look as follows:
ewfmgr -commit c:
shutdown /r
To ensure batch wait until ewfmgr command ends, use
start "" /W ewfmgr -commit c:
shutdown /r
With the /W or /WAIT switch, the start command will start application and wait for it to terminate. More info on start command.
To give a some kind of wait after the commit command so that it can run and finish its task, e.g. for a delay of 30 seconds:
add timeout /T 30 /nobreak>nul line before shutdown /r, and/or
use shutdown with /t xxx switch (this sets the time-out period
before shutdown to xxx seconds), i.e. shutdown /r /t 30
A workaround if timeout command is not recognized: PING -n 31 127.0.0.1>nul
Create a new file with the .bat extension. Open it in your preferred text editor and enter the commands you want to be run, and save the file. The commands that JosefZ wrote would probably do the job perfectly.

Windows batch file not working properly

My Batch file is not executing the START serial_new.exe, it only terminates serial_new.exe.
it also opens and closes immediately.
ECHO OFF
c:
cd C:\wamp\www\mobilesms\
START serial_new.exe
echo testing echo..
ping 1.1.1.1 -n 1 -w 5000 > nul
taskkill /F /IM serial_new.exe
Try
start "serial_new" serial_new.exe
The start command needs a title for the first argument.

Need to run an .exe, then kill it after ~10 seconds and move to next command in batch file

I am attempting to create a batch file that runs an .exe, but kills said .exe after about 10 seconds (before it completes), then moves on to the next command in the file. Any help would be greatly appreciated.
You can use this:
start program.exe
ping 127.0.0.1 -n 10
taskkill /im program.exe /f
rem continue here
echo Another command!

Resources