Cron to kill processes on Centos server

I want to create a cron to kill porcesses that have been running for a long time on my centos server.
For example If I find a mysqld process running for a long time I manually type killall mysqld and that fixes the high load on my server.
I want a cron that checks that , is that posible ?
What would be the line that I should add on my crontab ?

Who is Participating?
arnoldConnect With a Mentor Commented:
You can use top and awk to extract data to billed the list of PIDS to be killed.

Instead of concentrating on killing processes, you should fine tune the system to avoid this issue.
i.e. if you have a mysql server, limit simultaneous connections to 10 instead of unlimited. Configure the mysql server based on the available resources.
Similarly for the web server and php, limit the resources to lets say 5 or 10 child web servers. Limit the php to 5 or 10 as well.  This way you can have your system running in an optimal shape with the limited resources that are available.
With these adjustments, there should not be a need to 'kill' processes that run too long.  
The line in the crontab would be the bash script that will perform the task.
mysqld is the mysql server daemon.  You migt do better if you adjust the configuration of the applications running on the server to improve performance/resource management.
ok, but in general its a good thing (tm) for processes to be running a long time.  mysqld should be running for the entire time the server is running.

im not sure that what you are asking for will fix your problem.

to specifically answer your question...

create a bash script and then add that to roots cron

as root do a "crontab -e" and your editor will open with your crontab listed

add a line to your crontab with the codes for which month,day,hour etc you want the script to run and follow it with the name of the script you created.

as to the script itself, I assume you know something about scripting, if you dont, google for "bash scripting howto" and start reading.

kill is the command to stop a process, but if its a server process managed by a run script, then use those instead of kill

kill just stops stuff, your run scripts should have an option to restart things.

example script

# script to restart mysqld
/etc/init.d/mysqld restart
....(others as appropriate)

its harder when there isnt a server management script like this.

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

ultra1Author Commented:
And do you know a bash script to kill processes ?
I don´t want to restart it I want to kill it
Cpanel would restart it also mysqld was an example  there are another examples , like php5 process , also note that I would like to kill it only if the process were running for a long time.

can this be done ?

First, you should look into whether these services are needed. if not, do not start them in the first place.
What services does this server provde?  Is it a web server?

There are many processes that run for a long time on a server.
Instead of killing processes simply based on the length of time they have been running. How about killing processes that use too much memory? Disk I/O?

ultra1Author Commented:
arnold thats a grate Idea, killing high mem usage preccess.
But what about the script that can do that ?  Do you know a bash script that can do that ?
I'm agreeing with arnold, ultra1 you should listen to him and reconfigure the server accordingly to his advices... and only then, see if you still want to randomly kill processes.

If you really want to do so, my advice is to get psdoom : :p
I'm under the impression that you have some run-away script processes.  Whiel arnold is right you can, AND SHOULD tune your network daemons to your environment so you dont get overload, I'm understanding you to say that wasnt your problem.

I heard you say that some processes like a perl script or something are hanging and need to be killed manually.

and ideally the answer would be find out which scripts they are, and fix them, because run-away processes really are controllable on unix, and fixable.

supposing however that you are not the author, you are just the server manager, and other people are writing bad scripts and those bad scripts are bringing your server to a crawl, then your original question has merit, how to a check to see what processes are "stuck" and how to I force quit them.

The trick of course is defining stuck, and thats where I'm stuck helping you.

I started a script yesterday, and I think its about half finished, but it needs a way to allow you to specify what "stuck" means so that other processes are not killed, like normally functioning daemons that are supposed to be running should not be arbitrarily killed.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.