Can't get batch to logout when it's done - batch-file

I have a .bat that I've wrote to automate an install of a piece of software I have to install all the time at work. (I can't use powershell because a lot of the machines are XP still)
Near the end, the script is supposed to clean itself up and and logout, but I can't get it to log out for some reason.
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
del "%_sd%md5sum.exe"
...
del "%_sd%RUN-AS-ADMIN.bat"
wait 2
shutdown -f -l

There is no wait command in CMD. There is timeout for Win7+ but not for XP. To implement a kind of a timeout you'll have to use for example ping 127.0.0.1 -n 6 > nul. This will make your system "wait" for 5 seconds. You can modify the timeout by replacing 6 with any other value. If you want a timeout of X seconds, replace 6 with X+1.

EDIT: Made a stupid mistake making this, fixed it up.
Try this instead:
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
del "%_sd%md5sum.exe"
...
del "%_sd%RUN-AS-ADMIN.bat"
pause
shutdown -f -l
All I did was make it so that the wait is replaced with pause
I did some looking around, and I couldn't find any native commands that make the batch file delay and automatically continue, so this is the closest I can find. (Assuming the computer you're using this on is a Windows XP)
If you don't use a Windows XP for the batch file, use this instead:
taskkill /f /im explorer.exe>nul 2>&1
set _sd=%~dp0
cd /d c:\
del "%_sd%md5sum.exe"
...
del "%_sd%RUN-AS-ADMIN.bat"
timeout /t 2 >nul
shutdown -f -l
The timeout acts as a delay, however, it is unfortunately not a native command on Windows XP, but it is on Windows 7+ (Unsure about vista)
Hope this helps :)

Related

Run Batch Command Without Checking For Confirmation

I have a batch file that restarts several computers in my center. The batch file works fine but I would like if it ran faster. The reason it is slow is when it runs into a computer that has been turned off, it continues to try to find that computer for about 15 seconds before it moves to the next one.
Can I lower the time a batch command looks for the computer or just have it send the command and move on to the next?
I have pasted my current batch command below:
shutdown /f /r /m \\VAMAR-3STHWV1 /t 000
With the ping command I believe you can do it. By telling the ping command to only send one echo request and then &&ing the result with the shutdown command, something like:
ping VAMAR-3STHWV1 -n 1 >nul && shutdown /f /r /m \\VAMAR-3STHWV1 /t 000
That way the shutdown command gets executed only when the ping successfully reached out VAMAR-3STHWV1 in one echo request.

Jboss 7 automatic deployment of EAR bat file

I am trying to automate my maven build and deployment process of the EAR in Jboss. I managed to write a bat file for the same which is given below.
ECHO OFF
RMDIR /S /Q .\deploy
call mvn clean install -D build=P,JB
call mvn clean install -D build=F,JB
#echo | call C:\Work\jboss-as-7.1.1.Final\bin\jboss-cli.bat --connect --controller=[my-machine-name]:9999 command=:shutdown
del /q C:\Work\jboss-as-7.1.1.Final\standalone\deployments\*.*
xcopy /s /y .\deploy\function\Jboss\*.ear C:\Work\jboss-as-7.1.1.Final\standalone\deployments
xcopy /s /y .\deploy\WorkFlowEngine\Jboss\*.ear C:\Work\jboss-as-7.1.1.Final\standalone\deployments
cd C:\Work\jboss-as-7.1.1.Final\bin
rmdir "C:\work\jboss-as-7.1.1.Final\standalone\data" /s /q
rmdir "C:\work\jboss-as-7.1.1.Final\standalone\log" /s /q
rmdir "C:\work\jboss-as-7.1.1.Final\standalone\tmp" /s /q
standalone.bat -bmanagement [my-machine-name] -b [my-machine-name] -c standalone-full-ha.xml
{code for check of deployment success/failure}
PAUSE
Here you can see that I am using the line
standalone.bat -bmanagement sbstjwsvm1509 -b sbstjwsvm1509 -c standalone-full-ha.xml
My requirement is that I want to check if the EAR was deployed successfully from my bat file.One way which I thought of is to check for .deplyed or .failed extension files in the Jboss deployment folder.I tried to write codes for the same but my code which is written below the above mentioned line is not getting executed.Is there any other means by which I can achieve this?Or what am I doing wrong in my bat file?Why is my code to check for deployment not getting executed?
Because the standalone.bat isn't executing in background and will be running the JBoss instance until you kill/stop it (ctrl+C signal or like you shutdown through jboss CLI). See this answer to get examples and details about you want to achive.
Since you are using maven, I suggest you to use jboss-as-maven-plugin.
Take a look the Usages, you will find commands to deploy/undeploy applications, resources, and to start/stop the server.
Then you could integrate the plugin execution to stop/clean in the maven clean phase and deploy/start on maven install phase using goals. See complex examples.
Thought of posting my solution here.
ECHO OFF
SET "JBOSS_DIR=C:\Work\jboss-as-7.1.1.Final"
SET "deployedAPP=MyApp.ear"
RMDIR /S /Q .\deploy
call mvn clean install -D build=P,JB
call mvn clean install -D build=F,JB
#echo | call %JBOSS_DIR%\bin\jboss-cli.bat --connect --controller=[mymachine_name]:9999 command=:shutdown
del /q %JBOSS_DIR%\standalone\deployments\*.*
xcopy /s /y .\deploy\function\Jboss\*.ear %JBOSS_DIR%\standalone\deployments
xcopy /s /y .\deploy\WorkFlowEngine\Jboss\*.ear %JBOSS_DIR%\standalone\deployments
cd %JBOSS_DIR%\bin
PING -n 61 -w 1 localhost >nul
START CMD /C CALL MyJboss.bat
SET count=0
:checkIfDeployed
if exist "%JBOSS_DIR%\standalone\deployments\%deployedAPP%.deployed" (
GOTO appDeployed
)
if exist "%JBOSS_DIR%\standalone\deployments\%deployedAPP%.failed" (
GOTO deployFailed
)
PING -n 6 -w 1 localhost >nul
GOTO checkIfDeployed
:appDeployed
PAUSE
EXIT
:deployFailed
SET /A count+=1
if %count% == 5 ( goto end )
#echo | call %JBOSS_DIR%\bin\jboss-cli.bat --connect --controller=[mymachine_name]:9999 command=:shutdown
PING -n 61 -w 1 localhost >nul
START CMD /C CALL MyJboss.bat
GOTO checkIfDeployed
:end
PAUSE
Used the line
START CMD /C CALL MyJboss.bat
to start JBoss in another cmd window.Now all my codes from below will get executed.

