简单的bash脚本按名称计算运行进程

时间:2012-09-12 17:26:52

标签: bash ssh cron

我正在开发一个小型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语句中做错了什么?

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:1)

不需要计算线条。只需检查grep的返回值:

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

[s]技巧避免了grep -v grep

答案 3 :(得分: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

然后等待脚本的第一次迭代完成。之后,它在后台运行,如果您使用命令行中的pid计数或类似htop之类的工具,则可以看到。

它并不富有魅力,但它确实有效。 :)