pid 864 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/bin/sh
  2. # Uncomment the following line to get debug info
  3. #set -x
  4. SELF="$(basename ${0})"
  5. PID_FILE="/var/run/funkey.pid"
  6. PID_PATH="/var/run/pid_path"
  7. usage() {
  8. >&2 echo "Usage: ${SELF} record pid"
  9. >&2 echo " ${SELF} erase"
  10. >&2 echo " ${SELF} print"
  11. exit 1
  12. }
  13. record_pid() {
  14. local pid="${1}"
  15. if ! [ ! "${pid}" -ne "${pid}" ]; then
  16. >&2 echo "error: ${pid} is not a number"
  17. exit 2
  18. fi
  19. # Save PID
  20. echo "${1}" > "${PID_FILE}"
  21. # Save current pid path
  22. pid_path=$(dirname $(readlink /proc/${pid}/exe))
  23. echo -n "$pid_path" > "$PID_PATH"
  24. }
  25. erase_pid() {
  26. rm -f "${PID_FILE}"
  27. }
  28. case "${1}" in
  29. record)
  30. if [ ${#} -ne 2 ]; then
  31. usage
  32. fi
  33. record_pid "${2}"
  34. ;;
  35. erase)
  36. if [ ${#} -ne 1 ]; then
  37. usage
  38. fi
  39. erase_pid
  40. ;;
  41. print)
  42. cat "${PID_FILE}"
  43. ;;
  44. *)
  45. usage
  46. ;;
  47. esac
  48. exit 0