How to kill a service in a batch file every 20 seconds?

A task called "FireSvc.exe" (McAffee service) keeps interfering with our app upgrades. I put
> taskkill /f /im "FireSvc.exe"
in a batch file. We continuously run this during installs so the software will successfully upgrade. I'm not positive why this service starts back so often. We have to run from the command line, because in the task manager you get "access denied" when trying to kill this task.
My question is, how would you make this run every 20-30 seconds?
We cannot install any type of non-approved software either. So, theres that...
Thanks for any input.
Here's what we use:
:runagain
taskkill /f /im "FireSvc.exe"
timeout /T 5
goto runagain
You can use vbscript's sleep command in a batch file with cscript like so...
First create a vbscript file (.vbs extension) that contains the following code (don't forget to save it with ANSI encoding otherwise it won't work):
Wscript.sleep 2500
Wscript.exit
Create a batch file in the same directory with this code:
#echo off
:Kill
cscript //NoLogo file.vbs
taskkill /f /im "FireSvc.exe"
GoTo Kill
I currently don't have access to a PC to check if it works so let me know what happens. I still think there might be a cleverer alternative... cheers!
Edit: Btw you can also simulate a sleep command with the ping command like so:
ping localhost -n 1 -w 2500 >nul
And if you are using windows vista or above you can use the timeout command.

Timeout prompt won't stop showing

I'm attempting to use Batch for the first time, and I'm running into some trouble with the timeout command. I'm making a simple backup program to backup certain files to my flash drive, and this is the beginning.I'm trying to make it so that the prompt does not show how much of the countdown is left. This is what I have:
ECHO Deleting current backup location...
RD /s /q F:\CurrentBackup
#TIMEOUT /t 10
ECHO Setting up new backup...
MKDIR F:\CurrentBackup
MKDIR F:\CurrentBackup\Documents
MKDIR F:\CurrentBackup\Pictures
MKDIR F:\CurrentBackup\Desktop
MKDIR F:\CurrentBackup\Music
rem xcopy C:\Eric D:\
Can anyone help me with this seemingly simple problem?
you can tell a command, where to write it's output. If you don't, it writes it to screen
TIMEOUT /t 10 >nul
will write the output to a "Null-Device" (also known as "Nirwana")
by the way: # does not suppress the output of a command, but suppress the repetition of the commandline. It's a kind of "one-line-echo off"
Normally, you put
#echo off
as the first line of a script.
echo off will turn command repetition off, and the # does the same thing for this very line (as the echo off is not yet active for this line)

How to run multiple programs using batch file

