3

This

#!/bin/bash
if [ `ps -ef | grep "91.34.124.35" | grep -v grep | wc -l` -eq 0 ]; then sh home/asfd.sh; fi

or this?

ps -ef | grep "91\.34\.124\.35" | grep -v grep > /dev/null
if [  "$?" -ne "0" ]
then
sh home/asfd.sh
else
echo "Process is running fine"
fi

Hello, how can I write a shell script that looks in running processes and if there isn't a process name CONTAINING 91.34.124.35 then execute a file in a certain place and I want to make this run every 30 seconds in a continuous loop, I think there was a sleep command.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Tobis
  • 51
  • 1
  • 1
  • 2

9 Answers9

4

you can't use cron since on the implementation I know the smallest unit is one minute. You can use sleep but then your process will always be running (with cron it will started every time).

To use sleep just

while true ; do
  if ! pgrep -f '91\.34\.124\.35' > /dev/null ; then
    sh /home/asfd.sh
  fi
  sleep 30
done

If your pgrep has the option -q to suppress output (as on BSD) you can also use pgrep -q without redirecting the output to /dev/null

Matteo
  • 14,696
  • 9
  • 68
  • 106
  • +1; I think you have the logic backward, though - prefix the `if` conditional with `!`. Also, by using `pgrep` - which always excludes itself - instead of `ps` + `grep`, the `if` statement can be simplified to: `if ! pgrep -f '91\.34\.124\.35' > /dev/null; then` – mklement0 Feb 17 '14 at 19:29
  • @mklement0 Yep I didn't notice the negation in question. I corrected my answer. Thanks – Matteo Feb 17 '14 at 20:16
  • Thanks, but `pgrep -q` doesn't work on Linux, unfortunately; hence my `> /dev/null` redirection; also, (a) you need `-f` in order to match against the entire _command line_, not just the process name, and (b) you don't need the `[9]` trick when you use `pgrep` (`pgrep` doesn't match itself by default); if you update again, please also reflect the changes in the bullet points below the code. – mklement0 Feb 17 '14 at 20:18
  • Added -f and commented about GNU/BSD versions – Matteo Feb 17 '14 at 20:26
2

First of all, you should be able to reduce your script to simply

if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi

To run this every 30 seconds via cron (because cron only runs every minute) you need 2 entries - one to run the command, another to delay for 30 seconds before running the same command again. For example:

* * * * * root if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi
* * * * * root sleep 30; if ! pgrep "91\.34\.124\.35" > /dev/null; then ./your_script.sh; fi

To make this cleaner, you might be able to first store the command in a variable and use it for both entries. (I haven't tested this).

CHECK_COMMAND="if ! pgrep '91\.34\.124\.35' > /dev/null; then ./your_script.sh; fi"

* * * * * root eval "$CHECK_COMMAND"
* * * * * root sleep 30; eval "$CHECK_COMMAND"

p.s. The above assumes you're adding that to /etc/crontab. To use it in a user's crontab (crontab -e) simply leave out the username (root) before the command.

Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
1

I would suggest using watch:

watch -n 30 launch_my_script_if_process_is_dead.sh
podshumok
  • 1,649
  • 16
  • 20
0

You can save your script in file name, myscript.sh

then you can run your script through cron,

 */30 * * * * /full/path/for/myscript.sh

or you can use while

 # cat script1.sh
 #!/bin/bash
 while true; do /bin/sh /full/path/for/myscript.sh ; sleep 30; done &

 # ./script1.sh

Thanks.

Ranjithkumar T
  • 1,886
  • 16
  • 21
0

I have found deamonizing critical scripts very effective.

http://cr.yp.to/daemontools.html
BillyBigPotatoes
  • 1,330
  • 11
  • 23
0

You can use monit for this task. See docu. It is available on most linux distributions and has a straightforward config. Find some examples in this post

For your app it will look something like

check process myprocessname
    matching "91\.34\.124\.35"
    start program = "/home/asfd.sh"
    stop program = "/home/dfsa.sh"

If monit is not available on your platform you can use supervisord.

I also found this question very similar Repeat command automatically in Linux. It suggests to use watch.

jschnasse
  • 8,526
  • 6
  • 32
  • 72
0

Either way is fine, you can save it in a .sh file and add it to the crontab to run every 30 seconds. Let me know if you want to know how to use crontab.

roymustang86
  • 8,054
  • 22
  • 70
  • 101
0

Try this:

if ps -ef | grep "91\.34\.124\.35" | grep -v grep > /dev/null
then
    sh home/asfd.sh
else
    echo "Process is running fine"
fi

No need to use test. if itself will examine the exit code.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • a bit simpler: `if ps -ef | grep -q "[9]1\.34\.124\.35"; then ...`. The square brackets help filter out the grep process from the results. – glenn jackman Aug 26 '11 at 15:54
-1

Use cron for the "loop every 30 seconds" part.

Kheldar
  • 5,361
  • 3
  • 34
  • 63