Skip to content

Instantly share code, notes, and snippets.

@hirose31
Created February 23, 2012 06:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirose31/1891020 to your computer and use it in GitHub Desktop.
Save hirose31/1891020 to your computer and use it in GitHub Desktop.
daemontools詰め合わせ
### helper shell function
daemonup() {
[ -z "$1" ] && return
case $1 in
*/*)
DAEMONDIR=$1
;;
*)
if [ -d "$SYS_HOME" ]; then
DAEMONDIR=$SYS_HOME/daemon/$1
else
DAEMONDIR=/usr/irori/daemon/$1
fi
;;
esac
[ -d $DAEMONDIR ] || { echo "no such dir: $DAEMONDIR"; return; }
d=${DAEMONDIR##*/}
if [ ! -s "/service/$d" ]; then
ln -snf ${DAEMONDIR} /service/
fi
/command/svc -u /service/$d >/dev/null 2>&1
}
daemondown() {
[ -z "$1" ] && return
daemon=${1##*/}
if [ -s /service/${daemon} ]; then
mv /service/${daemon} /service/.${daemon}
/command/svc -dx /service/.${daemon}
if [ -d /service/.${daemon}/log ]; then
/command/svc -dx /service/.${daemon}/log
fi
rm -f /service/.${daemon}
else
echo "not found: /service/${daemon}"
fi
}
daemonstat() {
local -a ds
if [ $# -gt 0 ]; then
for i in "$@"; do
i=${i#/service/}
ds[${#ds[@]}]="/service/${i}"
done
else
ds="/service/*"
fi
svstat ${ds[@]} | \
while read daemon state dsec pid sec dumy; do
daemon=${daemon##*/}
[ "$state" == "down" ] && sec=$dsec
printf "%-29s %4s %8ds %3ddays %02d:%02d:%02d ~%s\n" \
$daemon $state $sec \
$((sec / (60 * 60 * 24) )) \
$(( (sec / (60 * 60 )) % 24 )) \
$(( (sec / 60) % 60 )) \
$((sec % 60)) \
"$(date -d "$sec seconds ago" "+%y/%m/%d %T")";
done
}
alias ds=daemonstat
tailess() {
[ $# -eq 1 ] || { echo '[ERROR] allow only one argument.'; return 1; }
f=$1
case $f in
*/*) ;;
*) f=/service/$f/log/main/current ;;
esac
[ -r "$f" ] || { echo "[ERROR] cannot open: $f"; return 1; }
tai64nlocal < "$f" | less
}
taitailf() {
#[ $# -eq 1 ] || { echo '[ERROR] allow only one argument.'; return 1; }
ff=
for f in "$@"; do
case $f in
*/*) ;;
*) f=/service/$f/log/main/current ;;
esac
[ -r "$f" ] || { echo "[ERROR] cannot open: $f"; return 1; }
ff="$ff $f"
done
tail -F $ff | tai64nlocal
}
### completion
_daemonup() {
local cur
cur=${COMP_WORDS[$COMP_CWORD]}
if [ -d "$SYS_HOME" ]; then
DAEMONDIR=$SYS_HOME/daemon;
else
DAEMONDIR=/usr/irori/daemon;
fi
WORDS=$(ls -1 $DAEMONDIR)
COMPREPLY=( $(compgen -W "$WORDS" -- "$cur") )
}
complete -F _daemonup daemonup
_daemondown() {
local cur
cur=${COMP_WORDS[$COMP_CWORD]}
WORDS=$(ls -1 /service)
COMPREPLY=( $(compgen -W "$WORDS" -- "$cur") )
}
complete -F _daemondown daemondown
complete -F _daemondown taitailf
complete -F _daemondown tailess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment