2017-01-06 17 views
-1

シェルスクリプトを実行できる回数を制限するにはどうすればよいですか。私はshcを試しましたが、これには時間制限と使用制限がありません。シェルスクリプトの実行回数を制限する

+1

あなたは何を意味するのですか?スクリプトが並行して呼び出されないようにしたいのですか、またはn回以上実行するとスクリプトの実行を拒否しますか? – choroba

+0

はい、たとえば、スクリプトが3回実行されたとします.4回目に実行しないようにしてください。[email protected]に連絡してください。 –

+1

代わりにスクリプトを実行しますか? – choroba

答えて

0

"実行カウンタ"としてファイルを使用し、実行中にそのファイルを読み取って、スクリプトが以前に実行された回数を確認することができます。

再起動後も「numOfRuns.txt」ファイルを保持したい場合は、ルートとして/ tmp以外のディレクトリを使用してください。

limitedScript.shここには、最初にコメントが記載されていません。

#!/bin/bash 

runCountFile="/tmp/numOfRuns.txt" 
maxRuns=3 

if [ -e "$runCountFile" ]; then 
    read value < "$runCountFile" 
else 
    value=0 
fi 

if ((value >= maxRuns)); then 
    echo "Script has been run too many times" 
    exit 
else 
    newValue=$((value + 1)) 
    echo $newValue > "$runCountFile" 
fi 

-

#!/bin/bash 

# limitedScript.sh: Demonstrates simple run-limiting through a file-based counter 

runCountFile="/tmp/numOfRuns.txt" # the file to store the number of times the script has run 
maxRuns=3 # maximum number of executions for this script 

if [ -e "$runCountFile" ]; then # does the run counter file exist? 
    value=`cat $runCountFile` # read the value from the file 
else 
    value=0 # the script has never run yet if the "run counter" file doesn't exist 
fi 

if ((value >= maxRuns)); then 
    echo "Script has been run too many times" 
    exit 
else 
    newValue=$((value + 1)) 
    echo $newValue > "$runCountFile" #update the "run counter" file 
fi 

OUTPUT:

[email protected]:/tmp# rm numOfRuns.txt 
[email protected]:/tmp# ./limitedScript.sh 
[email protected]:/tmp# ./limitedScript.sh 
[email protected]:/tmp# ./limitedScript.sh 
[email protected]:/tmp# ./limitedScript.sh 
Script has been run too many times 
+0

'read $ <" $ runCountFile "'であれば、外部プログラムを実行する必要はありません。 – chepner

+0

ありがとうございましたchepner - updated – nanch

+0

これは遠隔から行うため、サーバーを設定した以上の量のスクリプトを使用できないようにするか、それは不可能です。 –

関連する問題