2012-09-12 41 views
5

我正在研究一個小型bash腳本,該腳本計算具有特定名稱的腳本運行的頻率。簡單的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 

上面的腳本是我到目前爲止,但它不工作。我得到這個錯誤:

[[email protected] crons]# ./check_data.sh 
./check_data.sh: line 4: [: missing `]' 
wc: invalid option -- e 

我在做錯誤的if語句?

回答

7

您的測試語法不正確,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 
+0

謝謝你,工作! –

+0

我在CentOS 6.5上,我不確定爲什麼'-lt'不適合我,用'-gt'代替它適用於我。 – hailong

2

如果你使用Bash然後下降[-lt並使用((進行算術比較。

ps提供了-C開關,該開關接受要查找的進程名稱。
grep -v欺騙只是黑客。

#!/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 
1

計數行不是必需的。只是檢查的grep返回值:

if ! ps -ef | grep -q '[s]crape_data.php' ; then 
    ... 
fi 

的[S]招避免了grep -v grep

0

雖然最高票數的答案確實有效,但我有一個解決方案,我用於我的刮刀工作。

<?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

,然後就等待腳本的第一次迭代完成。之後,它會在後臺運行,您可以通過命令行或類似的工具(如htop)查看是否使用了pid計數。

這不是迷人的,但它的工作原理。 :)