vmtools.rc 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. NAME=vmtools
  3. USER=root
  4. CONFIG="/etc/vmware-tools/tools.conf"
  5. RUNDIR="/var/run"
  6. PIDFILE="$RUNDIR/vmtoolsd.pid"
  7. STARTCMD="/usr/bin/vmtoolsd --config=$CONFIG --background=$PIDFILE"
  8. STOPCMD=""
  9. STOPTIMEOUT=300
  10. function getpid() {
  11. if [ -z "$PIDFILE" ]; then
  12. pid="$(pgrep -xfn "$STARTCMD")"
  13. else
  14. if [ -f "$PIDFILE" ]; then
  15. pid=$(< $PIDFILE)
  16. if [ ! -d /proc/"$pid" ]; then
  17. echo "$NAME: removing stale pidfile $PIDFILE" >&2
  18. rm -f "$PIDFILE"
  19. unset pid
  20. fi
  21. fi
  22. fi
  23. echo "$pid"
  24. }
  25. case $1 in
  26. start)
  27. pid=$(getpid)
  28. install -d -m 755 -o $USER $RUNDIR || exit 1
  29. if [ -n "$pid" ]; then
  30. echo "$NAME already running with pid $pid" >&2
  31. exit 1
  32. fi
  33. eval "$STARTCMD"
  34. ;;
  35. stop)
  36. pid=$(getpid)
  37. if [ -n "$pid" ]; then
  38. if [ -n "$STOPCMD" ]; then
  39. eval "$STOPCMD"
  40. else
  41. kill "$pid"
  42. fi
  43. t=$(printf '%(%s)T' -1)
  44. tend=$((t+STOPTIMEOUT))
  45. while [ -d /proc/$pid -a $t -lt $tend ]; do
  46. sleep 0.5
  47. t=$(printf '%(%s)T' -1)
  48. done
  49. if [ -d /proc/"$pid" ]; then
  50. echo "$NAME still running with pid $pid" >&2
  51. else
  52. [ -n "$PIDFILE" ] && rm -f "$PIDFILE"
  53. fi
  54. else
  55. echo "$NAME is not running" >&2
  56. fi
  57. ;;
  58. restart)
  59. $0 stop
  60. $0 start
  61. ;;
  62. status)
  63. pid=$(getpid)
  64. if [ -n "$pid" ]; then
  65. echo "$NAME is running with pid $pid"
  66. else
  67. echo "$NAME is not running"
  68. fi
  69. ;;
  70. *)
  71. echo "usage: $0 [start|stop|restart|status]"
  72. ;;
  73. esac