Bash

bash 腳本。創建猴子測試工具安特克_____小號_一種n噸和rG這小號AntergOS

  • August 22, 2016

當我執行執行另一個 bash 腳本並遞歸執行自身的 bash 腳本時出現此錯誤。

在我使用的腳本xdotool中,創建計數器並在我使用的每個腳本中$RANDOM呼叫(甚至是$RANDOM在 while-loop 中呼叫的文件:最大 20 次迭代)並且每個命令都使用 echo ‘command’ 重定向流保存到文件中。

它給了我這個錯誤:

./somescript.sh: fork: Cannot allocate memory

關於作業系統:Antergos 64bit + Openbox + 8Gb RAM

已打開:qtcreator、lxterminal、我的 qt 應用程序 (100Mb) 現在日誌文件的大小為 3,5 МіB。我的ulimit -a

[user@workstation MonkeyClicker]$ ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31856
max locked memory       (kbytes, -l) 1024
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31856
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

我的run.sh腳本:

#!/bin/bash

if [ -z $1 ]
then
       echo ' '
       echo '***************MONKEY TESTING TOOL HELP***************'
       echo ' '
       echo 'command: run.sh [start delay]'
       echo '[start delay] - means that you have X seconds to start your program'
       echo 'for example : run.sh 3s 0.1s'
       echo ' '
else
       mkdir -p logScript
       ./monkeyTestingTool.sh $1 > $PWD/logScript/script.sh
fi

我的monkeyTestingTool.sh腳本:

!/bin/bash

if [ $1 ]
then
       echo '#Started in:' $date
       echo '#MONKEY TESTING START, BE CAREFUL!'
       echo '#You have' $1 'seconds to open your app for testing ...'
       sleep $1
fi

echo $(xdotool getmouselocation --shell)
echo 'xdotool mousemove --sync $X $Y'

RANGE=7
CHOOSE=$RANDOM
let "CHOOSE %= $RANGE"

case $CHOOSE in
0) sh ./move.sh
;;
1) sh ./callContextMenu.sh
;;
2) sh ./typeRandom.sh
;;
3) xdotool click 1 #this is leftMouseClick
;;
4) sh ./keyPressing.sh
;;
5) sh ./move.sh
;;
6) sh ./dragDrop.sh
#*) TODO: drag and drop
esac

./monkeyTestingTool.sh

例如我的callContextMenu.sh腳本:

#!/bin/bash

echo $(xdotool getmouselocation --shell)
echo 'xdotool mousemove --sync $X $Y'

echo 'xdotool click 3'
xdotool click 3

LASTKEY=0
RANGESTEPS=20
STEPS=$RANDOM
let "STEPS %= $RANGESTEPS"

while [ $STEPS != 0 ]; do
       RANGE=5
       CHOOSE=$RANDOM
       let "CHOOSE %= $RANGE"
       let STEPS=STEPS-1
       LASTKEY=$CHOOSE
       case $CHOOSE in
       0) xdotool key Up
       echo 'xdotool key Up'
       ;;
       1) xdotool key Left
       echo 'xdotool key Left'
       ;;
       2) xdotool key Down
       echo 'xdotool key Down'
       ;;
       3) if [ $STEPS == 1 ]
       then
               echo 'xdotool key Return'
               xdotool key Return
       fi
       ;;
       4) xdotool key Right
       echo 'xdotool key Right'
       ;;
       esac
done

if [ $LASTKEY != 3 ]
then
       echo 'xdotool key Return'
       xdotool key Return
else
       echo 'xdotool key Down'
       echo 'xdotool Return'
       xdotool key Down
       xdotool key Return
fi

你的最後一行monkeyTestingTool.sh電話本身

./monkeyTestingTool.sh

這意味著您很快就會執行該程序的數百個副本。

如果你想重新執行腳本,那麼最後一行

exec ./monkeyTestingTool.sh

或者把整個事情放在一個while循環中,類似於:

while [ 1 ]
do
 CHOOSE=$RANDOM
 let "CHOOSE %= $RANGE"

 case $CHOOSE in
 0) sh ./move.sh
 ;;
 ....
 esac
done

引用自:https://unix.stackexchange.com/questions/305060