Shell Stop User Processes


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/bin/sh
  2. # Alexis Megas, 2005.
  3. # Alexis Megas, 02/24/2007. Removed clear call.
  4. # Alexis Megas, 07/04/2007. Direct errors to /dev/null.
  5. # A script that allows the superuser to CONTINUE or STOP another
  6. # user's processes. The script runs in both interactive and
  7. # non-interactive modes.
  8.  
  9. # stop_user_processes.sh [-f] -c -u userid (CONTINUE)
  10. # stop_user_processes.sh [-f] -s -u userid (STOP)
  11.  
  12. force=0
  13. usage="usage: stop_user_processes.sh -c|-s -u USERID [-f(orce)]"
  14.  
  15. while getopts cfsu: options 2> /dev/null
  16. do
  17. case $options in
  18. c) sig="-CONT"
  19. ;;
  20. f) force=1
  21. ;;
  22. s) sig="-STOP"
  23. ;;
  24. u) userid=$OPTARG
  25. ;;
  26. \?) echo "$usage"
  27. exit 1
  28. ;;
  29. esac
  30. done
  31.  
  32. if [ -z "$userid" -o $OPTIND -ge 6 ]
  33. then
  34. echo "$usage"
  35. exit 1
  36. fi
  37.  
  38. answer=""
  39.  
  40. for pid in `ps -U $userid -o pid 2> /dev/null | tail +2 2> /dev/null`
  41. do
  42. name="`ps -U $userid -o pid,comm 2> /dev/null | grep \" $pid \" 2> /dev/null | awk '{print $2}' 2> /dev/null`"
  43.  
  44. if [ -z "$name" ]
  45. then
  46. continue
  47. fi
  48.  
  49. if [ $force -eq 0 -a "$answer" != "a" ]
  50. then
  51. if [ "$sig" = "-STOP" ]
  52. then
  53. echo "Stop $pid ($name)? [a/n/q/y]: \c"
  54. else
  55. echo "Continue $pid ($name)? [a/n/q/y]: \c"
  56. fi
  57.  
  58. read answer
  59.  
  60. while [ "$answer" != "a" -a "$answer" != "n" -a "$answer" != "q" \
  61. -a "$answer" != "y" ]
  62. do
  63. if [ "$sig" = "-STOP" ]
  64. then
  65. echo "Stop $pid ($name)? [a/n/q/y]: \c"
  66. else
  67. echo "Continue $pid ($name)? [a/n/q/y]: \c"
  68. fi
  69.  
  70. read answer
  71. done
  72.  
  73. if [ "$answer" = "n" ]
  74. then
  75. continue
  76. elif [ "$answer" = "q" ]
  77. then
  78. exit 0
  79. fi
  80. fi
  81.  
  82. kill $sig $pid 2> /dev/null
  83.  
  84. if [ $? -eq 0 ]
  85. then
  86. if [ "$sig" = "-STOP" ]
  87. then
  88. echo "$pid ($name) stopped."
  89. else
  90. echo "$pid ($name) continued."
  91. fi
  92. else
  93. if [ "$sig" = "-STOP" ]
  94. then
  95. echo "Error stopping $pid ($name)."
  96. else
  97. echo "Error continuing $pid ($name)."
  98. fi
  99. fi
  100. done
  101.  
  102. exit 0
  103.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.