

InterviewSolution
Saved Bookmarks
1. |
Solve : Dos Batch: Kill parallel jobs at the same time? |
Answer» <html><body><p>I am running the bat job(main bat) and that will call the two bats parallely at the same time,<br/><br/>I have tried to end the job(main bat) by ctrl+c then one bat file is getting ended, but the other bat file is still running,so again I have to do the ctrl+c to end the other bat file.<br/><br/>So how can I kill all the bat files at the same time and also how can i program this in the main bat job so that all the parallel jobs gets ended when I use ctrl+c.<br/><br/><br/><br/>Can some body please help me on this.<br/><br/><br/><br/>Thanks in advance. Quote</p><blockquote>I am running the bat job(main bat) and that will call the two bats parallely at the same time,<br/></blockquote> <br/>How are you doing that? When a call is made, the calling file enters a wait state and the called file begins executing. When the called file ends, control is returned to the calling file at the next sequential instruction after the call. This occurs no matter how <a href="https://interviewquestions.tuteehub.com/tag/many-554478" style="font-weight:bold;" target="_blank" title="Click to know more about MANY">MANY</a> levels of calls are used. <a href="https://interviewquestions.tuteehub.com/tag/using-1441597" style="font-weight:bold;" target="_blank" title="Click to know more about USING">USING</a> ctl+C will terminate the entire run unit.<br/><br/>It would be helpful if you posted your code and please mention your OS.<br/><br/> Thankyou..<br/><br/>Following is the code of the main bat file..<br/><br/>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MAbatch\jobs\old_Campaign_DB_Personal.bat<br/>call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MAbatch\jobs\Count_db_pers.bat<br/><br/>Following is the code for the 2 bat files..<br/><br/>Following is the code of the first bat file...<br/><br/>setlocal<br/>REM *********************************************************<br/>REM * Rundate 'D'YYMMDD must be specified as first argument *<br/>REM *********************************************************<br/><br/>REM *********************************************************<br/>REM * Command-file must exist *<br/>REM *********************************************************<br/>FOR /R D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\ %%G IN (*.bat) DO start /b "call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/><br/><br/>set rc=%errorlevel%<br/>REM *******************************************************<br/>REM * Exit script and report return code *<br/>REM *******************************************************<br/>:done<br/>echo rc=%rc%<br/>exit /b %rc%<br/><br/>REM *******************************************************<br/>REM * Handle argument errors *<br/>REM *******************************************************<br/>:noargument<br/>echo Error: No argument specified<br/>set rc=2<br/>goto done<br/>:nopgm<br/>echo Error: Command file "%sch_command%" does not exist<br/>set rc=2<br/>goto done<br/><br/>Following is the code of the second bat file.<br/><br/>echo off & setLocal enableDELAYedexpansion<br/>set toterrors=<br/>set totjobs=<br/>for /f "tokens=1 delims= " %%a in (D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\count\count.txt) do (<br/>set /a toterrors+=%%a<br/>)<br/>for /f "tokens=2 delims= " %%b in (D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\count\count.txt) do (<br/>set /a totjobs+=%%b<br/>)<br/>echo total errors is !toterrors!<br/>echo total jobs is !totjobs!<br/>goto :<a href="https://interviewquestions.tuteehub.com/tag/eof-446162" style="font-weight:bold;" target="_blank" title="Click to know more about EOF">EOF</a> <br/>Wow!<br/><br/><strong>Main.bat</strong><br/> Quote<blockquote>D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MAbatch\jobs\old_Campaign_DB_Personal.bat<br/>call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MAbatch\jobs\Count_db_pers.bat<br/></blockquote> <br/>The first statement will transfer control to <em>old_Campaign_DB_Personal.bat</em>. There is no mechanism to return, the next statement (call) will never <a href="https://interviewquestions.tuteehub.com/tag/get-11812" style="font-weight:bold;" target="_blank" title="Click to know more about GET">GET</a> executed.<br/><br/><strong>First.bat</strong><br/> Quote<blockquote>FOR /R D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\ %%G IN (*.bat) DO start /b "call D:\DAT\SAS\DMT\Lev1\SASMain\Data\MAMisc\MASched\jobs\DB_Personal\" %%G<br/></blockquote> <br/>Choose either <strong>start</strong> or <strong>call</strong> but not both. Note the /b switch on the <strong>start</strong> command disables CTL+C. <br/><br/>The <strong>Second.bat</strong> file seems OK.<br/><br/>It may help to map out what you expect to happen, read up on the both the start and call commands and go from there.<br/><br/>Good luck.</body></html> | |