I have a Garry's Mod server I want it to autorestart every 12 hours. I can make a script where it executes a command every 12 hours but I dont know how to send the command "quit" to the process "srcds.exe". I saw something like
echo quit |srcds.exe
I tried it but it didnt work. How do I do this using MS-DOS (batch file)?
You are looking for the taskkill command:
taskkill /f /im srcds.exe
And it kill the tasks srcds.exe
If you can edit the program you want to send a command to, you can try inserting this command line into it where you want it to terminate:
if exist (directory)\(triggering file).txt del /Q (directory)\(triggering file).txt & quit
This will scan for the file that the triggering program will create, delete it, and then quit the program. If the file is not present, the program will ignore the line and move on as if the command line wasn't there.
The triggering program can be run whenever you want to terminate the main program. This program should be:
echo.>"(directory)\(triggering file).txt"
exit
Above was the simplest way of doing this. If you want to make it more "idiot proofed" you can add as the first line of the main program:
echo.>"(directory)\(main run file).txt"
This will write a .txt file the same way the triggering program does. This tells the triggering program that the main program is running and it can terminate it. You will also have to change the above IF statement in the main program to read:
if exist (directory)\(triggering file).txt del /Q (directory)\(main run file).txt & del /Q (directory)\(triggering file).txt & quit
Finally, change the triggering program to read:
if not exist (directory)\(main run file).txt quit
echo.>"(directory)\(triggering file).txt"
quit
Just some final notes, wherever I have "(directory)" replace it and the ()'s with an otherwise unused, untouched directory. Wherever you see "(triggering file)" replace it and the ()'s with a filename that is common with all other places you see "(triggering file)". Same goes to "(main run file)" but it must have a different name then "(triggering file)".
I know this might be confusing. Don't be afraid to comment and ask quesions. I will be happy to clarify.
Related
I have 2 batch files (1.bat and 2.bat). I want 2.bat to run when 1.bat closes. Is that possible?
Or is there a way to close a batch file when a batch file closes. Here is my code which doesnt work:
cd C:\xampp
start apache_start.bat
cd C:\Users\MinecraftServer\Desktop\1.12.2MinecraftServer
MC1.12.2Start.bat
taskkill /F /IM cmd.exe /T
I have a minecraft server running with a .bat file and a web server running off of a .bat file (using xampp, its used for prtg monitorng).
start will create a completely independent process and continue immediately to your next batch command. That independent process may do a job and terminate, or may say open an application like notepad and wait until the notepad is closed. Regardless, a straight start command will not stop the batch that it is in - the next step will be executed, whether or not the process that has been started has terminated.
You can also add the /wait switch to start. If this switch is used, the batch will wait until the started process terminates.
Preferred syntax for start is
start /wait "window title" executable parameters...
where "window title" may be an empty, but not absent string. Including this as the first parameter is preferred because the first quoted string in a start command is used as a window title for the started process, and thus start may not act as expected where the executable or any of its parameters is quoted.
call executablename... will wait for that executable to terminate before progressing to the next step. This may or may not be what is desired.
MC1.12.2Start.bat in your code will SWITCH to that batch and remaining batch lines will be ignored.
Which may be fortunate in this case, since as you've coded it, the apache_start.bat may or may not have completed when the taskkill command is run, killing all cmd.exe sessions.
I have an issue where if i run a command manually it prompts for the next volume. which is fine, i can use the '< inputfile.txt' to run this through a batch file.
But what happens if the volume in inputfile.txt hasn't yet been created? - I get an error and the programs crashes out.
I cant change the program that prompts unfortunately as its bundled in a .exe.
My theory would be to run the program as if it's interactive with no '< inputfile.txt' and make the calling program wait until the volume is complete before providing the next volume.
i'm running this:
create.bat < cInputFile >> cTempLog
and get
** Cannot find or open file
if i run manually i get:
Please enter next device or file name or type 'quit' to exit:
Any help would be great,
Thanks
You could write a second batch file to pipe the input into create.bat and sleep before each line.
something like this i'd guess:
#echo off
for /f "delims=" %%i in (cInputFile) do sleep 1 && echo %%i
and run it something like:
input.bat | create.bat >> cLogFile
So, I have a program for the Windows Command Prompt, which is for changing the directory (so I can go to a language directory without having to do cd everytime)
and I want to kill it after I select an option. However, I have tried some
methods, which do one of the following:
a. taskkill /IM ... (blows up with a process not found error)
and
b. exit [as shown here] (does what I want, but it also closes the command prompt)
This is my program (the important part):
:C++
cd C:\Users\S.G.\Documents\C++ Scripts
echo What's in C++ Scripts:
dir
pause
exit
:Python
cd C:\Users\S.G.\AppData\Local\Programs\Python\Python36-32
echo What's in Python:
dir
exit
The reason why I'm stuck as to how one does this is because say I choose option "C++".
If I choose it, it runs what I have described, but it also runs the "Python" function. If I run the "Python"function however, it runs fine and doesn't display whatever's in the "C++" function.
Why is function "C++" also running "Python" when I intend not to?
You should use exit /b, followed by an optional error code (eg. exit /b 0). An alternative way to do this is to skip to the end of the file using GOTO:EOF.
I have a batch file with the following commands (to set up a compiler):
del Yylex.java
jflex scanner.flex
del parser.java
java -jar java-cup-11a.jar parser.cup
However, for some reason, after the conclusion of jflex scanner.flex, the batch script ends and command prompt closes. If I just run that command separately, this does not happen. Does anyone know what's wrong?
Is jflex a batch file?
If so, try
CALL jflex ...
or
start /wait "" jflex ...
(well, actually - give it a whirl anyway, can't hurt...)
When bat is asked to run another batch, it merely transfers control to that other batch and has no idea of where to return. CALL or START gives it a ticket home...
I want to ask you all how to run batch files sequentially in Windows.
I have tried :
start /w batchfile_1.bat
start /w batchfile_2.bat
..
start /w batchfile_n.bat
but I have to close the previous .bat file process manually (e.g. by clicking) before continuing into the next one.
Is there any solution to do this automatically without me doing the manual closing previous .bat program every time?
Thanks a lot.
I would check the solutions to this question: Run Multiple batch files
Taken from the answer in the link.
Use call:
call bat1.cmd
call bat2.cmd
By default, when you just run a batch file from another one control will not pass back to the calling one. That's why you need to use call.
Basically, if you have a batch like this:
#echo off
echo Foo
batch2.cmd
echo Bar
then it will only output
Foo
If you write it like
#echo off
echo Foo
call batch2.cmd
echo Bar
however, it will output
Foo
Bar
because after batch2 terminates, program control is passed back to your original batch file.
If you are in love with using START, you could have your batch files end with the EXIT command. That will close the windows created by the start command.
#echo off
.
.
:: Inspired coding
.
.
exit
I'm not sure but based on your comments, the following seems to be happening when you run that sequence of START commands:
A START /W command is invoked and starts a batch file.
The batch file starts executing and runs a program.
The batch file finishes and its console window remains open, but the program continues running.
The START /W command that was used to run the batch file is still executing because the console window remains open.
You wait until the program terminates, then you close the console window, and then the next START /W command is invoked, and everything is repeated.
Now, if you place EXIT at the end of every batch file you want to run sequentially, that makes situation worse because it causes the console window to close after the batch script completes, and that in turn ends the corresponding START /W command and causes another one to execute, even though the program invoked by the batch script may still be running. And so the effect is that the batch scripts (or, rather, the programs executed by them) run simultaneously rather than sequentially.
I think, if this can be solved at all, you need to move the START /W command and put it in every batch file in front of (every) command that that batch file executes and doesn't wait for the termination of. That is, if your batchfile_1.bat runs a program.exe, change the command line to START /W program.exe, and similarly for other relevant commands in other batch files.