Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
does anyone know how to make a script that will check service/s status whether running or stop and also will run stop services? I'm a newbie in scripting on linux ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    23

    Service Script


    does anyone know how to make a script that will check service/s status whether running or stop and also will run stop services?

    I'm a newbie in scripting on linux platform.

  2. #2
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    You can look at the scripts in /etc/rc.d/init.d or /etc/init.d and take one of them as a template. (redhat/fedora/mandriva/centos/... Not sure if it's the same in Debian/ubuntu/...

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    23
    Thanks for the response RDU,

    I have searched this script that checks services and it works.

    #!/bin/sh
    SERVICE=service;

    if ps ax | grep -v grep | grep $SERVICE > /dev/null
    then
    echo "$SERVICE service running, everything is fine"
    else
    echo "$SERVICE is not running"
    fi

    Now I'm trying to figure out how to make this script check for a distinct linux os, like I'm putting an ip address where the check will be done.

  4. #4
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    The easier is to have that script installed localy with the service (it's part of the service). E.g. if your service run on several machine, have the script on each of them.
    Now, just use ssh to launch the scripts and get the result :
    ssh root@remotecomputer /usr/local/bin/checkservice.sh
    You can skip the password dialog using ssh keys

  5. #5
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    For services that are started with init.d this should do the job better than just a plain ps:

    Code:
    /etc/init.d/service status
    Anyway, what does a service that runs tell you? It runs! You don't know if it works properly cause this would require further checks.

    If you're interested you can join my recently created project at google code:
    jasmo - Project Hosting on Google Code

    It basically implements a API to create automated checks and triggered actions. It's written in Java and thus can be run on every platform that has Java support.

    Feel free to contact me through the project mailing list.

  6. #6
    Just Joined!
    Join Date
    Sep 2010
    Posts
    23
    Thanks for the response RDU and Klos,

    RDU - is it really easier for me to install the script to every machine, isn't kinda hassle? and also regarding with ssh, how do ssh keys works?

    Klos - thanks for the code, i only need to do a loop so that i can check every service. most probably i only need to know if the service is running or stopped. do you have any idea on how can i check services not only locally?

    regarding with your project, it's pretty interesting but first i need to study more about linux and system administration.

  7. #7
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    RDU - is it really easier for me to install
    the script to every machine, isn't kinda hassle? and also regarding with ssh, how do ssh keys works?
    ssh keys are kewl! Basically it is a private/public keypair where you keep the private key and store the public key on the server. When you log in ssh automatically sends some data about you encrypted with your private key to the server, which then decrypts it with your public key. If your identify is authenticated, you are being automatically logged in without the need of entering a password. To set up you just need to do on the client:

    Code:
    # ssh-keygen
    # ssh-copy-id user@server
    see man ssh-keygen for all available encryption methods. If you use it to access a server with root user, you may want to use DSA encryption with a pretty long key.

    Klos - thanks for the code, i only need to do a loop so that i can check every service. most probably i only need to know if the service is running or stopped. do you have any idea on how can i check services not only locally?
    It would depend on the service. You could for example test server reachability with a simple ping. To test further if apache is running you could have a status.php script returning information if all modules are loaded and working properly. And finally to test if a database backend is there, you could improve the status.php to include also that information or just address the database directly. But to get to the nuts: all these tests would make no sense if for example your client internet connection is down. You can't say "remote server down" and start an alarm that wakes you up at 5AM if your ISP decided to turn off your internet connection for 5 minutes. And you can't for example run certain tests on the server, as you will never know if a remote client will see the same things as you see locally (i.e. server internet connection down) and you won't even get alarms if you mail them over the internet.

    Needless to say: It really depends on the kind of service and how accurate the "is working" should be. You can't tell if a server really is working just by a ping response. So you can't tell if a website is working by just asking "is php being interpreted and do i get output?".

    This is where my project starts off. I got busted that I was unable to find a framework that implements a common API where you could plug in these tests and run automatically actions when a certain event occurs. Anyway, you wouldn't need much linux and system administration knowledge to contribute to my project as it is nearly platform independend (java) but once you have time to get involved, let me know.

  8. #8
    Just Joined!
    Join Date
    Sep 2010
    Posts
    23
    Thanks for the response Klos,

    I'm quite confused with all the things that you said, I'm really a newbie in linux and some of what you're saying are very new to me. here

    Here is the script i made:

    !/bin/bash

    cd /etc/init.d/
    dirLength=$(ls | wc -l)
    arrayLength=$(($dirLength - 1))
    SERVICE=($(ls))
    cd ~

    echo "----------------SERVICES-----------------"

    for ((i = 0; i <= $arrayLength; i++))
    do
    /etc/init.d/${SERVICE[$i]} status
    done

    echo "-----------------------------------------"
    echo " "

    read -p "Do you want to start the stopped services? (yes/no) " choice

    echo " "

    serviceRun ()

    {
    for ((i = 0; i <= $arrayLength; i++))
    do
    status=$(/etc/init.d/${SERVICE[$i]} status)
    serviceStatus="${SERVICE[$i]} is stopped"
    if test "$status" = "$serviceStatus"
    then
    service ${SERVICE[$i]} start
    fi
    done
    }

    if test "$choice" = "yes"
    then
    serviceRun
    fi

    this script checks all the services in the /etc/init.d directory, as a workaround, i made another script that transfer this script to the machine/s that i want to check the service/s status by doing ssh and scp. Is there another way to make it easier?

  9. #9
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    read -p "Do you want to start the stopped services? (yes/no) " choice
    *uoh* For gods sake dont run this on a productive server machine!

    Take a look at the /etc/rc.d/ directories, the runlevel command and the update-init.rc command to get a feeling how the stuff is being put together.

    In the end you should only observe/start/stop/restart the services you really need. So, define them like this:

    Code:
    services[]="apache2"
    services[]="papanoah"
    
    for service in $services; do
       # check
    done
    Last edited by Kloschüssel; 09-07-2010 at 01:12 PM.

  10. #10
    Just Joined!
    Join Date
    Sep 2010
    Posts
    23
    Thanks for the response Klos,

    the choice is for testing only, i'm still on the process of experimenting. i might revise it to do specific choice of what service you want to start.

    regarding with your code, i made a snippet that will transfer all the services in the init.d to an array which is located in the first part of my script. so that i don't need to type all the service names. (for testing purpose only)

    i'll take in to consider your opinion with the other details of a service can do like stop, restart and etc.

Page 1 of 2 1 2 LastLast

Posting Permissions

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