LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to monitor some processes (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-monitor-some-processes-878059/)

alexandra12 04-30-2011 06:42 PM

How to monitor some processes
 
Hello,

Just looking for some advice. I am very new to Linux , so I am not that well experienced.

I need to create a small list of processes in a monitor.conf file. A shell script needs to check the status of these processes and restart if they are down. This shell script needs to be run every couple of minutes.

The output of the shell script needs to be recorded in a log file.


So far I have created a blank monitor.conf file.
I have gotten the shell script to automatically updated every couple of minutes
The Shell script also sends some default test information to the log file.

Can anyone guide me in how I go about doing this part ?
A shell script needs to check the status of these processes and restart if they are down.


I have put in the conf file the below commands but I am not sure if this is right.

ps ax | grep httpd
ps ax | grep apache


I also dont know if the shell script should read from the conf file or if the conf file should send information to the shell script file.

Thanks in advance

Any advice would be greatly appreciated !

paulsm4 04-30-2011 08:34 PM

Hi -

You're definitely on the right track. A couple of suggestions:

1. "cron" would be a great way to periodically run your scripts

2. "ps ax|grep httpd" will work if your Apache daemon is named "httpd"; otherwise "ps ax|grep apache" will work if it's named "apache2". It could be either one, depending on your Linux distro and version.

This syntax, however, will give you an EXTRA line: you'll also see an entry for the command itself. You can eliminate the extra line with the syntax "ps ax|grep httpd|grep -v grep".

Another way to check the status, without using "ps", is:

"/etc/init.d/httpd status"
... or, equivalently,
"apache2ctl status"

3. If you need to (re)start it from your script, the syntax is:
"/etc/init.d/httpd restart"

'Hope that helps .. PSM

alexandra12 05-01-2011 08:58 AM

Thanks Paul,


At least I know I am on the right track

I have added the below to my test.sh file

Quote:

cat 'etc/monitor.conf' | more
while read line; do
echo cat "$line" >> /var/log/monitor.log
done
fi
fi
I have added the below into my monitor.conf file
Quote:

# used for monitoring some of the Ubuntu services

ps ax | grep Xorg|grep -v grep
ps ax | grep cpuset |grep -v grep
ps ax | grep watchdog |grep -v grep
ps ax | grep bluetooth |grep -v grep
ps ax | grep events |grep -v grep
1st to make sure that my test.sh file is reading the monitor.conf file
I want my test.sh file to read the monitor.conf file one line at a time and print the information into the monitor.log file

can you tell me if there is something wrong with my statement in the test.sh file ???

lisle2011 05-01-2011 12:22 PM

Scripts
 
Both of the files you posted do not work as you intended.

I am working on them and will post something that works for you.
.

.

.

paulsm4 05-01-2011 03:09 PM

Hi, alexandra12 -

1. Here's a quick'n'dirty example:
Code:

monitor.lst:
-----------
apache
mysql
Xorg
cpuset
watchdog
bluetooth
events
cpuset
watchdog
bluetooth
events

Code:

monitor.sh:
----------
files=`cat monitor.lst`
for f in $files; do
  if (ps -eaf|grep $f|grep -v grep > /dev/null); then
    echo "process $f is running..."
  else
    echo "process $f is NOT running!"
  fi
done

Quote:

Sample output:

./monitor.sh
process apache is running...
process mysql is running...
process Xorg is NOT running!
process cpuset is NOT running!
process watchdog is NOT running!
process bluetooth is NOT running!
process events is NOT running!
process cpuset is NOT running!
process watchdog is NOT running!
process bluetooth is NOT running!
process events is NOT running!
Hopefully it's a useful starting point for you.

2. I'd also encourage you to look into "cron" (I gave a link in my earlier post).

3. Finally, please Google for "open source monitoring tools" (or a similar search). You might find a package that's already written, and ideal for your purposes.

lisle2011 05-01-2011 11:02 PM

catching processes
 
It might be useful for us to know what you are doing and what the problem is.

However the posting by paulsm4 will not guarantee that the process is or isn't running. I have tested it and it returns processes that I just switched off as running.

Also you might consider reading the bash book because the script you wrote lacks some basics i.e the sha bang and /bin/bash or sh or whatever.

I think paulm4's advice is good. I am sure this is not a new desire for many users and it is bound to be solved. You just need to hunt the resource down.


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