Changes in daemon

* changes in cmd parametrs
* some refactoring
+ added sleeptime parametr
- bug with pgrep
This commit is contained in:
arcan1s 2013-12-23 22:15:23 +04:00
parent d0672e76ce
commit f11944394f
3 changed files with 55 additions and 77 deletions

View File

@ -1,22 +1,25 @@
#!/bin/bash #!/bin/bash
# functions
error_mes() {
case "$1" in
"config" ) echo "[EE] Configuration file is not set";;
"file" ) echo "[EE]: '$2' is a file";;
"flag" ) echo "[EE] Unknown flag";;
"number" ) echo "[EE]: '$2' is not a number";;
"unknown" ) echo "[EE] Unknown error";;
esac
exit 1
}
func_help() { func_help() {
echo -e "Simple daemon written on BASH for starting jobs to queue of calculations" echo -e "Simple daemon written on BASH for starting jobs to queue of calculations"
echo -e "\nUsage: queued [ -jd | --jobdir /var/lib/queued/job ] [ --priority 0 ]" echo -e "\nUsage: queued [ -c /etc/queued.conf ] [ -v | --version ] [ -h | --help ]"
echo -e " [ -q | --queuefile /var/lib/queued/queue ] [ -u | --user root ]"
echo -e " [ -wd | --workdir /var/lib/queued/work ] [ -v | --version ]"
echo -e " [ -h | --help ]"
echo -e "\nParametrs:" echo -e "\nParametrs:"
echo -e " -jd --jobdir PATH - path to job directory. Default is '/var/lib/queued/job'" echo -e " -c PATH - path to configuration file. Default is '/etc/queued.conf'"
echo -e " --priority NUM - defalut priority. Default is '0'"
echo -e " -q --queuefile PATH - path to queue file. Default is '/var/lib/queued/queue'"
echo -e " -u --user USERNAME - start jobs by user. Default is 'root'"
echo -e " -wd --workdir PATH - path to work directory. Default is '/var/lib/queued/work'"
echo -e "\n -v --version - show version and exit" echo -e "\n -v --version - show version and exit"
echo -e " -h --help - show this help and exit" echo -e " -h --help - show this help and exit"
exit 0 exit 0
} }
func_ver() { func_ver() {
echo -e " queued " echo -e " queued "
echo -e "Simple daemon for starting jobs to queue of calculations" echo -e "Simple daemon for starting jobs to queue of calculations"
@ -25,87 +28,61 @@ func_ver() {
echo -e "E-mail : esalexeev (at) gmail.com" echo -e "E-mail : esalexeev (at) gmail.com"
exit 0 exit 0
} }
isnum() { isnum() {
(t=$(( 0$1+0 ))) 2>/dev/null (t=$(( 0$1+0 ))) 2>/dev/null
} }
start_job() {
echo "[II] Running job '$CURJOB' (priority '$CURJOB_PRIOR') as '$CURJOB_USER'"
su -c "/bin/sh "$JOBDIR/$(basename "$CURJOB")" &> "$JOBDIR/$(basename "$CURJOB")".log" $CURJOB_USER &
}
CONF_FILE="/etc/queued.conf"
# parametrs parsing
until [ -z $1 ]; do
case "$1" in
"-h" | "--help" ) func_help;;
"-v" | "--version" ) func_ver;;
"-c" ) [ -z "$2" ] && error_mes "config" || CONF_FILE="$2" && shift;;
* ) error_mes "flag";;
esac
shift
done
# default values # default values
JOBDIR="/var/lib/queued/job" JOBDIR="/var/lib/queued/job"
PRIORITY=0 PRIORITY=0
QUEUEFILE="/var/lib/queued/queue" QUEUEFILE="/var/lib/queued/queue"
SLEEPTIME=5
STARTASUSER="root" STARTASUSER="root"
WORKDIR="/var/lib/queued/work" WORKDIR="/var/lib/queued/work"
# parametrs parsing echo "[II] Reading configuration from '$CONF_FILE'"
until [ -z $1 ]; do for VAR in "$(cat "$CONF_FILE")"; do eval "$VAR"; done
if [ "$1" = "-h" ]; then
func_help; fi
if [ "$1" = "--help" ]; then
func_help; fi
if [ "$1" = "-v" ]; then
func_ver; fi
if [ "$1" = "--version" ]; then
func_ver; fi
if [ "$1" = "-jd" ]; then
JOBDIR="$2"
shift; fi
if [ "$1" = "--jobdir" ]; then
JOBDIR="$2"
shift; fi
if [ "$1" = "--priority" ]; then
PRIORITY="$2"
shift; fi
if [ "$1" = "-q" ]; then
QUEUEFILE="$2"
shift; fi
if [ "$1" = "--queuefile" ]; then
QUEUEFILE="$2"
shift; fi
if [ "$1" = "-u" ]; then
STARTASUSER="$2"
shift; fi
if [ "$1" = "--user" ]; then
STARTASUSER="$2"
shift; fi
if [ "$1" = "-wd" ]; then
WORKDIR="$2"
shift; fi
if [ "$1" = "--workdir" ]; then
WORKDIR="$2"
shift; fi
shift
done
# prepare
# creating directories if doesn't exist # creating directories if doesn't exist
if [ ! -d "$JOBDIR" ]; then if [ ! -d "$JOBDIR/done" ]; then
if [ -e "$JOBDIR" ]; then [ -e "$JOBDIR/done" ] && error_mes "file" "$JOBDIR/done"
echo "[EE]: '$JOBDIR' is a file" echo "[II]: Create directory '$JOBDIR/done'"
exit 1 mkdir -m777 -p "$JOBDIR/done" || error_mes "unknown"
fi
echo "[II]: Create directory '$JOBDIR'"
mkdir -m777 -p "$JOBDIR" || (echo "[EE] Unknown error"; exit 1)
fi fi
if [ ! -d "$WORKDIR" ]; then if [ ! -d "$WORKDIR" ]; then
if [ -e "$WORKDIR" ]; then [ -e "$WORKDIR" ] && error_mes "file" "$WORKDIR"
echo "[EE]: '$WORKDIR' is a file"
exit 1
fi
echo "[II]: Create directory '$WORKDIR'" echo "[II]: Create directory '$WORKDIR'"
mkdir -m777 -p "$WORKDIR" || (echo "[EE] Unknown error"; exit 1) mkdir -m777 -p "$WORKDIR" || error_mes "unknown"
fi fi
if [ ! -d "$(dirname "$QUEUEFILE")" ]; then if [ ! -d "$(dirname "$QUEUEFILE")" ]; then
if [ -e "$(dirname "$QUEUEFILE")" ]; then [ -e "$(dirname "$QUEUEFILE")" ] && error_mes "file" "$(dirname "$QUEUEFILE")"
echo "[EE]: '$(dirname "$QUEUEFILE")' is a file"
exit 1
fi
echo "[II]: Create directory '$(dirname "$QUEUEFILE")'" echo "[II]: Create directory '$(dirname "$QUEUEFILE")'"
mkdir -m777 -p "$(dirname "$QUEUEFILE")" || (echo "[EE] Unknown error"; exit 1) mkdir -m777 -p "$(dirname "$QUEUEFILE")" || error_mes "unknown"
echo "[II]: Create file '$QUEUEFILE'" echo "[II]: Create file '$QUEUEFILE'"
touch "$QUEUEFILE" || (echo "[EE] Unknown error"; exit 1) touch "$QUEUEFILE" || error_mes "unknown"
chmod 777 "$QUEUEFILE" chmod 777 "$QUEUEFILE"
fi fi
# check priority # check priority
isnum "$PRIORITY" || (echo "[EE]: '$PRIORITY' isn't a number"; exit 1) isnum "$PRIORITY" || error_mes "number" "$PRIORITY"
# check sleep time
isnum "$SLEEPTIME" && SLEEPTIME=$(($SLEEPTIME*60)) || error_mes "number" "$SLEEPTIME"
# work block # work block
@ -139,11 +116,11 @@ cd /
CALC=0 CALC=0
for FILE in $(ls "$JOBDIR/"); do for FILE in $(ls "$JOBDIR/"); do
[ -d "$JOBDIR/$FILE" ] && continue [ -d "$JOBDIR/$FILE" ] && continue
pgrep "$JOBDIR/$FILE" &> /dev/null && CALC=$(($CALC+1)) (ps aux | grep "$JOBDIR/$FILE" | grep --quiet --invert-match "grep") && CALC=$(($CALC+1))
done done
# running job # running job
if [ "$CALC" = "0" ]; then if [[ "$CALC" = "0" ]]; then
CURJOB_PRIOR=$(head -1 "$QUEUEFILE" | awk -F "==" '{print $1}') CURJOB_PRIOR=$(head -1 "$QUEUEFILE" | awk -F "==" '{print $1}')
CURJOB=$(head -1 "$QUEUEFILE" | awk -F "==" '{print $2}') CURJOB=$(head -1 "$QUEUEFILE" | awk -F "==" '{print $2}')
for JOB in $(cat "$QUEUEFILE"); do for JOB in $(cat "$QUEUEFILE"); do
@ -157,13 +134,13 @@ cd /
if [ -z "$CURJOB" ]; then if [ -z "$CURJOB" ]; then
echo "[II] You haven't job" echo "[II] You haven't job"
else else
mv "$CURJOB"* "$JOBDIR/" && (echo "[II] Running job '$CURJOB' (priority '$CURJOB_PRIOR') as '$CURJOB_USER'"; su -c "/bin/sh "$JOBDIR/$(basename "$CURJOB")" &> "$JOBDIR/$(basename "$CURJOB")".log" $CURJOB_USER ) || echo "[WW] Missing files for job '$CURJOB'" mv "$CURJOB"* "$JOBDIR/" && start_job || echo "[WW] Missing files for job '$CURJOB'"
fi fi
else else
echo "[II] You have a running job" echo "[II] You have a running job"
fi fi
# wait for 60 seconds # wait
sleep 60 sleep $SLEEPTIME
done done
) & ) &

View File

@ -10,5 +10,7 @@
## Additional parametrs ## Additional parametrs
# default priority # default priority
#PRIORITY=0 #PRIORITY=0
# time to sleep, minutes
#SLEEPTIME=5
# start as user # start as user
#STARTASUSER=root #STARTASUSER=root

View File

@ -2,9 +2,8 @@
Description=Queue daemon Description=Queue daemon
[Service] [Service]
EnvironmentFile=/etc/queued.conf
Type=forking Type=forking
ExecStart=/usr/bin/queued -jd $JOBDIR --priority $PRIORITY -q $QUEUEFILE -u $STARTASUSER -wd $WORKDIR ExecStart=/usr/bin/queued
RemainAfterExit=yes RemainAfterExit=yes
[Install] [Install]