Loading scripts/runner.sh 0 → 100755 +121 −0 Original line number Diff line number Diff line #!/usr/bin/env bash function help() { echo "Usage:" echo " $(basename "$0") COMMAND" echo "" echo " Where COMMAND is one of:" echo " start PIDFILE PROGRAM ARGS... Run PROGRAM and record its PID in PIDFILE" echo " stop SIGNAL PIDFILE Send SIGNAL to the PID contained in PIDFILE" echo " help Print this message" } function readpid() { if [ $# -eq 0 ]; then echo "FATAL: readpid(): Missing pifile" >&2 exit 1 fi pidfile="$1" read -r pid <"$pidfile" echo $pid } function run() { if [[ -n "${RUNNER_SKIP_START}" && "${RUNNER_SKIP_START}" != "0" ]]; then exit 0 fi if [ $# -eq 0 ]; then echo "FATAL: missing program pidfile" >&2 elif [ $# -eq 1 ]; then echo "FATAL: missing program to run" >&2 help exit 1 fi pidfile="$1" shift if [ -e "$pidfile" ]; then pid=$(readpid "$pidfile") echo "$pid" if pgrep --pidfile "$pidfile"; then exit 1 fi fi "$@" 2>runner.$$.err 1>runner.$$.out 0</dev/null & pid=$! echo $pid >"$pidfile" sleep 1 if ! kill -0 $pid; then echo "Process $pid does not seem to exist." >&2 echo "The program below may not exist or may have crashed while starting:" >&2 echo " $*" >&2 echo " STDOUT: " >&2 cat "runner.$$.out" >&2 echo " STDERR: " >&2 cat "runner.$$.err" >&2 rm runner.$$.out runner.$$.err exit 1 fi exit 0 } function stop() { if [ $# -eq 0 ]; then echo "FATAL: missing signal" >&2 exit 1 elif [ $# -eq 1 ]; then echo "FATAL: missing pidfile" >&2 exit 1 fi signal="$1" pidfile="$2" if [ ! -e "$pidfile" ]; then echo "FATAL: pidfile '$pidfile' does not exist" >&2 exit 1 fi if pkill "-$signal" --pidfile "$pidfile"; then rm "$pidfile" rm runner.$$.out runner.$$.err exit 0 fi exit 1 } if [ $# -eq 0 ]; then echo "FATAL: missing arguments" >&2 help exit 1 fi case $1 in start) shift run "$@" ;; stop) shift stop "$@" ;; help) help exit 0 ;; esac Loading
scripts/runner.sh 0 → 100755 +121 −0 Original line number Diff line number Diff line #!/usr/bin/env bash function help() { echo "Usage:" echo " $(basename "$0") COMMAND" echo "" echo " Where COMMAND is one of:" echo " start PIDFILE PROGRAM ARGS... Run PROGRAM and record its PID in PIDFILE" echo " stop SIGNAL PIDFILE Send SIGNAL to the PID contained in PIDFILE" echo " help Print this message" } function readpid() { if [ $# -eq 0 ]; then echo "FATAL: readpid(): Missing pifile" >&2 exit 1 fi pidfile="$1" read -r pid <"$pidfile" echo $pid } function run() { if [[ -n "${RUNNER_SKIP_START}" && "${RUNNER_SKIP_START}" != "0" ]]; then exit 0 fi if [ $# -eq 0 ]; then echo "FATAL: missing program pidfile" >&2 elif [ $# -eq 1 ]; then echo "FATAL: missing program to run" >&2 help exit 1 fi pidfile="$1" shift if [ -e "$pidfile" ]; then pid=$(readpid "$pidfile") echo "$pid" if pgrep --pidfile "$pidfile"; then exit 1 fi fi "$@" 2>runner.$$.err 1>runner.$$.out 0</dev/null & pid=$! echo $pid >"$pidfile" sleep 1 if ! kill -0 $pid; then echo "Process $pid does not seem to exist." >&2 echo "The program below may not exist or may have crashed while starting:" >&2 echo " $*" >&2 echo " STDOUT: " >&2 cat "runner.$$.out" >&2 echo " STDERR: " >&2 cat "runner.$$.err" >&2 rm runner.$$.out runner.$$.err exit 1 fi exit 0 } function stop() { if [ $# -eq 0 ]; then echo "FATAL: missing signal" >&2 exit 1 elif [ $# -eq 1 ]; then echo "FATAL: missing pidfile" >&2 exit 1 fi signal="$1" pidfile="$2" if [ ! -e "$pidfile" ]; then echo "FATAL: pidfile '$pidfile' does not exist" >&2 exit 1 fi if pkill "-$signal" --pidfile "$pidfile"; then rm "$pidfile" rm runner.$$.out runner.$$.err exit 0 fi exit 1 } if [ $# -eq 0 ]; then echo "FATAL: missing arguments" >&2 help exit 1 fi case $1 in start) shift run "$@" ;; stop) shift stop "$@" ;; help) help exit 0 ;; esac