I like to run two programs using batch file, but the condition is, the second program must start only after the first program loaded, so is there any way to control using timer to control when the program starts.
I needed the same thing, and found out that following thing works as hoped:
start D:\Michal\Xming\Xming.exe -multiwindow
start D:\Michal\Xming\putty.exe
start D:\Michal\WinSCP\WinSCP.exe
And it all is saved in file Login.bat.
BTW, I am running Win7 but I doubt that this has any impact.
Basically, you could try this approach (not tested):
Run the first program using the start command.
Check the task list in a loop to see if the program has appeared there.
Impose some time limitation to the said loop.
Run the next program in case of success, exit with notification otherwise.
The scripting might look like this:
#ECHO OFF
START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START program2.exe
Keep in mind that the timing is not precise, especially if you are going to insert delays between the task list checks.
I think this might be irrelevant here but would like share the following:
I 've created the following batch file and run it whenever I open my laptop in the office to open relevant programs at a single click.
Kept this file at Desktop and created a folder where I put all shortcuts for relevant programs.
So, I run these shortcuts in the batch file as follows:
#ECHO off
start C:\Users\User1\Desktop\Softwares\IE
start C:\Users\User1\Desktop\Softwares\Googletalk
start C:\Users\User1\Desktop\Softwares\YahooMessenger
start C:\Users\User1\Desktop\Softwares\Program4
start C:\Users\User1\Desktop\Softwares\Program5
I have also find a small hack to do it, just using a ping command with -n switch as follows:
start /d "C:\Program Files (x86)\Mobile Partner\" MobilePartner.exe
ping 127.0.0.1 -n 8
start /d "F:\Other Applcations\System Tools\OS Tweak\" dragfullwindows.exe
I wrote this answer as I am on Windows 10 and it is 2021.
And this provides a few more ideas for newage programs, that should run in the taskbar(in the background).
Here is my "Work Start.bat" batch file sitting on my desktop:
rem Work Start Batch Job from Desktop
rem Launchs All Work Apps
#echo off
rem start "Start VS Code" "C:\Users\yourname\AppData\Local\Programs\Microsoft VS Code\code.exe"
start "Start OneDrive" "C:\Users\yourname\AppData\Local\Microsoft\OneDrive\OneDrive.exe"
start "Start Google Sync" "C:\Program Files\Google\Drive\GoogleDriveSync.exe"
start "Start Clipboard" "C:\Program Files\Beyond Compare 4\BCClipboard.exe"
start "Start Cisco AnyConnect" "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe"
start chrome
start firefox
start skype
start "Start Teams" "C:\Users\yourname\AppData\Local\Microsoft\Teams\current\Teams.exe" /MIN
start Slack
start Zoom
sleep 10
taskkill /IM "explorer.exe"
taskkill /IM "teams.exe"
taskkill /IM "skype.exe"
taskkill /IM "slack.exe"
taskkill /IM "zoom.exe"
taskkill /IM "cmd.exe"
#echo on
Some Apps would not start with a simple "start app" command, so I used the full path.
For some reason some were found in my user appdata folder and not in program files, I do not understand this behaviour of program storage, it makes no sense.
I used a time delay so that the apps could fully start before I sent them to the background using taskkill command
I killed explorer.exe because OneDrive opens explorer
I killed cmd.exe because it opened and stayed opened due to badly behaving apps.
The rest I killed so that they would just move to the background.
Here is my "Work End.bat" batch file to forceably close everything:
rem Work End Batch Job from Desktop
rem Forcibly Closes All Work Apps
#echo off
taskkill /f /IM OneDrive.exe
taskkill /f /IM GoogleDriveSync.exe
taskkill /f /IM BCClipboard.exe
taskkill /f /IM "vpnui.exe"
taskkill /f /IM "chrome.exe"
taskkill /f /IM "firefox.exe"
taskkill /IM "explorer.exe"
taskkill /f /IM "teams.exe"
taskkill /f /IM "skype.exe"
taskkill /f /IM "slack.exe"
taskkill /f /IM "zoom.exe"
#echo on
I do have to ensure I have saved all my work, and that files are no longer syncing.
Possibly I will need a batch file that kills everything except file sync.
The good thing about forceably killing Chrome and firefox is they ask to be restored on next start, so I can continue where I left off, assuming I saved everything.
If I don't forceably kill these they stay in the background, if I close using the Cross they do not offer me to start from where I left off.
I could then start my gaming files, in another batch file but this would be similar to the first file.
Now I can turn off the default "Start Up" Apps and use this "Work Start.bat" , because the Start Up Apps annoy me when I start my pc just to game.

Resources