Simple bash script counting running processes by name

I am working on a small bash script that counts how often a script with a specific name works.

ps -ef | grep -v grep | grep scrape_data.php | wc -l

- this is the code that I use, through ssh it displays the number of scrape_data.php starts. Currently, the output is, for example, 3. So this works great.

Now I am trying to make a little script that does something when the amount is less than 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

The script above is what I have so far, but it does not work. I get this error:

[root@s1 crons]# ./check_data.sh
./check_data.sh: line 4: [: missing `]'
wc: invalid option -- e

What am I doing wrong in an if statement?

+5
source share
4 answers

The test syntax is incorrect, ltmust be in the test bracket:

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

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

. grep:

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

[s] grep -v grep.

+1

, , , .

<?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 (, php script OP), PHP script?

, script :

php go_get.php

and then just wait for the first iteration of the script to finish. After that, it starts in the background, which you can see if you use pid counting from the command line or a similar tool, for example htop.

This is not glamorous, but it works. :)

0
source

All Articles