[1120] | 1 | #! /bin/bash |
---|
| 2 | # |
---|
| 3 | # functions: This file contains functions to be used for autostart.sh |
---|
| 4 | # in WindowMaker |
---|
| 5 | # ported from /etc/init.d/functions 23-Dec-1998 |
---|
| 6 | # added checkStart 24-Dec-1998 |
---|
| 7 | # added path manipulation 11-Jul-2000 |
---|
| 8 | # by Jun Nishii <jun@vinelinux.org> |
---|
| 9 | # Time-stamp: <2000-07-11 12:15:23 vine> |
---|
| 10 | # |
---|
| 11 | # (original source, /etc/init.d/functions, is written by |
---|
| 12 | # Miquel van Smoorenburg, Greg Galloway and Marc Ewing. |
---|
| 13 | |
---|
| 14 | # A function to stop a program. |
---|
| 15 | killproc() { |
---|
| 16 | # Test syntax. |
---|
| 17 | if [ $# = 0 ]; then |
---|
| 18 | echo "Usage: killproc {program} [signal]" |
---|
| 19 | return 1 |
---|
| 20 | fi |
---|
| 21 | |
---|
| 22 | notset=0 |
---|
| 23 | # check for second arg to be kill level |
---|
| 24 | if [ "$2" != "" ] ; then |
---|
| 25 | killlevel=$2 |
---|
| 26 | else |
---|
| 27 | notset=1 |
---|
| 28 | killlevel="-9" |
---|
| 29 | fi |
---|
| 30 | |
---|
| 31 | # Save basename. |
---|
| 32 | base=`basename $1` |
---|
| 33 | |
---|
| 34 | # Find pid. |
---|
| 35 | pid=`pidofproc $base` |
---|
| 36 | |
---|
| 37 | # Kill it. |
---|
| 38 | if [ "$pid" != "" ] ; then |
---|
| 39 | echo -n "$base " |
---|
| 40 | if [ "$notset" = 1 ] ; then |
---|
| 41 | # TERM first, then KILL if not dead |
---|
| 42 | kill -TERM $pid |
---|
| 43 | usleep 100000 |
---|
| 44 | if ps h $pid >/dev/null 2>&1 ; then |
---|
| 45 | sleep 3 |
---|
| 46 | kill -KILL $pid |
---|
| 47 | fi |
---|
| 48 | # use specified level only |
---|
| 49 | else |
---|
| 50 | kill $killlevel $pid |
---|
| 51 | fi |
---|
| 52 | fi |
---|
| 53 | |
---|
| 54 | # Remove pid file if any. |
---|
| 55 | rm -f /var/run/$base.pid |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | # A function to stop a user's program. |
---|
| 59 | killmyproc() { |
---|
| 60 | # Test syntax. |
---|
| 61 | if [ $# = 0 ]; then |
---|
| 62 | echo "Usage: killmyproc {program} [signal]" |
---|
| 63 | return 1 |
---|
| 64 | fi |
---|
| 65 | |
---|
| 66 | notset=0 |
---|
| 67 | # check for second arg to be kill level |
---|
| 68 | if [ "$2" != "" ] ; then |
---|
| 69 | killlevel=$2 |
---|
| 70 | else |
---|
| 71 | notset=1 |
---|
| 72 | killlevel="-9" |
---|
| 73 | fi |
---|
| 74 | |
---|
| 75 | # Save basename. |
---|
| 76 | base=`basename $1` |
---|
| 77 | |
---|
| 78 | # Find pid. |
---|
| 79 | pid=`pidofmyproc $base` |
---|
| 80 | |
---|
| 81 | # Kill it. |
---|
| 82 | if [ "$pid" != "" ] ; then |
---|
| 83 | echo "killing $base ..." |
---|
| 84 | if [ "$notset" = 1 ] ; then |
---|
| 85 | # TERM first, then KILL if not dead |
---|
| 86 | kill -TERM $pid |
---|
| 87 | usleep 100000 |
---|
| 88 | if ps h $pid >/dev/null 2>&1 ; then |
---|
| 89 | sleep 3 |
---|
| 90 | kill -KILL $pid |
---|
| 91 | fi |
---|
| 92 | # use specified level only |
---|
| 93 | else |
---|
| 94 | kill $killlevel $pid |
---|
| 95 | fi |
---|
| 96 | fi |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | # A function to find the pid of a program. |
---|
| 100 | pidofproc() { |
---|
| 101 | # Test syntax. |
---|
| 102 | if [ $# = 0 ] ; then |
---|
| 103 | echo "Usage: pidofproc {program}" |
---|
| 104 | return 1 |
---|
| 105 | fi |
---|
| 106 | |
---|
| 107 | # First try "/var/run/*.pid" files |
---|
| 108 | if [ -f /var/run/$1.pid ] ; then |
---|
| 109 | pid=`head -1 /var/run/$1.pid` |
---|
| 110 | if [ "$pid" != "" ] ; then |
---|
| 111 | echo $pid |
---|
| 112 | return 0 |
---|
| 113 | fi |
---|
| 114 | fi |
---|
| 115 | |
---|
| 116 | # Next try "pidof" |
---|
| 117 | pid=`/sbin/pidof $1` |
---|
| 118 | if [ "$pid" != "" ] ; then |
---|
| 119 | echo $pid |
---|
| 120 | return 0 |
---|
| 121 | fi |
---|
| 122 | |
---|
| 123 | # Finally try to extract it from ps |
---|
| 124 | ps auxw | awk 'BEGIN { prog=ARGV[1]; ARGC=1 } |
---|
| 125 | { if ((prog == $11) || (("(" prog ")") == $11) || |
---|
| 126 | ((prog ":") == $11)) { print $2 ; exit 0 } }' $1 |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | pidofmyproc() { |
---|
| 131 | # Test syntax. |
---|
| 132 | if [ $# = 0 ] ; then |
---|
| 133 | echo "Usage: ownofproc {program}" |
---|
| 134 | return 1 |
---|
| 135 | fi |
---|
| 136 | |
---|
| 137 | # Next try "pidof" |
---|
| 138 | pid=`pidofproc $1` |
---|
| 139 | if [ "$pid" != "" ] ; then |
---|
| 140 | for i in $pid; do |
---|
| 141 | user=`ps h -o user --pid $i` |
---|
| 142 | if [ "${user}" = "${USER}" ]; then |
---|
| 143 | echo -n $i" " |
---|
| 144 | fi |
---|
| 145 | done |
---|
| 146 | echo |
---|
| 147 | return 0 |
---|
| 148 | fi |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | checkStart(){ |
---|
| 153 | pid=`pidofproc $1` |
---|
| 154 | if [ -z "$pid" ] ; then |
---|
| 155 | $@ >& /dev/null & |
---|
| 156 | fi |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | isXRunning(){ |
---|
| 160 | pid=`pidofproc X` |
---|
| 161 | |
---|
| 162 | if [ -z "$pid" ] ; then |
---|
| 163 | # for Xpmac server |
---|
| 164 | pid=`pidofproc Xpmac.bin` |
---|
| 165 | |
---|
| 166 | # for sparc |
---|
| 167 | if [ -z "$pid" ] ; then |
---|
| 168 | pid=`pidofproc Xsun` |
---|
| 169 | fi |
---|
| 170 | if [ -z "$pid" ] ; then |
---|
| 171 | pid=`pidofproc Xsun24` |
---|
| 172 | fi |
---|
| 173 | |
---|
| 174 | if [ -z "$pid" ] ; then |
---|
| 175 | return -1 |
---|
| 176 | else |
---|
| 177 | return 0 |
---|
| 178 | fi |
---|
| 179 | fi |
---|
| 180 | return 0 |
---|
| 181 | } |
---|
| 182 | |
---|