Cron to kill processes on Centos server

Posted on 2008-11-06
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
    LVL 76

    Expert Comment

    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.
    LVL 8

    Expert Comment

    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

    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 ?

    LVL 76

    Expert Comment

    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

    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 76

    Accepted Solution

    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.  
    LVL 5

    Expert Comment

    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
    LVL 8

    Expert Comment

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
    SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now