Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
nfaguirrec committed Nov 24, 2011
0 parents commit 3d07828
Show file tree
Hide file tree
Showing 4 changed files with 387 additions and 0 deletions.
174 changes: 174 additions & 0 deletions sjobq.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
#!/bin/bash
############################################################################
# Copyright (C) 2010 by Nestor Aguirre #
# nfaguirrec@iff.csic.es #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################

DATA_DIR="/var/tmp/$USER/sjobq.data"
COUNTER_FILE="$DATA_DIR/counter"
STOP_FILE="$DATA_DIR/stop"
REFRESH_INTERVAL="2"
CURRENT_JOB_HOME="$DATA_DIR/current"

update()
{
local lastID="`cat $CURRENT_JOB_HOME/pid 2> /dev/null`"
local currentPID=""

if [ -z "`ps -A | awk -v id=$lastID '($1==id){print "1"}'`" ]
then
pushd . &> /dev/null
cd $DATA_DIR
IDL="`ls *.bid 2> /dev/null | sed '{s/[.].*//g}' | sort -n | head -n1`"
popd &> /dev/null

# Saves the command to the history file
# exactly after that this has finished
if [ "`cat $CURRENT_JOB_HOME/alive`" -eq "1" ]
then
spentTime=$(( $(date +%s) - $(cat $CURRENT_JOB_HOME/beginTime) ))

echo "command = `cat $CURRENT_JOB_HOME/com`" >> $DATA_DIR/history
echo "dir = `cat $CURRENT_JOB_HOME/pwd`" >> $DATA_DIR/history
echo "time spent = $((spentTime/3600))h $((spentTime/60))m $((spentTime%60))s" >> $DATA_DIR/history
echo "" >> $DATA_DIR/history
fi

# Flag that indicate the end of the job
echo "0" > $CURRENT_JOB_HOME/alive

# If the previous job has finished, it will run the next command in the queue
if [ -n "$IDL" ]
then
# Register the begin time
echo $(date +%s) > $CURRENT_JOB_HOME/beginTime

# this will go to the appropriate directory
# where the command should be to executed
pushd . &> /dev/null
cd `cat $DATA_DIR/$IDL.pwd`

# this builds a litle script that run the command using nohup
# and updates the files necesary files for register it
echo "#!/bin/bash" > .tmp2156456
cat $DATA_DIR/$IDL.com | sed '{s/^/nohup /g;s/$/ \&/g}' >> .tmp2156456
echo "echo \$! > $CURRENT_JOB_HOME/pid" >> .tmp2156456

chmod +x .tmp2156456
./.tmp2156456
rm .tmp2156456

rm -f $DATA_DIR/$IDL.bid
mv $DATA_DIR/$IDL.com $CURRENT_JOB_HOME/com
mv $DATA_DIR/$IDL.pwd $CURRENT_JOB_HOME/pwd

# this will return to the original directory
popd &> /dev/null

sleep 2

currentPID="`cat $CURRENT_JOB_HOME/pid`"
pstree -p $currentPID > $CURRENT_JOB_HOME/tree
pstree -p $currentPID | awk 'BEGIN{RS="[()]"}($0~/^[[:digit:]]+$/){printf $0" "}' | sed 's/$/\n/' > $CURRENT_JOB_HOME/pids

# Flag that indicate the begin of the job
echo "1" > $CURRENT_JOB_HOME/alive
fi
fi

if [ -d "$CURRENT_JOB_HOME" ]
then
echo -n "Saving data for process $CURRENT_JOB_HOME ... "
else
echo -n "Making data directory for process in $CURRENT_JOB_HOME ... "
mkdir $CURRENT_JOB_HOME && echo "OK"
fi
}

start()
{
while [ 1 ]
do
if [ -f $STOP_FILE ]
then
rm $STOP_FILE
exit
fi

sleep $REFRESH_INTERVAL
update > /dev/null
done
}

stop()
{
echo "" > $STOP_FILE
sleep 3
rm -rf $DATA_DIR
}

case $1 in
start)
if [ ! -d "$DATA_DIR" ]
then
mkdir -p $DATA_DIR
fi

nohup $0 __start > $DATA_DIR/log 2> $DATA_DIR/err &

echo "=============================="
echo " SJobQ daemon has been started"
echo "=============================="
;;
stop)
isRunning="`ps -u $USER | grep "sjobq.d$" | awk '{a[NR]=$1}END{ if(a[2]==(a[1]+1)) print 0; else print 1}'`"
if [ $isRunning -eq "0" ]
then
echo "### Error ### The daemon sjobq.d is not running"
echo " run it using \"sjobq.d start\""
exit 1
else
echo "=============================="
echo " SJobQ daemon has been stopped"
echo "=============================="
stop
fi

;;
restart)
stop

if [ ! -d "$DATA_DIR" ]
then
mkdir -p $DATA_DIR
fi

nohup $0 __start > $DATA_DIR/log 2> $DATA_DIR/err &

echo "================================"
echo " SJobQ daemon has been restarted"
echo "================================"
;;
__start)
start
;;
*)
echo "Usage: sjobq.d {start|stop|restart}"
;;
esac

