![](/img/trans.png)
[英]Bash script with basic if/else and count of processes running based on a pattern
[英]Simple bash script count running processes by name
我正在使用一個小的bash腳本來計算具有特定名稱的腳本運行的頻率。
ps -ef | grep -v grep | grep scrape_data.php | wc -l
是我使用的代碼,通過ssh輸出scrape_data.php運行的次數。 目前輸出為3例如。 所以這很好。
現在我正在嘗試制作一個小小的腳本,當計數小於1時會做一些事情 。
#!/bin/sh
if [ ps -ef | grep -v grep | grep scrape_data.php | wc -l ] -lt 1; then
exit 0
#HERE PUT CODE TO START NEW PROCESS
else
exit 0
fi
上面的腳本是我到目前為止,但它不起作用。 我收到這個錯誤:
[root@s1 crons]# ./check_data.sh
./check_data.sh: line 4: [: missing `]'
wc: invalid option -- e
我在if語句中做錯了什么?
您的測試語法不正確, lt
應該是測試支架內:
if [ $(ps -ef | grep -v grep | grep scrape_data.php | wc -l) -lt 1 ]; then
echo launch
else
echo no launch
exit 0
fi
或者你可以測試pgrep
的返回值:
pgrep scrape_data.php &> /dev/null
if [ $? ]; then
echo no launch
fi
如果您正在使用Bash
則刪除[
和-lt
並使用((
用於算術比較)。
ps
提供-C
開關,它接受要查找的進程名稱。
grep -v
trickery只是黑客攻擊。
#!/usr/bin/env bash
proc="scrape_data.php"
limit=1
numproc="$(ps hf -opid,cmd -C "$proc" | awk '$2 !~ /^[|\\]/ { ++n } END { print n }')"
if (( numproc < limit ))
then
# code when less than 'limit' processes run
printf "running processes: '%d' less than limit: '%d'.\n" "$numproc" "$limit"
else
# code when more than 'limit' processes run
printf "running processes: '%d' more than limit: '%d'.\n" "$numproc" "$limit"
fi
不需要計算線條。 只需檢查grep
的返回值:
if ! ps -ef | grep -q '[s]crape_data.php' ; then
...
fi
[s]技巧避免了grep -v grep
。
雖然最高投票的答案確實有效,但我有一個解決方案,我用我的刮刀對我有用。
<?php
/**
* Go_Get.php
* -----------------------------------------
* @author Thomas Kroll
* @copyright Creative Commons share alike.
*
* @synopsis:
* This is the main script that calls the grabber.php
* script that actually handles the scraping of
* the RSI website for potential members
*
* @usage: php go_get.php
**/
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
// script execution timing
$start = microtime(true);
// how many scrapers to run
$iter = 100;
/**
* workload.txt -- next record to start with
* workload-end.txt -- where to stop at/after
**/
$s=(float)file_get_contents('./workload.txt');
$e=(float)file_get_contents('./workload-end.txt');
// if $s >= $e exit script otherwise continue
echo ($s>=$e)?exit("Work is done...exiting".PHP_EOL):("Work is not yet done...continuing".PHP_EOL);
echo ("Starting Grabbers: ".PHP_EOL);
$j=0; //gotta start somewhere LOL
while($j<$iter)
{
$j++;
echo ($j %20!= 0?$j." ":$j.PHP_EOL);
// start actual scraping script--output to null
// each 'grabber' goes and gets 36 iterations (0-9/a-z)
exec('bash -c "exec nohup setsid php grabber.php '.$s.' > /dev/null 2>&1 &"');
// increment the workload counter by 36 characters
$s+=36;
}
echo PHP_EOL;
$end = microtime(true);
$total = $end - $start;
print "Script Execution Time: ".$total.PHP_EOL;
file_put_contents('./workload.txt',$s);
// don't exit script just yet...
echo "Waiting for processes to stop...";
// get number of php scrapers running
exec ("pgrep 'php'",$pids);
echo "Current number of processes:".PHP_EOL;
// loop while num of pids is greater than 10
// if less than 10, go ahead and respawn self
// and then exit.
while(count($pids)>10)
{
sleep(2);
unset($pids);
$pids=array();
exec("pgrep 'php'",$pids);
echo (count($pids) %15 !=0 ?count($pids)." ":count($pids).PHP_EOL);
}
//execute self before exiting
exec('bash -c "exec nohup setsid php go_get.php >/dev/null 2>&1 &"');
exit();
?>
雖然這看起來有點矯枉過正,但我已經在使用PHP來抓取數據(比如你在OP中的php腳本),那么為什么不使用PHP作為控制腳本呢?
基本上,你會像這樣調用腳本:
php go_get.php
然后等待腳本的第一次迭代完成。 之后,它在后台運行,如果您從命令行使用pid計數,或者像htop
這樣的類似工具,您可以看到它。
它並不富有魅力,但它確實有效。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.