interrupt batch file

nobby87

Reputable
Mar 16, 2014
5
0
4,510
Hi everyone. forum newbie here.
I am trying to find a way to interrupt a running batch file. Basically, my reason for this is I have some batch files that carry out a backup process of data. This data is stored across a network and backs up to a seperate server. occasionally certain backups fail to complete 100% due to an intermittent connection (this has been resolved). As a precautionary, I have wrote a script that will monitor the network connection at all times while the backup is running. When it detects an drop in the connection and returns an errorlevel of 1, I want it to interrupt the batch file running the backup until it detects the network and then carry on. I have scoured the net and so far have found nothing so i am thinking this may not be possible, I am also open to using powershell.

Cheers guys.
 

nobby87

Reputable
Mar 16, 2014
5
0
4,510
The scripts run automatically during the night so there can be no operator input whatsoever. That's why I need the other batch file to be able to intervene.
 
lol... just found a neat way to pause the batch, throw a

ping -n 30 ip_adress

at it, it will cause the job to stop for 30 seconds

or:
detects a Ctrl-C or Ctrl-Break when ON BREAK is not in use, it displays a prompt, for example:

Cancel batch job C:\CHARGE.BTM ? (Y/N/A) :

Enter N to continue, Y to terminate the current batch file and continue with any batch file which called it, or A to end all batch file processing regardless of the batch file nesting level. Answering Y is similar to the QUIT command; answering A is similar to the CANCEL command.
 

nobby87

Reputable
Mar 16, 2014
5
0
4,510
Going by Eximo's theory, lets say for example my back up script was this...(Its not...but lets pretend it is :) )

@echo off
start C:\users\aaronc\desktop\Test2.bat
timeout 15
ping -t 192.168.1.1




And I also have a script that is monitoring a connection to an IP address.....

@echo off
ECHO Establishing Connection to 192.168.1.1
Timeout 5
PING -n 1 192.168.1.1|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 ECHO Connection is UP
goto SUCCESS
IF ERRORLEVEL 1 goto :ERROR
goto :START

:START
PING -n 1 192.168.1.1|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :ERROR

:ERROR
ECHO Connection is DOWN
ECHO Pausing Backup
Timeout 5
PING -n 1 192.168.1.1|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto :SUCCESS
IF ERRORLEVEL 1 goto :ERROR

:SUCCESS
goto START



How do I embed the second script into the first one?

 

Eximo

Titan
Ambassador
Well for starters put all the code in a single batch file. When do you want the backup to occur? Only after a successful connection, so you want your monitoring script to execute first. Your backup command or call to the other script, could be placed in your SUCCESS section, and to have it pause or restart your backup.

Not easy to provide advice on interrupting your back up script without knowing what it actually is.

I can think of dozens of ways to write interrupts, but most involve switching to an actual scripting language, or something even more advanced. I commonly use WMI/vbscript to handle one-offs, but generally turn to more advanced tools for custom solutions.

I think a lot of what you are doing can be handled by robocopy which is part of Windows. Robocopy has some extended features you can make use of to resume a a failed backup, called Restart mode. Even without that, for temporary connection issues Robocopy, will soldier on until it is finished. But it does tend to have difficulties using raw IP addresses in a dynamic environment. Fully qualified names can work in your favor here.
 

nobby87

Reputable
Mar 16, 2014
5
0
4,510
"Well for starters put all the code in a single batch file."........This is what im trying to find out.
the back ups are scheduled to run at certain times during the night. The back up scripts are for work. All need the script to do is monitor the netowork connection to a specified IP address at the same time as the backups are running. If then half way through a backup the connection is lost, my script will recognise that and then force the backup to wait until the connection is restored and then carry on from where it left off and avoid missing any of the data it is supposed to back up.
 

Eximo

Titan
Ambassador
I meant that literally, take your written batch files and place them into a single file, then edit them, but for what you are trying to do it looks like you are re-inventing the wheel.

Robocopy has that function built in. I use it all the time for unattended file transfers on a wireless infrastructure.

robocopy.exe "C:\path" "\\Network\Path" /E /R:N /W:2 (Or network path to network path or whatever)

R:N is the number of retries N = 1000000 W:2 specifies 2 seconds between retries (2,000,000 seconds is a very long time) /E specifies to copy everything including folders and subfolders.

Robocopy is endlessly configurable, from writing log files, to excluding specific files and folders, all kinds of stuff.