0

I have a .cmd batch file (let's call it RunSQlCmd.cmd) which pipes out output of sqlcmd to 7 zip compressor

sqlcmd -i.\table.sql -S . -E -s "," -I -h -1 -W| "c:\Program Files\7-Zip\7z.exe" a -tbzip2 -si "out.csv.bz2"

I run it from Azure Batch in a task from a C# driver program with the following command line

(1) cmd /c %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd

But what seem to happen is that command line returns almost immediately and commands in RunSqlCmd.cmd are not fully executed and empty compressed archive is created. Azure Batch task exits with success code 0.

If I change task's command line to

(2) cmd /c start /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd

commands in a batch file run successfully but stdout and stderr is lost for Azure Batch as batch file runs in a separate cmd window and task hangs up without getting any error code.

changing tasks's command line to

(3) cmd /c start /B /wait %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd

is similar to (1)

What is the correct way to do it so that Azure Batch detects correctly when task is finished and RunSqlCmd.cmd command finishes completely?

P.S. Real content of RunSqlCmd.cmd file is as @echo Run SqlCmd sqlcmd -i "%~dp0%1.sql" -d dbName -S serverName -U userName -P "password" -s "," -I -h -1 -W -b | "%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 -si "%~dp0%1.csv.bz2" @echo Done SqlCmd

It takes 1 parameter - name of sql file to pull data

1
  • Apparently from 'start /wait' documentation "If it is an internal cmd command or a batch file then the command processor is run with the /K switch to cmd.exe. This means that the window will remain after the command has been run." So it is default /K switch added by 'start /wait' which is a problem. If I change it to 'start /wait cmd /c %AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd' it all works but stdout and stderr is all lost
    – YuGagarin
    May 29, 2017 at 8:12

1 Answer 1

1

I do not quite understand why you want to run cmd /c and start /wait to kick off a program. you do not need to call cmd.exe from start and do not need to call cmd to start a batch file.

if it is a batch file, you can just call the batch file. it will automatically wait internally for the command to complete.

%AZ_BATCH_NODE_SHARED_DIR%\RunSqlCmd.cmd

That should work as is without calling start or cmd again.

The main problem is that you are sending the command with parameters in quotes to external program, so you you call cmd again and it sends the commands to this cmd window and stripping out quotes when it sends to the external. Just calling the script as is will do.

2
  • Thanks for your comment, but I did try to run .cmd file by itself and it did not work. The only way to make it work under Azure Batch is to use cmd /c start /wait cmd /c RunSqlCmd.cmd, which is way off in my point of view and thus original question here...
    – YuGagarin
    May 29, 2017 at 11:42
  • The actual failure is as follows when RunSqlCmd.cmd is run by itself from Azure Batch prospective "WARNING: Task [topNtask0] returned a non-zero exit code - this may indicate task execution or completion failure."
    – YuGagarin
    May 29, 2017 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.