LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Scripts to check if a process is running (https://www.linuxquestions.org/questions/programming-9/scripts-to-check-if-a-process-is-running-341362/)

kt8993 07-08-2005 02:49 PM

Scripts to check if a process is running
 
Hi All,
I am using Mandrake10.1. I am running amule all the time, but sometimes this program ended unexpectedly while I was away.

How do I write a script to check if this process is running, if not run the process?
How do I automatically run this script every hour?

Thanks.

fouldsy 07-08-2005 02:57 PM

There's a nifty tool called "pidof" that basically runs through and checks for a running serivce and returns the pid of it. It's the same as running ps aux | grep amule and nothing the pid of it. I'm no expert, but something like:

Code:

$service=0
$service=pidof amule

if($service==0) {
    /path/to/amule
}

and then setup a cronjob to run this every 30 minutes. I don't know much about bash scripting, but that's the basic control loop, but the syntax is propbably wrong for bash. Have a read on bash scripting and cronttab for scheduling events. Shouldn't be too hard though.

eddiebaby1023 07-09-2005 05:11 PM

Code:

#!/bin/bash
while true
do
    [ ! /sbin/pidof amule ] && /path/to/amule
    sleep 3600
done


fouldsy 07-09-2005 05:32 PM

Shows how good my bash scripting is - think I'll stick to PHP! Least I was along the right lines :-)


All times are GMT -5. The time now is 08:19 AM.