Skip to content

Instantly share code, notes, and snippets.

@AuroransSolis
Last active March 28, 2020 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AuroransSolis/55cb0a6222e478219fae07504a31f19d to your computer and use it in GitHub Desktop.
Save AuroransSolis/55cb0a6222e478219fae07504a31f19d to your computer and use it in GitHub Desktop.
Two script files I wrote to help automate my usage of mfakto.
#!/bin/sh
# Requires:
# - entr
# - tmux
# - mfloop
# - mfloop-runner.sh
################################################################################
# Edit the variables here to match the locations of things in your system.
# The next two should be absolute paths (couldn't figure out how to get ones
# with ~ or . or .. in them to work for some reason).
MFAKTO_DIR=""
MFLOOP_RUNNER=""
# Feel free to change these next two to whatever you want. These are just my
# personal preferences.
TMUX_SESSION="mfakto"
MFAKTO_ARGS="-v 1"
################################################################################
WORKTODO_PATH="$MFAKTO_DIR/worktodo.txt"
MFAKTO_PID=$(pidof mfakto)
ENTR_PID=$(pgrep -a entr | grep mfloop-runner | cut -d ' ' -f 1)
# Storing this as a string since I couldn't get using booleans to work correctly
TMUX_SESSION_ACTIVE=$(tmux ls | grep $TMUX_SESSION)
HELP=$(cat <<-END
mfakto and mfloop.py helper script written by Aurorans Solis
Options:
start-fetch
starts entr process watching worktodo.txt and runs mfloop with a
timeout of 0 every time the file is edited
start
starts a tmux session running mfakto and uses entr to watch
worktodo.txt, running mfloop with a timeout of 0 every time the file
is edited by mfakto to fetch more work
stop-fetch
kills the entr process running mfloop so no more work is fetched
stop
stops entr, the tmux session, and mfakto
status NUM_LINES
displays the last NUM_LINES lines of output in the tmux session
help
prints this message
END
)
case $1 in
"start-fetch")
if [[ -z $ENTR_PID ]]; then
echo "starting entr watching worktodo.txt and disowning process"
ls $WORKTODO_PATH | entr -r -p $MFLOOP_RUNNER & disown
echo "started entr"
else
echo "entr already started"
fi
;;
"start")
if [[ -z $MFAKTO_PID ]]; then
if [[ -z $TMUX_SESSION_ACTIVE ]]; then
echo "mfakto not running and tmux session not active"
echo "starting tmux session '$TMUX_SESSION'"
tmux new-session -d -s $TMUX_SESSION
else
echo "tmux session active but mfakto is not running"
fi
echo "starting mfakto in tmux session '$TMUX_SESSION'"
tmux send-keys -t $TMUX_SESSION.0 "cd $MFAKTO_DIR" ENTER \
"./mfakto $MFAKTO_ARGS" ENTER
echo "started mfakto"
else
if [[ -z $TMUX_SESSION_ACTIVE ]]; then
echo "mfakto started outside tmux session '$TMUX_SESSION'"
echo "sending SIGTERM to mfakto and waiting for it to stop"
kill -15 SIGINT $MFAKTO_PID
while :
do
if [[ -z $(pidof mfakto) ]]; then
echo "mfakto has stopped"
break
fi
done
echo "starting tmux session '$TMUX_SESSION'"
tmux new-session -s $TMUX_SESSION
echo "starting mfakto in tmux session '$TMUX_SESSION'"
tmux send-keys -t $TMUX_SESSION.0 "cd $MFAKTO_DIR" ENTER \
"./mfakto $MFAKTO_ARGS" ENTER
echo "started mfakto in tmux session '$TMUX_SESSION'"
else
echo "mfakto already started and tmux session exists"
fi
fi
if [[ -z $ENTR_PID ]]; then
echo "starting entr watching $WORKTODO_PATH and disowning process"
ls $WORKTODO_PATH | entr -r -p $MFLOOP_RUNNER & disown
echo "started entr"
else
echo "entr already started"
fi
echo "completed startup"
;;
"stop-fetch")
if [[ -z $ENTR_PID ]]; then
echo "entr already stopped"
else
kill -15 $ENTR_PID
fi
;;
"stop")
echo "stopping work"
if [[ -z $MFAKTO_PID ]]; then
echo "mfakto already stopped"
else
echo "sending SIGTERM to mfakto and waiting for it to stop"
kill -15 $MFAKTO_PID
while :
do
if [[ -z $(pidof mfakto) ]]; then
echo "mfakto has stopped"
break
fi
done
fi
if [[ -z $TMUX_SESSION_ACTIVE ]]; then
echo "tmux session '$TMUX_SESSION' already stopped"
else
echo "stopping tmux session '$TMUX_SESSION'"
tmux kill-session -t $TMUX_SESSION
fi
if [[ -z $ENTR_PID ]]; then
echo "entr already stopped"
else
echo "stopping entr"
kill -15 $ENTR_PID
fi
;;
"status")
if [[ -z $2 ]]; then
echo "last 10 lines of output from tmux session '$TMUX_SESSION':"
else
echo "last $2 lines of output from tmux session '$TMUX_SESSION':"
fi
tmux capture-pane -pt "$TMUX_SESSION" | tail -$2
;;
"help")
echo "$HELP"
;;
*)
echo "unrecognized option: '$1'"
echo "$HELP"
;;
esac
#!/bin/sh
PNUSER=""
PNPASS=""
GPU72USER=""
GPU72PASS=""
GPU72TYPE=""
# Shouldn't be more than 100
NUMCACHE=""
# The next two should be absolute paths (couldn't figure out how to get ones
# with ~ or . or .. in them to work for some reason).
MFAKTO_DIR=""
MFLOOP_PATH=""
CACHED_TASKS=$(wc -l $MFAKTO_DIR/worktodo.txt | cut -d ' ' -f 1)
DATE=$(date)
echo $DATE >> $MFAKTO_DIR/mfloop.log
echo $DATE >> $MFAKTO_DIR/mfloop.err.log
if [ "$CACHED_TASKS" -ge "$NUMCACHE" ]; then
echo "already have $CACHED_TASKS tasks cached, not requesting more" \
>> $MFAKTO_DIR/mfloop.log
else
echo "only have $CACHED_TASKS of $NUMCACHE cached, requesting more" \
>> $MFAKTO_DIR/mfloop.log
python2 $MFLOOP_PATH --username=$PNUSER --password=$PNPASS \
--gpu72user=$GPU72USER --gpu72pass=$GPU72PASS --gpu72type=$GPU72TYPE \
--num_cache=$NUMCACHE --workdir=$MFAKTO_DIR --timeout=0 \
>> $MFAKTO_DIR/mfloop.log 2>> $MFAKTO_DIR/mfloop.err.log
fi
echo >> $MFAKTO_DIR/mfloop.log
echo >> $MFAKTO_DIR/mfloop.err.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment