我有以下脚本(在解释和问题的最后)。
有两个function。 在第一个函数中,调用firefox -print扩展。 一切顺利,如果我逐行运行它。
问题是,在一个bash脚本中,firefox会在几秒钟内被调用很多次,而不是一次调用一次,等待完成。
我不想在function上增加睡眠,因为我不知道需要多长时间。
有什么办法强制bash不启动线程? (我想这是做的)
谢谢。
这是我的脚本(因为命令let是bash而不是sh)
#!/bin/bash ####TODO### ####variables # join files # send with attachment ## Date, to be used as reference on tmp files DATE=`date +%Y%m%d` ## File where all webs to report are listed WEBSFILE="/home/marc.riera/Desktop/reports/websfile.txt" ## Where to store all the files. WORKDIR="/home/marc.riera/Desktop/reports" ##################do not edit under this line TMPDIR=$WORKDIR/$DATE test -d $TMPDIR || mkdir -p $TMPDIR && echo "Create folder $TMPDIR for temporal usage" REPORT=$WORKDIR/Report_$DATE.pdf ##firefox -print "http://fbmsgga01/ganglia/?m=cpu_report&r=month&s=descending&c=CPU+cluster&h=&sh=1&hc=4&z=small" -printmode pdf -printdelay 5 -printfile ~/Desktop/reports/test.pdf firefoxprint (){ web2Print=$1 outputFile=$2 echo -n "printing $1 on $2" firefox -print "$web2Print" -printmode pdf -printdelay 5 -printfile $outputFile } ##gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=dospdf.pdf -dBATCH ganglia.pdf test.pdf pdfjoin (){ outputFile=$1 origA=$2 origB=$3 echo -n "Joining $origA and $origB into $outputFile" gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=$outputFile -dBATCH $origA $origB } ###################################### MAIN ####################################### clear COUNTER=0 for web in `cat $WEBSFILE|grep -v '^#'` do firefoxprint $web $TMPDIR/$COUNTER.pdf let COUNTER+=1 done echo "--------------------" $COUNTER while [ $COUNTER -gt 0 ]; do let COUNTER=COUNTER-1 pdfjoin $REPORT $REPORT $TMPDIR/$COUNTER.pdf done echo "done" exit 0
如果有一个正在运行的Firefox,对Firefox的调用只是接触它,传输打印请求,并立即退出。 打印请求在运行实例中asynchronous执行。 打印完成后,我不知道是否有办法得到通知。
解决方法(未经testing)将在您的脚本中创build一个新的configuration文件(以随机名称解压一个准备好的zip文件)。 只要他们使用不同的configuration文件,你可以运行多个firefox实例。 一旦firefox退出,清除临时configuration文件。 这假定打印扩展不会离开firefox实例运行; 如果是这样,一旦完成,你可以破解它退出Firefox。
我不太熟悉使用FireFox打印,但是你有没有玩过bash的wait命令? 你可以喂它特定的PID等待。 所以通过遵循你的一个firefox打印命令,你可以wait $! 。 或者,如果您不指定任何内容,只需使用wait ,则只需等待所有subprocess完成即可。
我不能保证FireFox能够很好地玩这个游戏。 但值得一试。
在firefoxprint()例程的末尾添加:
while [ ! -s $outputFile ]; do sleep 1 done
这会让你至less有一些延迟 – 在继续之前等待有一个非零长度的输出文件。 希望这会减慢足够的后续调用将使用现有的Firefox,而不是认为他们必须自己产卵。
当你没有指定一个完整的目录的时候调用firefox ,它会运行一个包装脚本来exec实际的可执行文件。 这可能会导致您的问题或可执行文件可能会做一个双叉,导致它在后台运行而不使用& 。 我不知道如何防止这种情况。
只有一个等待。
a)在Firefox上创build一个专门的configuration文件进行打印:
firefox -no-remote -ProfileManager
b)通过firefox -no-remote -P启动firefox
c)等待
好的,终于解决了。 感谢你的帮助。
还有一个问题:cron启动了没有envvariables的firefox,所以我不得不定义显示在哪里工作。
这是最后的代码。
#!/bin/bash #autor : Joan Marc Riera Duocastella. #license : BSD i guess. I just don't care. :) # firefox -print option depends on http://sites.google.com/site/torisugari/commandlineprint2 # it's kind of possible that a file inside de .xpi extension must be edited to get bigger pdf . Depends on the html you would like to print. Good html code does not need it. ####variables ## Date, to be used as reference on tmp files DATE=`date +%Y%m%d` ## File where all webs to report are listed WEBSFILE="/home/marc.riera/Desktop/reports/websfile.txt" # urls to print. lines starting with # will be ignored. ## Where to store all the files. WORKDIR="/home/marc.riera/Desktop/reports" MAILTO="[email protected]" # other mails can be added just with a single space between MAILTEXTFILE=$WEBSFILE FFPROFILE="cnm-profile" #FIREFOX PROFILE - create it with firefox -ProfileManager while no other firefox instances running ##################do not edit under this line #echo "$DATE - $0" logger $0 TMPDIR=$WORKDIR/$DATE test -d $TMPDIR && rm -f $TMPDIR/*.pdf|| mkdir -p $TMPDIR && echo "Create folder $TMPDIR for temporal usage" REPORT=$WORKDIR/Report_$DATE.pdf ##firefox -print "http://fbmsgga01/ganglia/?m=cpu_report&r=month&s=descending&c=CPU+cluster&h=&sh=1&hc=4&z=small" -printmode pdf -printdelay 5 -printfile ~/Desktop/reports/test.pdf firefoxprint (){ web2Print=$1 outputFile=$2 #echo "printing $1 on $2" env DISPLAY=:0.0 firefox -P $FFPROFILE -no-remote -print "$web2Print" -printmode pdf -printdelay 15 -printfile $outputFile & while [ ! -s $outputFile ]; do sleep 1 done } ##gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=dospdf.pdf -dBATCH ganglia.pdf test.pdf pdfjoin (){ outputFile=$1 tempdir=$2 cd $tempdir gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=$outputFile -dBATCH `ls -tr $tmpdir` wait } sendmail (){ #echo "Sending mail" mutt -s "Report del dia $DATE" -a $REPORT -- $MAILTO < $MAILTEXTFILE wait } ###################################### MAIN ####################################### COUNTER=0 for web in `cat $WEBSFILE|grep -v '^#'|grep -v '^$'` do firefoxprint $web $TMPDIR/$COUNTER.pdf let COUNTER+=1 done pdfjoin $REPORT $TMPDIR sendmail exit 0
我仍然需要改进,但是到现在为止我已经做到了。