标题: 怎么实现同时运行5个子批处理哪?
[打印本页]
作者: honghunter
时间: 2007-4-28 21:40
标题: 怎么实现同时运行5个子批处理哪?
我的问题是,在一个批处理里面,有没有什么办法把批处理的内容,保持同一个时间运行多个哪?
比如我的批处理类似下面的情况,我想要达到的效果是,始终有且最多只有5个子批处理(book*_getpage.bat)在运行。
当然,我问的是,怎么能自动化的由BAT控制,保持同一个时间,有5个子批处理被启动运行。
每个book*_getpage.bat就是一个不断调用wget来下载小说的批处理。假设算是一个线程把,这里假设我抓取小说的站点,同时5个线程抓取是安全的(也就是不会被屏蔽IP)。
call sub_batch.bat book10766_心伤奇侠录_getpage.bat
call sub_batch.bat book11658_不能终止的游戏_getpage.bat
call sub_batch.bat book13269_ 志末_getpage.bat
call sub_batch.bat book2081_ 幽灵人间_getpage.bat
call sub_batch.bat book21187_落 日 之 战_getpage.bat
call sub_batch.bat book26632_黑暗帝国 原名 一剑天下_getpage.bat
call sub_batch.bat book26920_天道----沧桑柔情_getpage.bat
call sub_batch.bat book26939_恶魔果实の我是超能人_getpage.bat
call sub_batch.bat book27044_千年后的桃花运_getpage.bat
call sub_batch.bat book27078_异界幽灵龙骑士_getpage.bat
call sub_batch.bat book27082_人龙之光_getpage.bat
call sub_batch.bat book27094_神祸_getpage.bat
call sub_batch.bat book27205_昙花_getpage.bat
call sub_batch.bat book27281_飞龙在天_getpage.bat
call sub_batch.bat book27297_幻世奇行_getpage.bat
call sub_batch.bat book27383_死神之手_getpage.bat
call sub_batch.bat book27473_深山野匪_getpage.bat
call sub_batch.bat book27474_天书_getpage.bat
call sub_batch.bat book27522_魔剑_getpage.bat
call sub_batch.bat book27681_圣宗_getpage.bat
call sub_batch.bat book27901_龙驸马_getpage.bat
call sub_batch.bat book28047_幻剑天梦_getpage.bat
call sub_batch.bat book28141_南九卿之:玲珑公子妙郎君_getpage.bat
call sub_batch.bat book28334_天空的心_getpage.bat
call sub_batch.bat book28402_如梦令_getpage.bat
call sub_batch.bat book28425_黑龙舞兮云飞扬_getpage.bat
call sub_batch.bat book28595_神剑传说_getpage.bat
call sub_batch.bat book28676_因为爱,所以爱_getpage.bat
call sub_batch.bat book28687_寒剑梦醒_getpage.bat
call sub_batch.bat book28759_雪地阳天_getpage.bat
call sub_batch.bat book28770_雪地阳天_getpage.bat
call sub_batch.bat book28860_碧海幽燕_getpage.bat
……
作者: honghunter
时间: 2007-4-28 21:42
子批处理的内容类似下面这样:
IF NOT EXIST "book28999_酒剑情仇录\." mkdir "book28999_酒剑情仇录"
CD "book28999_酒剑情仇录"
call sub_get_book_page.bat 0 "http://club.book.sina.com.cn/yuanchuang/chapter.php?id=507434" "酒剑情仇录(1)" "book28999_酒剑情仇录"
call sub_get_book_page.bat 1 "http://club.book.sina.com.cn/yuanchuang/chapter.php?id=507435" "酒剑情仇录(2)" "book28999_酒剑情仇录"
call sub_get_book_page.bat 2 "http://club.book.sina.com.cn/yuanchuang/chapter.php?id=507436" "酒剑情仇录(3)" "book28999_酒剑情仇录"
call sub_get_book_page.bat 3 "http://club.book.sina.com.cn/yuanchuang/chapter.php?id=507437" "酒剑情仇录(4)" "book28999_酒剑情仇录"
call sub_get_book_page.bat 4 "http://club.book.sina.com.cn/yuanchuang/chapter.php?id=507438" "酒剑情仇录(5)" "book28999_酒剑情仇录"
作者: honghunter
时间: 2007-4-28 21:46
另外,说一下,算是一个经验教训吧。
以前我不知道有call命令,一直使用cmd /c来调用子批处理。
批量下载几个小时,cmd.exe就会报错退出,然后整个电脑的dos功能都失效,只能重新启动电脑……
作者: honghunter
时间: 2007-4-28 22:02
我现在想到的,是写个小程序,读入这一个BAT,然后输出成5个BAT,然后START这5个BAT。
但是这样需要另外写个小东西了。我想要知道有没有更加简便的实现方式。
作者: bjsh
时间: 2007-4-28 22:35
别用call ;
直接start 五个;
然后
tasklist /fi "IMAGENAME eq cmd.exe" /nh |findstr /n .*
来监视cmd.exe的个数;
利用 ping -n 次数自定 127.1 延时再循环;知道cmd.exe减少一个 则start 下一个;
比如
start sub_batch.bat book10766_心伤奇侠录_getpage.bat
start sub_batch.bat book11658_不能终止的游戏_getpage.bat
start sub_batch.bat book13269_ 志末_getpage.bat
start sub_batch.bat book2081_ 幽灵人间_getpage.bat
start sub_batch.bat book21187_落 日 之 战_getpage.bat
延时循环监视 cmd.exe的数量 提示用 (for提取前面的数字.注意减1)
把剩下的那些bat 放到 text.txt中;
用循环每次 提取一个来start;
作者: honghunter
时间: 2007-4-28 22:52
谢谢您的回复。
下面两个功能如何实现,对我也是问题,我先搜索一下。
1,延时循环监视 cmd.exe的数量 提示用 (for提取前面的数字.注意减1)
2,把剩下的那些bat 放到 text.txt中;
用循环每次 提取一个来start;
作者: honghunter
时间: 2007-4-28 23:02
找到读取特定行了。
"小函数"读取文件指定的特定行(ReadLine2.bat)
http://www.cn-dos.net/forum/view ... 6%96%87%E4%BB%B6###
作者: bjsh
时间: 2007-4-28 23:23
用不着这么复杂的;
比如test.txt内容如下
Quote: |
sub_batch.bat book28595_神剑传说_getpage.bat
sub_batch.bat book28676_因为爱,所以爱_getpage.bat
sub_batch.bat book28687_寒剑梦醒_getpage.bat
sub_batch.bat book28759_雪地阳天_getpage.bat
sub_batch.bat book28770_雪地阳天_getpage.bat
sub_batch.bat book28860_碧海幽燕_getpage.bat |
|
@echo off & setlocal enabledelayedexpansion
start sub_batch.bat book10766_心伤奇侠录_getpage.bat
start sub_batch.bat book11658_不能终止的游戏_getpage.bat
start sub_batch.bat book13269_ 志末_getpage.bat
start sub_batch.bat book2081_ 幽灵人间_getpage.bat
start sub_batch.bat book21187_落 日 之 战_getpage.bat
for /f "delims" %%a in (test.txt) do (
for /f "delims=:" %%i in ('tasklist /fi "IMAGENAME eq cmd.exe" /nh ^|findstr /n .*') do set "count=%%i"
set /a count=!count!-2
if !count! lss 5 start %%a
)
里面涉及的数字;请自行测试修改.
作者: honghunter
时间: 2007-4-28 23:41
我写的确实比较复杂了。
而且我这样读行有个问题,不知道什么时候结束。 :(
call set-env.bat
set /a LineNo=0
:check_process_num
tasklist /fi "IMAGENAME eq cmd.exe" /nh |findstr /n .* |findstr "6:cmd"
if "%errorlevel%"=="1" goto start_next
goto found_it
:found_it
echo sleep 1m
sleep 1m
goto check_process_num
:start_next
set /a LineNo=LineNo+1
echo LineNo=%LineNo%
call %pubbatch%\ReadLine2.bat subbat.txt %LineNo% >b:\tmp.bat
start "%LineNo%" /min b:\tmp.bat
REM pause
goto check_process_num
[
Last edited by honghunter on 2007-4-28 at 10:45 AM ]
作者: honghunter
时间: 2007-4-28 23:57
再次谢谢bjsh版主。
call set-env.bat
@echo off & setlocal enabledelayedexpansion
start sub_batch.bat book10766_心伤奇侠录_getpage.bat
start sub_batch.bat book11658_不能终止的游戏_getpage.bat
start sub_batch.bat book13269_ 志末_getpage.bat
start sub_batch.bat book2081_ 幽灵人间_getpage.bat
start sub_batch.bat book21187_落 日 之 战_getpage.bat
set /a LineNo=1
for /f "delims=:" %%a in (subbat.txt) do (
for /f "delims=:" %%i in ('tasklist /fi "IMAGENAME eq cmd.exe" /nh ^|findstr /n .*') do set "count=%%i"
set /a count=!count!-2
if !count! lss 5 (
start "!LineNo!" /min %%a
set /a LineNo=LineNo+1
)
echo sleep 5s in main batch.
sleep 5s
)