LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-30-2011, 07:42 PM   #1
alexandra12
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
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 !
 
Old 04-30-2011, 09:34 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 05-01-2011, 09:58 AM   #3
alexandra12
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
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 ???
 
Old 05-01-2011, 01:22 PM   #4
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.
.

.

.
 
Old 05-01-2011, 04:09 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.

Last edited by paulsm4; 05-01-2011 at 04:13 PM.
 
Old 05-02-2011, 12:02 AM   #6
lisle2011
Member
 
Registered: Mar 2011
Location: Surrey B.C. Canada (Metro Vancouver)
Distribution: Slackware 2.6.33.4-smp
Posts: 183
Blog Entries: 1

Rep: Reputation: 25
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Using ps to Monitor Processes LXer Syndicated Linux News 0 06-03-2009 04:51 AM
monitor processes to be notified when a new process is started? cfh Programming 2 10-03-2005 08:07 PM
Lots of Processes in system monitor gauntalus Linux - General 2 03-03-2005 01:03 PM
Monitor & respawn a BG Processes? amaze Linux - General 2 08-12-2003 10:02 PM
Monitor and Restart - Bkg Processes? KendersPlace Linux - General 1 02-15-2003 03:27 AM


All times are GMT -5. The time now is 11:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration