[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Cron to kill processes on Centos server

Posted on 2008-11-06
Medium Priority
Last Modified: 2013-12-26
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 ?

Question by:ultra1
  • 3
  • 2
  • 2
  • +1
LVL 81

Expert Comment

ID: 22898676
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.

Expert Comment

ID: 22898767
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.


Author Comment

ID: 22898882
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 ?

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

LVL 81

Expert Comment

ID: 22899457
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?


Author Comment

ID: 22899536
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 ?
LVL 81

Accepted Solution

arnold earned 500 total points
ID: 22900037
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.  

Expert Comment

ID: 22903047
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 :
http://psdoom.sourceforge.net/screenshots.html :p

Expert Comment

ID: 22905025
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.


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month19 days, 23 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question