Prozess mit PID bestimmter starten

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Bei Netzwerkserver sehe ich kein Problem - die werden doch sowieso meist nur über ihre Ports angesprochen.

    Ansonsten braucht man die PIDs auch nicht zwischenspeichern. Man kann sie einfach mit folgendem Befehl auslesen

    Quellcode

    1. pidof apache2


    Ansonsten schau dir irgendwelche init Scripts an
    PIDs mal am Beispiel von Apache:

    Quellcode

    1. #!/bin/bash -e
    2. APACHE2CTL="$ENV /usr/sbin/apache2ctl"
    3. apache_stop() {
    4. PID=""
    5. PIDFILE=""
    6. AP_CONF=/etc/apache2/apache2.conf
    7. # apache2 allows more than PidFile entry in the config but only the
    8. # last found in the config is used; we attempt to follow includes
    9. # here, but only first-level includes are supported, not nested ones
    10. for i in $AP_CONF `awk '$1 ~ /^\s*[Ii]nclude$/ && $2 ~ /^\// {print $2}' $AP_CONF`; do
    11. PIDFILE=`grep -i ^PidFile $i | tail -n 1 | awk '{print $2}'`
    12. if [ -e "$PIDFILE" ]; then
    13. PID=`cat $PIDFILE`
    14. fi
    15. done
    16. if [ $? = 0 ]; then
    17. # if the config is ok than we just stop normaly
    18. if [ -n "$PID" ]
    19. then
    20. $APACHE2CTL stop
    21. CNT=0
    22. while [ 1 ]
    23. do
    24. CNT=$(expr $CNT + 1)
    25. [ ! -d /proc/$PID ] && break
    26. if [ $CNT -gt 60 ]
    27. then
    28. if [ "$VERBOSE" != "no" ]; then
    29. echo " ... failed!"
    30. echo "Apache2 failed to honor the stop command, please investigate the situation by hand."
    31. fi
    32. return 1
    33. fi
    34. sleep 1
    35. done
    36. else
    37. if [ "$VERBOSE" != "no" ]; then
    38. echo -n " ... no pidfile found! not running?"
    39. fi
    40. fi
    41. else
    42. [ "$VERBOSE" != "no" ] && echo "$errors"
    43. # if we are here something is broken and we need to try
    44. # to exit as nice and clean as possible
    45. # if pidof is null for some reasons the script exits automagically
    46. # classified as good/unknown feature
    47. PIDS=`pidof apache2` || true
    48. REALPID=0
    49. # if there is a pid we need to verify that belongs to apache2
    50. # for real
    51. for i in $PIDS; do
    52. if [ "$i" = "$PID" ]; then
    53. # in this case the pid stored in the
    54. # pidfile matches one of the pidof apache
    55. # so a simple kill will make it
    56. REALPID=1
    57. fi
    58. done
    59. if [ $REALPID = 1 ]; then
    60. # in this case everything is nice and dandy
    61. # and we kill apache2
    62. kill $PID
    63. else
    64. # this is the worst situation... just kill all of them
    65. #for i in $PIDS; do
    66. # kill $i
    67. #done
    68. # Except, we can't do that, because it's very, very bad
    69. if [ "$PIDS" ] && [ "$VERBOSE" != "no" ]; then
    70. echo " ... failed!"
    71. echo "You may still have some apache2 processes running. There are"
    72. echo "processes named 'apache2' which do not match your pid file,"
    73. echo "and in the name of safety, we've left them alone. Please review"
    74. echo "the situation by hand."
    75. fi
    76. return 1
    77. fi
    78. fi
    79. }
    80. # Stupid hack to keep lintian happy. (Warrk! Stupidhack!).
    81. case $1 in
    82. start)
    83. log_begin_msg "Starting apache 2.0 web server..."
    84. $APACHE2CTL startssl
    85. ;;
    86. stop)
    87. log_begin_msg "Stopping apache 2.0 web server..."
    88. apache_stop
    89. ;;
    90. esac
    Alles anzeigen


    (reduziert - original unter /etc/init.d/apache2)