Results 1 to 3 of 3

Thread: How to check if service is UP via PHP?

  1. #1
    Join Date
    Oct 2006
    Location
    Argentina
    Beans
    251
    Distro
    Ubuntu 13.10 Saucy Salamander

    Question How to check if service is UP via PHP?

    I am trying to make a status web site, simple, and wanted to check the status of certain services, like if vsftpd, avahi, cups are running.

    Any ideas?

  2. #2
    Join Date
    Feb 2007
    Location
    West Hills CA
    Beans
    10,044
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to check if service is UP via PHP?

    -------------------------------------
    Oooh Shiny: PopularPages

    Unumquodque potest reparantur. Patientia sit virtus.

  3. #3
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: How to check if service is UP via PHP?

    I run this script on my servers every fifteen minutes from cron:

    Code:
    #!/bin/sh
    
    LOG=/var/log/daemon-check.log
    
    PROGS="sshd crond rsyslog httpd named ntpd [more daemons if appropriate]"
    
    for prog in $PROGS
    do
       progtest=$(ps aux --width=256 | grep $prog | grep -v grep)
       if [ "$progtest" != "" ]
       then
          echo -n `date` >> $LOG
          echo " $prog running" >> $LOG
       else
          /sbin/service $prog restart  >> $LOG 2>&1
          echo -n `date` >> $LOG
          echo " $prog *** RESTARTED ***" >> $LOG
       fi
    done
    The key line is the one that creates "progtest" by using "ps aux".

    This script is designed to restart daemons that have stopped for some reason. You might also want to receive email notifications if a daemon is restarted. If the machine has an SMTP listener running like Postfix or sendmail, add the line

    Code:
    echo "Restarted $prog at $(date)" | mail -s "$prog RESTARTED!" you@example.com
    just after the line that logs the restart, replacing "you@example.com" with your correct email address.

    In PHP, you can run the "ps aux" command using the shell_exec() function.
    Last edited by SeijiSensei; November 17th, 2013 at 04:03 PM.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

Tags for this Thread

Bookmarks

Posting Permissions

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