132 changes: 132 additions & 0 deletions sjobq.del
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#!/bin/bash
############################################################################
# Copyright (C) 2010 by Nestor Aguirre #
# nfaguirrec@iff.csic.es #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################

DATA_DIR="/var/tmp/$USER/sjobq.data"
COUNTER_FILE="$DATA_DIR/counter"
CURRENT_JOB_HOME="$DATA_DIR/current"
MAX_HISTORY="10"

showJobs()
{
pushd . &> /dev/null
cd $DATA_DIR
ID_LIST=`ls *.bid 2> /dev/null | sed '{s/[.].*//g}' | sort -n`
popd &> /dev/null

local lastID="`cat $CURRENT_JOB_HOME/pid 2> /dev/null`"
if [ -n "`ps -A | awk -v id=$lastID '($1==id){print "1"}'`" ]
then
currentPID="`cat $CURRENT_JOB_HOME/pid`"
spentTime=$(( $(date +%s) - $(cat $CURRENT_JOB_HOME/beginTime) ))
pstree -p $currentPID > $CURRENT_JOB_HOME/tree
pstree -p $currentPID | awk 'BEGIN{RS="[()]"}($0~/^[[:digit:]]+$/){printf $0" "}' | sed 's/$/\n/' > $CURRENT_JOB_HOME/pids

echo "-------------"
echo " Current job"
echo "-------------"
echo "pid = `cat $CURRENT_JOB_HOME/pid`"
echo "command = `cat $CURRENT_JOB_HOME/com`"
echo "dir = `cat $CURRENT_JOB_HOME/pwd`"
echo "time spent = $((spentTime/3600))h $((spentTime/60))m $((spentTime%60))s"
echo "pids = `cat $CURRENT_JOB_HOME/pids`"
echo "tree ="
cat $CURRENT_JOB_HOME/tree | sed '{s/^/\t/g}'
echo ""
fi


if [ -n "$ID_LIST" ]
then
echo "-----------"
echo " Job queue"
echo "-----------"

for procID in $ID_LIST
do
echo "id = `cat $DATA_DIR/$procID.bid`"
echo "command = `cat $DATA_DIR/$procID.com`"
echo "dir = `cat $DATA_DIR/$procID.pwd`"
echo ""
done
fi

if [ -f "$DATA_DIR/history" ]
then
echo "---------"
echo " History"
echo "---------"

cat $DATA_DIR/history | tail -n $(( 4*$MAX_HISTORY ))
fi
}

deleteJob()
{
ID=$1

pushd . &> /dev/null
cd $DATA_DIR
ID_LIST=`ls *.bid | sed '{s/[.].*//g}'`
popd &> /dev/null

if [ $ID = "current" ]
then
kill -9 `cat $CURRENT_JOB_HOME/pids`
return
fi

exist="0"
for IDL in $ID_LIST
do
if [ $IDL -eq $ID ]
then
exist="1"
rm $DATA_DIR/$ID.bid
rm $DATA_DIR/$ID.com
rm $DATA_DIR/$ID.pwd

echo "Job with id=$ID has been deleted !!"
return
fi
done

if [ $exist -eq "0" ]
then
echo "Job with id=$ID not found !!"
fi
}

if [ -z "`ps -u $USER | grep "sjobq.d$"`" ]
then
echo "### Error ### The daemon sjobq.d is not running"
echo " run it using \"sjobq.d start\""
exit 1
fi

if [ -n "$*" ]
then
for delId in $*
do
deleteJob $delId
done
else
showJobs
fi
59 changes: 59 additions & 0 deletions sjobq.push
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
############################################################################
# Copyright (C) 2010 by Nestor Aguirre #
# nfaguirrec@iff.csic.es #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################

DATA_DIR="/var/tmp/$USER/sjobq.data"
COUNTER_FILE="$DATA_DIR/counter"

if [ -z "`ps -u $USER | grep "sjobq.d$"`" ]
then
echo "### Error ### The daemon sjobq.d is not running"
echo " run it using \"sjobq.d start\""
exit 1
fi

BID="X"
if [ -f "$COUNTER_FILE" ]
then
previous=`cat $COUNTER_FILE`

if [ $previous -eq "32767" ]
then
BID="0"
else
BID=$(( $previous + 1 ))
fi

else
echo "0" > $COUNTER_FILE
BID=`cat $COUNTER_FILE`
fi
echo $BID > $COUNTER_FILE

COMMAND=$*

echo "id = $BID"
echo "command = $COMMAND"
echo "dir = $PWD"

echo "$BID" > $DATA_DIR/$BID.bid
echo "$COMMAND" > $DATA_DIR/$BID.com
echo "$PWD" > $DATA_DIR/$BID.pwd

22 changes: 22 additions & 0 deletions sjobq.stat
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
############################################################################
# Copyright (C) 2010 by Nestor Aguirre #
# nfaguirrec@iff.csic.es #
# #
# This program is free software; you can redistribute it and#or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
############################################################################

sjobq.del

0 comments on commit 3d07828

Please sign in to comment.