This repository was archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqueue
executable file
·96 lines (84 loc) · 1.98 KB
/
squeue
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/sh
#
# Bourne Shell implementation of a simple queue command.
# This script comes in handy for hot-keys and the like.
#
# It does not support a timer or other means of job triggers
# (e.g. system load).
#
# License: MIT
# Copyright: Tobias Farrenkopf [email protected]
set -o nounset
NQUEUE="/tmp/${USER}_simple_queue"
usage() {
echo "Usage: $(basename "$0") [COMMAND]"
echo 'Appends every COMMAND to a queue and executes them one by one.'
echo 'It reads COMMAND from standard input or from ARGS.'
echo
echo '-h, --help display this help and exit'
echo '-q, --quit terminate the queue process'
echo '-d allow duplicate COMMAND in the queue'
}
echoerr() { echo "$@" 1>&2; }
check_running() {
if [ -p "$NQUEUE" ] && $(ps x | grep -q "$0"); then
echo 1
else
echo 0
fi
}
run_loop() {
last_job=
# read from the named pipe. The first char (duplicate flag)
# is removed from the job and evaluated.
while read job <"$NQUEUE"
do
skip_duplicate=${job%${job#?}}
job=${job#?}
if [ "$skip_duplicate" -eq 1 ] && [ "$job" = "$last_job" ]; then
echoerr "WARN: skipping duplicate job"
elif [ -n "$job" ]; then
last_job="$job"
eval "$job"
fi
sleep .2
done
}
# Main
#
skip_duplicate=1
# fork the run_loop if not running
if [ $(check_running) -eq 0 ]; then
rm -f "$NQUEUE"
mkfifo "$NQUEUE" || exit 1
run_loop &
fi
if [ ! -p "$NQUEUE" ]; then
echoerr "ERROR: ${NQUEUE} is not a named pipe. Abort!"
exit 1
fi
# check for options
if [ $# -ne 0 ]; then
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
usage
exit 0
elif [ "$1" = '-q' ] || [ "$1" = "--quit" ]; then
echo 'Done processing jobs'
rm -f "$NQUEUE"
exit 0
elif [ "$1" = "-d" ]; then
skip_duplicate=0
shift
elif [ ${1%${1#?}} = "-" ]; then
echoerr "Invalid option ${1}"
echoerr "Try '$(basename ${0}) --help' for more information."
exit 1
fi
fi
if [ $# -ne 0 ]; then
echo "${skip_duplicate}${@}" > "$NQUEUE"
else
while read LINE; do
echo "${skip_duplicate}${LINE}" > "$NQUEUE"
done
fi