Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[bash] Frage zu "pidof"
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Sep 14, 2009 6:55 pm    Post subject: [bash] Frage zu "pidof" Reply with quote

Hallo Zusammen,

ich will mir gerade einen "Watchdog" basteln, aber leider stehe ich gerade irgendwie auf dem Schlauch.

Hier mal das Script:

Code:
#!/bin/sh
 
# Watchdog by 3PO
 
 
#check-frequency in seconds

CHECKTIME=60


LOG="/var/log/foo.log" 
DAEMOM=foo.x86

 
while sleep $CHECKTIME
 do
  PID="pidof $DAEMOM"
   if [ "$PID" = "" ] ; then 
    echo "$(date) Fehler, $DAEMOM laeuft nicht" >> $LOG
    echo "$(date) Starte $DAEMOM neu ..." >> $LOG
    /etc/init.d/ $DAEMOM restart
    PID="pidof $DAEMOM" && echo $PID
     if [ "$PID" = "" ] ; then
      echo "$(date) Fehler, $DAEMOM konnte nicht neu gestartet werden" >> $LOG
     else    
      echo "$(date) $DAEMOM wurde neu gestartet" >> $LOG   
     fi
   fi
 done

exit


Das mit $PID, bzw. pidof funktioniert nicht.

Wie kann ich das lösen, bzw anders machen?
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Mon Sep 14, 2009 7:11 pm    Post subject: Reply with quote

Glaub ganz einfach:

falsch
Code:

  PID="pidof $DAEMOM"
  if [ "$PID" = "" ] ; then 


richtig
Code:

  PID=`pidof $DAEMOM`
  if [ -z "$PID" ] ; then
 
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Sep 14, 2009 7:21 pm    Post subject: Reply with quote

