Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running a web server on an AWS EC2 micro instance. The instance has ~630MB of RAM. With time, I have several httpd processes and very little free RAM. When I restart the httpd service, I end up freeing about 350MB of RAM.

I thought of having this automated every 12 hours using a cron job under root. My script includes code as

service httpd restart
service mysqld restart
ps aux
free -m

This is the first time I am attempting cron scripts.

I receive an email with the expected output for ps aux and free -m, but

./scriptName.sh: line 1: service: command not found
./scriptName.sh: line 2: service: command not found

for the restarts commands.

The script did run as root. I am afraid that using sudo may cause the script to hang. The relevant lines from the output of ps -

root     14664  0.0  0.2 142200  1720 ?        S    22:41   0:00 CROND
root     14665  0.0  0.2   9296  1236 ?        Ss   22:41   0:00 /bin/sh -c ./scriptName.sh
smmsp    14667  0.0  0.6  76020  4244 ?        R    22:41   0:00 /usr/sbin/sendmail -FCronDaemon -i -odi -oem -oi -t -f root
root     14669  0.0  0.1  11244  1008 ?        R    22:41   0:00 ps aux

What is the right thing to do to have a successful restart of services?

Is it even advisable to do something like this?

Output of free -m

             total       used       free     shared    buffers     cached
Mem:           596        573         23          0          8         71
-/+ buffers/cache:        493        103
Swap:            0          0          0
share|improve this question
5  
Since your using apache, and probably using mpm_prefork, then why not set MaxRequestsPerChild, so child processes are terminated after handling a certain number of requests? Next, is re-starting really the right answer? Restarting your services like mysql because it uses memory is a bad idea. The whole point of it using memory, is so that it can give you answers faster. If it is using too much, then adjust the settings. –  Zoredache Feb 17 '12 at 0:34
add comment

4 Answers

up vote 8 down vote accepted

The primary problem is that there is no proper $PATH defined in the run environment of cron, so you need to use the full path to service for this to work.

You can find out this path with the command which service, which should print something like /usr/sbin/service.

The secondary problem: I wouldn't do that, just blindly restarting services on a production system is never a good idea. Do you have an actual memory/performance problem or might it be that your RAM is just used up by buffers and the like (see http://www.linuxatemyram.com/)?

Please add the output of free -m after a few hours to your question.

share|improve this answer
    
Added the o/p of free -m. –  Lord Loh. Feb 17 '12 at 23:22
add comment

It might be better to look at something like Monit instead of trying to do a homebrew cron script.

Here's the example config for Apache:

http://mmonit.com/wiki/Monit/ConfigurationExamples#apache

You should be able to do something like:

if totalmem > 300 MB for 2 cycles then alert

in the Apache block.

Monit should be in one of the RPM repositories.

share|improve this answer
add comment

I think you're going about this the wrong way. Look into tuning Apache first of all. Then, research Linux memory management. You WANT your server using the ram, otherwise why do you have it?

share|improve this answer
    
I think I'll try to tune apache. My RAM is highly limited and would like to have some free RAM for some process popping up. –  Lord Loh. Feb 17 '12 at 1:45
1  
I run several sites on a tiny instance that are receiving a LOT of hits. Read this: linuxatemyram.com your ram will be there for processes unless your sites are being hammered. NINJA EDIT: You WANT Apache using that ram, otherwise your sites will load slowly. If something needs the ram and you're not getting a bunch of traffic, the system will 'release' it for that process to use. –  Publiccert Feb 17 '12 at 1:50
add comment

Why do you want free RAM? free ram is wasted RAM. Either you are miscalculating the amount of ram free by not counting cache memory as free ram (see linuxatemyram.com) or you should be tuning your apache for fewer MaxClients so that apache will us less memory.

share|improve this answer
    
I have just 630 MB RAM. I would like to have some free for some eventuality and not run out of it 100%. –  Lord Loh. Feb 17 '12 at 1:44
1  
@LordLoh. - you need to learn about how linux uses RAM. Stew is right, you don't really want any free ram. Don't try and out-smart the linux kernel when it comes to memory management. Just let the system run and do its thing. –  EEAA Feb 17 '12 at 1:49
    
@LordLoh.how are you determining the amount of free RAM? –  stew Feb 17 '12 at 2:16
    
@stew - by running free -m. –  Lord Loh. Feb 17 '12 at 23:23
    
and are you properly discounting the RAM being used as cache? Your output of free -m actually shows that you actually have 15% or so of you RAM free? –  stew Feb 18 '12 at 2:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.