Results 1 to 8 of 8

Thread: verhindern das ein Prozess mehr als 1x gestartet wird

  1. #1
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    423

    verhindern das ein Prozess mehr als 1x gestartet wird

    Hi,

    ich habe ein Script.sh was mir ein Programm per Cron jede Minute aufruft.

    Ich möchte das Script erweitern um die Funktion:
    zu prüfen ob das Programm schon läuft und wenn ja das laufende Programminstanz killed und dann das Programm neu startet.

    Wie würdet ihr das machen mit ps | grep programm ?

    Danke.

  2. #2
    Schwups wups:

    PHP Code:
    #!/bin/sh
    # if flag file exists, check also the process via ps
    CHECK_ALSO_PROCESS=1
    # program details
    FETCH_PROG=test2.sh
    FULL_PROG
    =/tmp/disk1/tmp/test2.sh
    FLAG_FILE
    =/tmp/disk1/tmp/digitemp.flg

    # is the program running or not, default = NO == 1 - dont touch this!
    RUN_PROG=1

    # if file exists, check the process
    if [ -f $FLAG_FILE ] ; then
     
    # if file exists, check if we want to look for the process via ps
     
    if [ $CHECK_ALSO_PROCESS -eq 1 ] ; then
      
    catch=`/bin/ps | grep $FETCH_PROG | grep -v grep -c`
      
    # if it is greater than 1, then the program is running - nothing todo
      
    if [ $catch -ge 1 ] ; then
       RUN_PROG
    =0
      
    else
       echo 
    "Flag file ($FLAG_FILE) exists, but program ($FETCH_PROG) is not running! Program will be executed!"
      
    fi
     fi
    fi

    # if program should be executed
    if [ $RUN_PROG -eq 1 ] ; then
     touch $FLAG_FILE
     
    echo "Starting program: $FULL_PROG"
     
    $FULL_PROG

     
    # remove the flag file
     
    rm $FLAG_FILE
     
    # return not 0, because its an error
     
    else
      return 
    2
    fi 
    Last edited by thE_29; 26-06-2008 at 10:34.

  3. #3
    Und hier noch mit den Parameter als Übergabe ans Programm!

    ./runner.sh Programm_Name Voller_Pfad_zum_Programm Flag_file_Name

    PHP Code:
    #!/bin/sh
    if [ -$] || [ -$] || [ -$] ; then
     
    echo -"Wrong Usage!\nUsage: $0 PROG_NAME FULL_PROG_PATH FLAG_FILE"
     
    return 3
    fi

    # if flag file exists, check also the process via ps
    CHECK_ALSO_PROCESS=1
    # program details
    FETCH_PROG=$1
    FULL_PROG
    =$2
    FLAG_FILE
    =$3

    # is the program running or not, default = NO == 1 - dont touch this!
    RUN_PROG=1

    # if file exists, check the process
    if [ -f $FLAG_FILE ] ; then
     
    # if file exists, check if we want to look for the process via ps
     
    if [ $CHECK_ALSO_PROCESS -eq 1 ] ; then
      
    # dont grep the grep cmd and also not the scripts name, because the fetch program name is a parameter
      
    catch=`/bin/ps | grep $FETCH_PROG | grep -v grep | grep -v $0 -c`
      
    # if it is greater than 1, then the program is running - nothing todo
      
    if [ $catch -ge 1 ] ; then
       RUN_PROG
    =0
      
    else
       echo 
    "Flag file ($FLAG_FILE) exists, but program ($FETCH_PROG) is not running! Program will be executed!"
      
    fi
     fi
    fi

    # if program should be executed
    if [ $RUN_PROG -eq 1 ] ; then
     touch $FLAG_FILE
     
    echo "Starting program: $FULL_PROG"
     
    $FULL_PROG

     
    # remove the flag file
     
    rm $FLAG_FILE
     
    # return not 0, because its an error
     
    else
      return 
    2
    fi 

  4. #4
    Join Date
    Feb 2005
    Location
    Germany - Frankfurt
    Posts
    1,548
    Na da kopier ich doch Dein eigenes script aus dem anderen thread und nehme das ! raus:
    Code:
    #!/bin/sh
    #feel free to copy and/or modify 
    
    Processname=busybox_httpd
    if [ -n "`pidof $Processname`" ] ; then  #when Processname is in pid list
        echo -n Killing: $Processname
        kill `pidof $Processname`
        echo -n Starting: $Processname
        exec $Processname
    fi
    das hab ich jetzt trocken gemacht. Müsste prinzipiell funktionieren. getestet ist es, wie gesagt, nicht.

    wengi
    Übersicht aller HowTos --- HowTo Overview (mostly german)
    WL-HDD mit diesem Setup. --- WL-HDD with this setup.
    Kein Support per PM - Frage im jeweiligen Thread! --- No support via pm - ask in the according thread!
    Eine wirklich gute Idee erkennt man daran, dass ihre Verwirklichung von vorne herein ausgeschlossen erscheint. (Albert Einstein)

  5. #5
    Och!

    Wusste gar nicht das pidof den Prozessname als Parameter kann! Wieder was gelernt

  6. #6
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336
    Eigentlich absoluter Quatsch - kein Mensch killt und startet jede Minute den Webserver neu oder auch einen anderen Prozess - weder fuer intern od. extern........
    Die Webseite moechte ich in Funktion sehen.... lol

    Aber wie auch immer, Wengi liegt eben meiner Meinung nach wieder einmal voellig richtig und wie man sieht auch im trockenen = ohne Kastl!!

    Jedenfalls gibts auch fuer WLANfans Trickkiste ein kurzes Beispiel:

    Code:
    #!/bin/sh
    #feel free to copy and/or modify
    # example: ./startprocess httpd -xy -p777 /www/meins /sonstnochirgendwas
    
    echo "Es sind " $# "Argumente mitgegeben worden"
    
    if [ -n "`pidof $1`" ] ; then  #when Processname is in pid list
        echo  Killing: $1 at `date`
        kill `pidof $1`
        echo  Starting: $1 at `date` with parameter: $2 $3 $4 $5 $6
        exec $* &
    fi
    echo $1 $2 $3 $4 $5 $6
    echo ist das Gleiche wie $*  (einfach alle 6 uebergebene Argumente)
    echo Der Name von diesem Script: $0
    Naja wennst jetzt noch die echos wegnimmst, wird das ganze Ding unheimlich kurz....und natuerlich uebersichtlich.
    So, hoffentlich gehts, denn das war auch ohne probieren.....
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

  7. #7
    Jo, warum WLAN-FAN sich soviele sorgen macht das der Prozess abstürtzt verstehe ich zwar auch nicht.. Aber tjo

    Mein Router läuft jetzt seit 122 Tagen und es ist kein einziger Prozess abgestürzt..

  8. #8
    Join Date
    Jul 2007
    Location
    Austria
    Posts
    1,336
    Quote Originally Posted by thE_29 View Post
    Jo, warum WLAN-FAN sich soviele sorgen macht das der Prozess abstürtzt verstehe ich zwar auch nicht.. Aber tjo

    Mein Router läuft jetzt seit 122 Tagen und es ist kein einziger Prozess abgestürzt..
    Jaja das verstehe ich gar nicht mehr, aber wenn man schon in Sorge ist, dann kontrolliere ich hoechstens, ob das Progi laeuft, wieso killen und dann starten, wenn das Progi laeuft iss ja ohnehin gut.....
    Alle HowTo's, all howto's

    RT-N16 1.9.2.7-rtn-r3121, Samba, VSFTP, Lightthpd, PHP, Perl, MySQL, Serendipity, Aria2web, HDD 640GB
    RT-N66U, 16GB MicroSD/ 2 Partitions, 2,5" HDD 1TB, running with Merlin's FW and Entware, 16 Mbit A1,
    Netgear DGND 3700V2, QNAP TS119PII 4 TB, QNAP TS209 2 TB Raid1, Backup Synology DS107+ 1 TB, HP CP1515n

Similar Threads

  1. ASUS-Router als USB-Drucker / Scanner / Fax Server geht das alles?
    By WLAN-Fan in forum German Discussion - Deutsch (DE)
    Replies: 12
    Last Post: 19-03-2008, 13:52
  2. Wie benutze ich den WL-500gP als Repeater?
    By edelknecht in forum German Discussion - Deutsch (DE)
    Replies: 1
    Last Post: 05-02-2008, 12:12
  3. Wird ein Modem benötigt?
    By Allo in forum German Discussion - Deutsch (DE)
    Replies: 3
    Last Post: 19-06-2007, 06:14
  4. Kein Zugriff mehr auf das Web-Interface (WL500g)
    By el_diablo in forum German Discussion - Deutsch (DE)
    Replies: 1
    Last Post: 10-04-2006, 11:10

Posting Permissions

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