Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, My server pretty often becomes full up php processes running which are not needed. Is there a way to search for and kill any php process that is more ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! saiyen2002's Avatar
    Join Date
    Dec 2006
    Posts
    12

    kill process more than 3 hours old from shell command


    Hi,

    My server pretty often becomes full up php processes running which are not needed.

    Is there a way to search for and kill any php process that is more than 3 hours old?

    as I understand it, i need to use ps piped with awk. awk at the moment seems very complicated to me, do not how to start tackling it.

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,398
    In theory this can be done, but it is a bad solution.
    If a php process (what kind? cron, application, mod_php?) is not needed anymore,
    it should exit by itself.

    Or the other way around: How can you be sure, that all processing has been done after 3h?

    imho, for mod_php and php cronjobs: write an error ticket to the devs and let them figure out proper exit conditions.

    If it is intended, that one process runs long, then at least the forks or threads should exit.
    You must always face the curtain with a bow.

  3. #3
    Just Joined! saiyen2002's Avatar
    Join Date
    Dec 2006
    Posts
    12
    The php processes are to do with voice calls that are being handled by server.

    Sometimes the call terminates but the php script continues to run. Even if the call is really active, i do not want any calls to last more than 3 hours.

  4. #4
    Just Joined!
    Join Date
    Mar 2010
    Location
    Grand Rapids, MI
    Posts
    15
    As Irithori stated. killing these processes is a hack. The best solution is to fix the scripts to timeout properly. If that's not possible, you can write a wrapper script to manage the timeout.

    Unfortunately, I have a similar case where I need to reap processes due to a customers buggy ssh client. Here's an quick cmd line to list / grep / kill all sshd processes containing the string X in the ps output:

    ps -C sshd --format pid,start_time,start,cmd --no-headers | grep X | awk '{print $1}' | xargs -i kill -15 {}


    The awk section only grabs and prints column 1, which is the pid. So if you want to grab php process that started between 02:00:00 and 03:00:00, you could use:

    ps -C php --format pid,start_time,start,cmd --no-headers | grep 02:..:.. | awk '{print $1}' | xargs -i kill -15 {}


    To limit based on time is a bit more difficult to automate, as you'll need something to determine which times occur over 3 hours ago, and I don't know of any simple command line programs which do this.

    Good luck.

  5. #5
    Just Joined!
    Join Date
    Dec 2006
    Posts
    6
    Quote Originally Posted by saiyen2002 View Post
    The php processes are to do with voice calls that are being handled by server.

    Sometimes the call terminates but the php script continues to run. Even if the call is really active, i do not want any calls to last more than 3 hours.
    If you tell me the exact script name that runs for voice call. Then I might write a the kill script for you.

  6. #6
    Just Joined! saiyen2002's Avatar
    Join Date
    Dec 2006
    Posts
    12
    Ramesh, the script name varies. But the general format is like this

    Code:
    root     16001  0.0  0.1 157448  6700 ?        S    Nov15   0:00 /usr/bin/php -q /home/voice/ivr/dial_through/ldial_through.php ddi=12345678912 type
    =ivr name= ccode=code_name acode=mob
    root     16002  0.0  0.1 157448  6696 ?        S    07:07   0:00 /usr/bin/php -q /home/voice/ivr/play_audio.php ddi=98765432178 type
    =ivr name= ccode=code_name acode=mob
    All these processes start with /usr/bin/php -q.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •