Results 1 to 5 of 5

Thread: Start and Stop program

  1. #1
    Join Date
    Apr 2007
    Location
    England, United Kingdom
    Beans
    805
    Distro
    Ubuntu 8.04 Hardy Heron

    Start and Stop program

    After looking at compiz-switch/ i decided i wanted to make a simple program that does the following.

    Checks to see if a program is running (i.e apache, amsn)
    If its running stops it if its not running starts it.

    What language would be best to do this and how would i go about doing this. i.e checking if a program is running.

    Saj
    Ubuntu User Since 6.06 - "Here To Help"
    Ubuntu Beta Tester Since 6.10 - "We have the problems so you don't have too."
    --------------------------------------------------
    Linux Registered User: 452642 Ubuntu Registered User: 17365

  2. #2
    Join Date
    Jan 2006
    Location
    Philadelphia
    Beans
    4,076
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: Start and Stop program

    Quote Originally Posted by saj0577 View Post
    After looking at compiz-switch/ i decided i wanted to make a simple program that does the following.

    Checks to see if a program is running (i.e apache, amsn)
    If its running stops it if its not running starts it.

    What language would be best to do this and how would i go about doing this. i.e checking if a program is running.

    Saj
    seems that this is a good job for a shell script. something along the lines of the code below should do the trick:

    Code:
    if $(ps ax |grep 'yourprogram' |grep -v 'grep'; then
        pkill 'yourprogram'
    else
        /path/to/yourprogram &

  3. #3
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Start and Stop program

    Code:
    #!/bin/bash
    if [ ! -z "$(pidof program)" ] ;then  
        pkill program
    else 
        /path/program &
    fi

  4. #4
    Join Date
    May 2005
    Location
    Helsinki, Finland
    Beans
    Hidden!

    Re: Start and Stop program

    Plain killing may work, but the right way to do something like this is to use start_stop_daemon with PIDFILE -- take a look at just about any startup script in /etc/init.d (hal should be a good example).
    GeoClue -- a geographic information service for Linux
    Contact Info

  5. #5
    Join Date
    Apr 2007
    Location
    England, United Kingdom
    Beans
    805
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Start and Stop program

    Cheers.

    Saj
    Ubuntu User Since 6.06 - "Here To Help"
    Ubuntu Beta Tester Since 6.10 - "We have the problems so you don't have too."
    --------------------------------------------------
    Linux Registered User: 452642 Ubuntu Registered User: 17365

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
  •