Logging the exit of a batch file - windows

So I wrote a tool with some batch commands, nothing specific. In the beginning the user can choose which task to perform thanks to a loop.
In that loop I included the "Q" option, as to quit the batch file. When this happens, it gets written to a logfile to check when the user started the script(s), and when it ended.
The issue is this only happens if the user actually quits/exits with Q. If (s)he quits by just closing the batch file, this won't be logged.
In short: how can I record when the user has quit the batch file without using the build-in function?

a batch file can't receive the "exiting"-event. What you can do is:
Make a launcher.bat file, that starts the original (yourfilename).bat file with:
start /wait (yourfilename).bat
the launcher.bat file will now wait until you close the second (yourfilename).bat file. place your log-information on the next line of launcher.bat
convert launcher.bat to launcher.exe using bat to exe converter (and make it invisible).

Related

how to get a stdout on a batch timeout indicating that a forced exit occurred

using windows schedular,
I am calling a batch file on windows:
C:\ETL\Scheduler\Schedule.bat >> C:\ETL\Logs\batchlog.log 2>&1
My problem is that Schedule.bat takes a really long time to run, and I have had cases where the batch file timesout.
I realize that windows scheduler will show the timeout and the close connection to the batch file,
but I'm wondering if there's a way to put in the line "forced timeout" or something similar into batchlog.log before the batch exists?
Thanks.
I'd suggest you control timing out yourself with a second script, that runs every minute, for example.
It can be like this:
1) At the start of the main script a lock file is created (and deleted when script finishes)
2) the watchdog script starts and checks the lock file every minute, comparing the lock time with the current time.
3) if watchdog script sees that lock file has not been deleted after a predefined time, it terminates the main script and writes corresponding message into a log file.

Keep a shell or batch file from proceeding until the previous run finishes

Is it possible for a batch program and shell script to know if a previous instance of itself is still running and wait until that program ends before triggering the next step in the command line?
We have a bat file and a corresponding shell that is triggered by an application which writes logs into a file, what happens when multiple users use the same application and triggers the program at the same time is that the logs get jumbled and is not readable by the program that turns the log into a PDF.
Thanks.

How to terminate only the inner batch job

Let's say there is some .bat file that is required to run inside .cmd batch script. This inner .bat file has a series of user interactions on a local host, and are only able to be ended using ctrl+c.
The question is: Is there some way to make the outer batch script resume after the inner script is terminated? Or is the ctrl+c the end all be all?
I've tried giving the inner script a different way out only to be told I'm not allowed to change that file. I've also done a fair amount of research and haven't found a solution. Forgive me if I've overlooked something! I'd like to avoid having two windows or extraneous termination messages pop up.
The only way I can think of to handle this is to use the following line in outer.cmd to call inner.bat -- with the disadvantage of receiving a new command prompt window for the execution of inner.bat:
start "" /WAIT cmd /C "inner.bat"
(Exchanging start and cmd does not work as the new window might unintentionally remain open.)
Note that for inner.bat, all the console input and output are handled via the new window, hence any redirections for outer.cmd (e. g., outer.cmd > "return.txt") will not include data from inner.bat.

Why is an error message output instead of starting an executable in subdirectory of batch file directory?

An error message is output when I try to start "launcher.exe" located in anylocation\ffa\ with this command:
start \ffa\launcher.exe
Batch script should have access to file because stored on disk like this:
ANYLOCATION/myprogram.bat
ANYLOCATION/ffa/launcher.exe
If I type
start /ffa/launcher.exe
output is: invalid switch
But if I type
start \ffa\launcher.exe
output is system cannot find file
Which mistake to I made on starting launcher.exe?
%~dp0
is your batch file directory. So
%~dp0\ffa\launcher.exe
There is no need to use start.
Starting a Program
See start /? and call /? for help on all three ways.
Specify a program name
c:\windows\notepad.exe
In a batch file the batch will wait for the program to exit. When
typed the command prompt does not wait for graphical
programs to exit.
If the program is a batch file control is transferred and the rest of the calling batch file is not executed.
Use Start command
start "" c:\windows\notepad.exe
Start starts a program and does not wait. Console programs start in a new window. Using the /b switch forces console programs into the same window, which negates the main purpose of Start.
Start uses the Windows graphical shell - same as typing in WinKey + R (Run dialog). Try
start shell:cache
Use Call command
Call is used to start batch files and wait for them to exit and continue the current batch file.

Looping a batch file that calls another batch file

My embedded C compiler generates a batch file to run various build outputs in a processor simulator from the command line. I'd like to create a batch file to call that auto generated batch file that is somewhere convenient, and I'd like to be able to rerun it just by clicking a key as often as I want all day long.
Here's what I've tried
cd "C:\foo\project name\settings\"
:repeat
cls
"Project Name.Unit Test Ouput.cspy.bat" & pause
goto repeat
I see the output I expect from the batch file, followed by a:
Press any key to continue . . .
When I press enter the script ends and never executes the goto.
If I remove the pause statement the script just ends immediately.
If I type & goto repeat the script still ends immediately.
CDing to the batch file from the command line, running it, and then click the up arrow and enter effectively does what I want... I am trying to automate it a tiny bit more.
To run a batch file from another batch file, you have to use the call statement. If you don't, then your outer batch file will end when the inner one does.
call "Project Name.Unit Test Ouput.cspy.bat" & pause

Resources