Setup timeout for commands in Windows .bat file - windows

I wrote a windows .bat script. To run a list of commands and then shut down the computer.
such as:
c:\someapplication.exe
c:\someapplication2.exe
Shutdown -s -t 0
Sometimes, "c:\someapplication.exe" freezes and do not respond. How can I setup timeout for my command "c:\someapplication.exe", so that after a certain amount of time, I want windows to force close the application and continue the rest of the commands?

You could use a combination of ping and taskkill to do this.
start c:\someapplication.exe
ping 127.0.0.1 -n seconds
taskkill /im someapplication.exe /f
start c:\someapplication2.exe
ping 127.0.0.1 -n seconds
taskkill /im someapplication2.exe /f
Shutdown -s -t 0 /f
Just replace seconds in the ping command with the number of seconds you want to wait before attempting to close the process (enough time so if it's still running it must have crashed). Then the rest of the app can continue until it is forced to shutdown.

if you may afford that all someapplications run in parallel try this
start someapplication
start someapplication2
wait n secons
shutdown
choose your value of n so that it does not proceed with shutdown while someapplications still run legit
or alternatively
start someapplication
wait n seconds
start someapplication2
wait m seconds
shutdown
for wait there are many solutions around, google some bat wait timeout

You can run your exe program and the shutdown command at once and put the timeout in shutdown options [-t].
To run multiple command at once, use "start" command ("start [yourProgram.exe]").
To do force shutdown use [-f] option.
good luck

Related

Disconnect automatically a vpn connection after 5 hours

Is there a way to create a batch file or run some command like shutdown -s -t , where I can include time after which a vpn connection will be disconnected for example after 4 or 5 hours.
My gratitude to everyone.
I'd say you start this script up at a similar time that you start the VPN software up:
TIMEOUT /T 18000 /NOBREAK
TaskKill /f /IM VPNName.exe
This will wait for 5 hours, specified by the 18000 ( the seconds it should wait ) and will then forcefully close your VPN executable. Of course, you must replace VPNName with the executable's name. Also, you should try to change the /t value to test it as 5 hours is a long time to wait to test something.
By the way, this is a batch file.
thank you for your help. I have found something that may prove more useful in the future, though. It's called Perfect Automation, and I can record any action and set it to run at some specific time.
My thanks extended to the creator of this software as well as to you.

Run a batch file 5 minutes after startup in Windows Xp

I want to execute a .bat file 5 minutes after windows starts up. Unfortunately, windows task scheduler doesn't offer anything of the sort, only execute something right on start up. However, I need something to be 5 minutes after startup.
.bat file doesn't do much, just calls one separate .cmd file and passes a parameter. I've tried:
timeout /t 300 /nobreak
"C:\Documents and Settings\Administrator\Desktop\sikuli\runIDE.cmd" -r "C:\Documents and Settings\Administrator\Desktop\sikuli\SikuliXmlTestRunner.sikuli"
However, the runIDE.cmd gets called right away, regardless of the timeout.
You can give wait(300) command at the beginning in your sikuli script to achieve this.
XP doesn't have timeout command, use ping -n 300 localhost>nul or ping -n 1 -w 300000 localhost>nul.

Task scheduler, terminate started program in windows

At my work we have a set up with the task scheduler periodically starting a java program to read mails.
the task is scheduled to run every minute and it calls a .bat file which starts the java program.
Now the problem.
Once in a month or so the jave.exe process doesn't end properly, so the next minute when it tries to run I get:
Task Scheduler failed to start "\XXX Jobs" task for user "NT AUTHORITY\System". Additional Data: Error Value: 2147750687.
And then I get that message every minute until I terminate the java.exe from the task manager.
Now my question, in task scheduler there are some options to choose.
Under settings there is "If the task is already running, then the following rule applies"
If I then choose "Stop the existing instance"
Will this stop the java.exe or just the task? Or is there a better way.
Some advice would be welcome.
At the end of your batch file kill the task.
taskkill /im java.exe
java.exe could be whatever process you are planning on killing. You can add multiple lines of tasskill in a batch file to kill multiple processes at a time.
taskkill /im java.exe
taskkill /im explorer.exe
taskkill /im svhost.exe
"Stop the existing instance" : The Task Scheduler service will stop
the instance of the task that is already running, and run the new
instance of the task.
that means it will kill the process that the scheduler has launched, cmd.exe in your case, as you told us your program is started from within a batch.
Now, i'm not familiar with java but i guess that stopping your batch will kill the java process that where launched if it is not started as a service.
The scheduler will then run another cmd process and execute your batch once again
Better solution would be fix the java hang. As the process starts every minute then in your java program should have the code to automatically exit if it takes more than a minute to complete the task. Another way would be in the batch file you can kill the process after 1 minute. Use the taskkill /im to kill the process.

windows script to run after a specific time

I am wondering if there is any command for windows script that makes the execution of the script halt for some time before continuing executing it?
e.g: run the first two commands in a script, wait 10 seconds then continue executing
Thanks
I guess you are looking for timeout command.
#echo off
command1
REM wait for 10 seconds before executing command2
timeout /t 10
command2
Hope this helps.
You can use the waitfor command. Check out the documentation here.

In batch programing can one command run before the previous command finishes executing?

In batch programing is one command waited until completed until the next one is run? What I mean is for example
net stop wuauserv
net start wuauserv
Since net stop wuauserv takes a while to complete is it given time to complete or do I need another command to wait until it completes?
The NET STOP command does wait (or timeout while waiting) for a service to stop or start.
You can check the %ERRORCODE% from the command to get more information about if there was a problem or if it worked as expected.
In general most system command line tools return control once they are done executing. A few specialized programs will call into other services or systems and may return control before execution is complete. You will need to check the docs for whatever you are trying to run, but generally processes exit once the 'task' they perform is complete.
In a batch file, all commands are run sequentially, and execution waits for the command to complete.
In your example, net stop wuauserv would complete before net start wuauserv gets run.
You could confirm that by running something you know will take a long time, such as
ping www.google.com
ping www.stackoverflow.com
and you'll see that the second ping does not start until the first completes.
In your case, yes the second command will not execute until the first finishes.
However, GUI apps will start up and return control the batch file.
For example,
PING localhost
NOTEPAD
DIR
The DIR command will execute even if NOTEPAD is still running.

Resources