2010-10-15 02:32:43 +02:00
|
|
|
#!/bin/bash
|
2008-08-09 06:38:44 +02:00
|
|
|
# Check for an argument
|
2008-12-04 23:38:32 +01:00
|
|
|
if [ ${1:-""} = "" ]; then
|
2008-08-09 06:38:44 +02:00
|
|
|
echo "Sapphire Sake
|
|
|
|
|
|
|
|
Usage: $0 (command-url) (params)
|
|
|
|
Executes a Sapphire command"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2008-08-26 06:10:37 +02:00
|
|
|
# Set $sapphire and $base
|
2009-10-16 00:24:17 +02:00
|
|
|
sakedir=`dirname $0`
|
|
|
|
if [ -f $sakedir/cli-script.php ]; then
|
|
|
|
sapphire=$sakedir
|
|
|
|
base=`dirname $sakedir`
|
|
|
|
elif [ -d ./sapphire ]; then
|
2008-08-26 06:10:37 +02:00
|
|
|
sapphire=./sapphire
|
|
|
|
base=.
|
|
|
|
elif [ -f ./cli-script.php ]; then
|
|
|
|
sapphire=.
|
|
|
|
base=..
|
|
|
|
else
|
|
|
|
echo "Can't find ./sapphire/cli-script.php or ./cli-script.php"
|
|
|
|
exit 1
|
2008-08-09 06:38:44 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Find the PHP binary
|
2008-08-11 01:29:30 +02:00
|
|
|
for candidatephp in php5 php; do
|
2008-08-11 05:45:55 +02:00
|
|
|
if [ `which $candidatephp` -a -f `which $candidatephp` ]; then
|
2008-08-09 06:38:44 +02:00
|
|
|
php=`which $candidatephp`
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ "$php" = "" ]; then
|
|
|
|
echo "Can't find any php binary"
|
|
|
|
exit 2
|
|
|
|
fi
|
|
|
|
|
2008-08-26 06:10:37 +02:00
|
|
|
################################################################################################
|
|
|
|
## Installation to /usr/bin
|
|
|
|
|
|
|
|
if [ "$1" = "installsake" ]; then
|
|
|
|
echo "Installing sake to /usr/bin..."
|
|
|
|
cp $0 /usr/bin
|
|
|
|
exit 0
|
2008-08-09 06:38:44 +02:00
|
|
|
fi
|
|
|
|
|
2008-08-26 06:10:37 +02:00
|
|
|
################################################################################################
|
|
|
|
## Process control
|
|
|
|
|
|
|
|
if [ "$1" = "-start" ]; then
|
|
|
|
if [ "`which daemon`" = "" ]; then
|
|
|
|
echo "You need to install the 'daemon' tool. In debian, go 'sudo apt-get install daemon'"
|
|
|
|
exit 1
|
|
|
|
fi
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2008-08-26 06:10:37 +02:00
|
|
|
if [ ! -f $base/$2.pid ]; then
|
|
|
|
echo "Starting service $2 $3"
|
|
|
|
touch $base/$2.pid
|
|
|
|
pidfile=`realpath $base/$2.pid`
|
|
|
|
|
|
|
|
outlog=$base/$2.log
|
|
|
|
errlog=$base/$2.err
|
|
|
|
|
|
|
|
echo "Logging to $outlog"
|
|
|
|
|
|
|
|
sake=`realpath $0`
|
|
|
|
base=`realpath $base`
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
# if third argument is not explicitly given, copy from second argument
|
|
|
|
if [ "$3" = "" ]; then
|
|
|
|
url=$2
|
|
|
|
else
|
|
|
|
url=$3
|
|
|
|
fi
|
2008-08-26 06:10:37 +02:00
|
|
|
|
|
|
|
# TODO: Give a globally unique processname by including the projectname as well
|
|
|
|
processname=$2
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
daemon -n $processname -r -D $base --pidfile=$pidfile --stdout=$outlog --stderr=$errlog $sake $url
|
2008-08-26 06:10:37 +02:00
|
|
|
else
|
|
|
|
echo "Service $2 seems to already be running"
|
|
|
|
fi
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$1" = "-stop" ]; then
|
|
|
|
pidfile=$base/$2.pid
|
|
|
|
if [ -f $pidfile ]; then
|
|
|
|
echo "Stopping service $2"
|
|
|
|
|
|
|
|
# TODO: This is a bad way of killing the process
|
|
|
|
kill -KILL `cat $pidfile`
|
|
|
|
unlink $pidfile
|
|
|
|
else
|
|
|
|
echo "Service $2 doesn't seem to be running."
|
|
|
|
fi
|
|
|
|
exit 0
|
2008-08-09 06:38:44 +02:00
|
|
|
fi
|
|
|
|
|
2008-08-26 06:10:37 +02:00
|
|
|
################################################################################################
|
|
|
|
## Basic execution
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
$php $sapphire/cli-script.php ${*}
|