Nein das funktioniert leider auch nicht. :(

BTW: Ist denn " und ' nicht das selbe?
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Mon Sep 14, 2009 7:23 pm    Post subject: Reply with quote

` (neben dem Fragezeichen) und nicht ' oder "

Ich habs grad getestet und es sollte das machen was Du willst.
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Sep 14, 2009 7:24 pm    Post subject: Reply with quote

Code:
PID=`pidof $DAEMOM`
  if [ -z "$PID" ] ; then


ich bäuchte des invertiert, aber

Code:
PID=`pidof $DAEMOM`
  if [ ! -z "$PID" ] ; then


gehht leider nicht. :(
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Mon Sep 14, 2009 7:26 pm    Post subject: Reply with quote

Code:

 -n STRING the length of STRING is nonzero


Aber im ersten Fall macht das doch gar keinen Sinn, oder?

Code:

DAEMOM=/usr/sbin/acpid
PID=`pidof $DAEMOM`
       if [ -z "$PID" ] ; then
           echo "$(date) Fehler, $DAEMOM laeuft nicht"
         else
         echo "tut"
   fi


Wenn der ACPID Daemon läuft kommt die Ausgabe tut, falls nicht kommt deine Ausgabe mit laeuft nicht...
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Mon Sep 14, 2009 7:37 pm    Post subject: Reply with quote

Probier es mal mit

Code:

#!/bin/sh
 
# Watchdog by 3PO
 
 
#check-frequency in seconds

CHECKTIME=60


LOG="/var/log/foo.log"
DAEMOM=foo.x86

 
while sleep $CHECKTIME
 do
  PID=$(pidof $DAEMOM)
   if [ -z "$PID" ] ; then
    echo "$(date) Fehler, $DAEMOM laeuft nicht" >> $LOG
    echo "$(date) Starte $DAEMOM neu ..." >> $LOG
    /etc/init.d/$DAEMOM restart
    PID=$(pidof $DAEMOM)
     if [ -z "$PID" ] ; then
      echo "$(date) Fehler, $DAEMOM konnte nicht neu gestartet werden" >> $LOG
     else   
      echo "$(date) $DAEMOM wurde neu gestartet" >> $LOG   
     fi
   fi
 done

exit


Ich lass meine Dienste mittels
Code:

#!/bin/bash

function check_runlevel {
        for SERVICE in $(ls /etc/runlevels/$1); do
                /etc/runlevels/"$1"/"$SERVICE" status || /etc/runlevels/"$1"/"$SERVICE" start
        done
}

check_runlevel boot
check_runlevel default

und eine cronjob Eintrag überprüfen.
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Mon Sep 14, 2009 7:54 pm    Post subject: Reply with quote

Mit PID=`pidof $DAEMOM` gehts.

1000 thx @ Evildad
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Mon Sep 14, 2009 8:02 pm    Post subject: Reply with quote

Kein Problem :D

@esocr: Ich überwache meine wichtigen Dienste mit Nagios.
Back to top
View user's profile Send private message
69719
l33t
l33t


Joined: 20 Sep 2004
Posts: 865

PostPosted: Mon Sep 14, 2009 8:06 pm    Post subject: Reply with quote

Evildad wrote:
Kein Problem :D

@esocr: Ich überwache meine wichtigen Dienste mit Nagios.

Ich auch, aber nur auf Arbeit :), zuhause reicht so nen kleines Script, welches mir die weggeflogenen Dienste neu startet.
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Tue Sep 15, 2009 4:46 pm    Post subject: Reply with quote

Ich hätte da noch eine Frage:

Wie kann ich den prüfen ob ein Script gerade ausgeführt wird?

pidof foo.sh gibt leider keinen Wert zurück, obwohl foo.sh gerade ausgeführt wird.
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Tue Sep 15, 2009 4:48 pm    Post subject: Reply with quote

Code:

FOO=`ps  aux |grep foo |grep  -v grep`

if [ -z "$FOO" ]
   then
   # Just do it
   foo
else   
   # Do nothing
   exit 1
fi



Sowas?

€dit: Wobei glaube ich auch pidof -x funktionieren könnte...
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Tue Sep 15, 2009 5:14 pm    Post subject: Reply with quote

Thx @ Evildad.

Funktioniert beides. :)
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Tue Sep 15, 2009 5:18 pm    Post subject: Reply with quote

3PO wrote:
Thx @ Evildad.

Funktioniert beides. :)


Wunderbar, freut mich :D
Back to top
View user's profile Send private message
3PO
Veteran
Veteran


Joined: 26 Nov 2006
Posts: 1110
Location: Schwabenländle

PostPosted: Tue Sep 15, 2009 5:42 pm    Post subject: Reply with quote

BTW.....

man Pages lesen bildet. :lol:

man pidof
Quote:
.....
-x Scripts too - this causes the program to also return process id's of shells running the named scripts.
.....



Da hätte ich auch selber drauf kommen können. :oops: :roll:
Back to top
View user's profile Send private message
Evildad
Guru
Guru


Joined: 15 Apr 2004
Posts: 475

PostPosted: Tue Sep 15, 2009 6:35 pm    Post subject: Reply with quote

3PO wrote:
BTW.....
man Pages lesen bildet. :lol:


Daraus hab ichs ja :-)
Wollte aber nicht einen auf RTFM machen.
Back to top
View user's profile Send private message
Knieper
l33t
l33t


Joined: 10 Nov 2005
Posts: 846

PostPosted: Wed Sep 16, 2009 3:43 pm    Post subject: Reply with quote

http://www.math.ntnu.no/mirror/www.qmail.org/koobera/www/daemontools/supervise.html
_________________
Je dümmer desto Gnome/KDE.
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3925
Location: Hamburg

PostPosted: Thu Sep 17, 2009 8:30 am    Post subject: Reply with quote

Das leidige back-quote - Mißverständnis (` versus ') läßt sich auf der Shellebene umgehen durch Verwendung von $(...), also z.B.
Code:
tfoerste@n22 ~ $ D=`date`; echo D=$D; U=$(date -u); echo U=$U
D=Thu Sep 17 10:29:58 CEST 2009
U=Thu Sep 17 08:29:58 UTC 2009
Anbei, was heißt eigentlich DAEMOM ? Ich kenne nur DAEMON.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum