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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

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 ?

Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

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 ?
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.  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.