diff options
-rwxr-xr-x | xalarm | 49 |
1 files changed, 38 insertions, 11 deletions
@@ -8,6 +8,8 @@ # Parameters FULLNAME="$0" BASENAME="`basename $0`" +XALARMPID="$BASHPID" +XALARMPPID="$PPID" # Set alarm function xalarm_set { @@ -46,8 +48,15 @@ function xalarm_set { # List alarms function xalarm_list { - ps -U $USER -o pid,state,lstart,command | grep xalarm | grep -v grep | grep -v list | grep -v cancel | grep -v pause | grep -v resume | grep -v reset | grep -v reset-loop | \ - sed -e "s|$FULLNAME||" -e "s|/bin/bash||" | grep -v -- "sed -e" + # Simpler process filtering, but might collide with alarm messages + #ps -U $USER -o pid,state,lstart,command | grep xalarm | grep -v grep | grep -v list | grep -v cancel | grep -v pause | grep -v resume | grep -v reset | grep -v reset-loop | \ + # sed -e "s|$FULLNAME||" -e "s|/bin/bash||" | grep -v -- "sed -e" + + # Better, butt more difficult way to filter processes + THISPID="$BASHPID" + PARENTPID="$PPID" + ps -U $USER -o pid,state,lstart,command | grep xalarm | grep -v grep | \ + sed -e "s|$FULLNAME||" -e "s|/bin/bash||" | grep -v -- "sed -e" | grep -v "^ *$THISPID" | grep -v "^ *$PARENTPID" | grep -v "^ *$XALARMPID" | grep -v "^ *$XALARMPPID" } # Return xalarm PIDs @@ -61,56 +70,74 @@ function xalarm_pids { fi } +# Send a signal to a process +function xalarm_signal { + local pid="$1" + local signal="$2" + + if [ -z "$signal" ]; then + signal="-TERM" + fi + + if ps $1 &> /dev/null; then + kill $signal $pid + fi +} + # Cancel alarms function xalarm_cancel { for pid in `xalarm_pids $1`; do - kill $pid + xalarm_signal $pid done } # Pause alarms function xalarm_pause { for pid in `xalarm_pids $1`; do - kill -STOP $pid + xalarm_signal -STOP $pid done } # Resume alarms function xalarm_resume { for pid in `xalarm_pids $1`; do - kill -CONT $pid + xalarm_signal -CONT $pid done } # Reset alarms function xalarm_reset { for pid in `xalarm_pids $1`; do - kill -USR1 $pid + xalarm_signal -USR1 $pid done } # Reset alarm loop counter function xalarm_reset_loop { for pid in `xalarm_pids $1`; do - kill -USR2 $pid + xalarm_signal -USR2 $pid done } # Usage function xalarm_usage { - echo "usage: $BASENAME [list|cancel|kill|help|usage|loop] [timedef] [message]" + echo "usage: $BASENAME [list|cancel|kill|help|usage|loop|reset|reset-loop] [timedef] [message]" exit 1 } # Respond to SIGUSR1 function xalarm_sigusr1 { - ALARM_RESET="1" - kill $SLEEP_PID 2> /dev/null + if [ ! -z "$SLEEP_PID" ]; then + ALARM_RESET="1" + kill $SLEEP_PID 2> /dev/null + fi } # Respond to SIGUSR2 function xalarm_sigusr2 { - COUNT=0 + if [ ! -z "$COUNT" ]; then + COUNT=0 + fi } # Register traps |