Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple bash script count running processes by name

Tags:

bash

ssh

cron

i'm working on a small bash script which counts how often a script with a certain name is running.

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

is the code i use, via ssh it outputs the number of times scrape_data.php is running. Currently the output is 3 for example. So this works fine.

Now I'm trying to make a little script which does something when the count is smaller 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'm getting this error:

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

What am I doing wrong in the if statement?

like image 632
Mr.Boon Avatar asked Sep 12 '12 17:09

Mr.Boon


People also ask

How do I find the number of processes with the same name in Linux?

Find how many processes are running in Linux One can use the ps command along with with the wc command to count the number of processes running on your Linux based system by any user.

How do I count running processes in Linux?

You can use ps (will show snapshot of processes) with wc (will count number of words, wc -l option will count lines i.e. newline characters). Which is very easy and simple to remember. This simple command will print number of processes running on current server.

How can I tell how many processes are running?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How do I see what processes are running in bash?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.


2 Answers

Your test syntax is not correct, the lt should be within 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

or you can test the return value of pgrep:

pgrep scrape_data.php &> /dev/null

if [ $? ]; then
  echo no launch
fi
like image 75
perreal Avatar answered Sep 18 '22 13:09

perreal


if you're using Bash then drop [ and -lt and use (( for arithmetic comparisons.

ps provides the -C switch, which accepts the process name to look for.
grep -v trickery are just hacks.

#!/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
like image 22
c00kiemon5ter Avatar answered Sep 17 '22 13:09

c00kiemon5ter