aboutsummaryrefslogtreecommitdiff
path: root/xalarm
blob: ca3613c8a2e222563531de2666fcc7d21d0dec2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
#
# Simple alarm applicaton
# See discussion at https://www.reddit.com/r/Gentoo/comments/1rryh1/kalarm_replacement/
#

# Parameters
FULLNAME="$0"
BASENAME="`basename $0`"

# List alarms
function xalarm_list {
  ps -U $USER -o pid,lstart,command | grep xalarm | grep -v grep | grep -v list | grep -v cancel | \
    sed -e "s|$FULLNAME||" -e "s|/bin/bash||" | grep -v -- "sed -e"
}

# Set alarm
function xalarm_set {
  # Delay
  DELAY="${1:-10m}"

  # Message
  shift
  MESSAGE="${*:-Alarm!}"

  # AT(1) implementation
  # Example run: xalarm 1minute mymessage
  #echo "xmessage $MESSAGE" | at now +$DELAY

  # Sleep implementation
  # Example run: xalarm 1m mymessage
  sleep $DELAY
  xmessage $MESSAGE
}

# Cancel alarms
function xalarm_cancel {
  if [ ! -z "$1" ]; then
    if xalarm_list | awk '{ print $1 }' | grep -q "^$1"; then
      kill $1
    fi
  else
    for pid in `xalarm_list | awk '{ print $1 }'`; do
      kill $pid
    done
  fi
}

# Usage
function xalarm_usage {
  echo "usage: $BASENAME [list|cancel|kill|help|usage] [timedef] [message]"
  exit 1
}

# Dispatch
if [ "$1" == "list" ]; then
  xalarm_list
elif [ "$1" == "cancel" ] || [ "$1" == "kill" ]; then
  xalarm_cancel $2
elif [ "$1" == "help" ] || [ "$1" == "usage" ]; then
  xalarm_usage
else
  xalarm_set $*
fi