Skip to content

Instantly share code, notes, and snippets.

@zhouyl
Last active August 29, 2017 10:29
Show Gist options
  • Save zhouyl/2ec9f4fc50626dbae07e77866010e0da to your computer and use it in GitHub Desktop.
Save zhouyl/2ec9f4fc50626dbae07e77866010e0da to your computer and use it in GitHub Desktop.
php daemon service
#!/usr/bin/env bash
if [ -f /usr/local/bin/php7 ]; then
php=/usr/local/bin/php7
else
php=`which php`
fi
check=$($php -r "echo version_compare(PHP_VERSION, '5.6.0') >= 0 ? 'yes' : 'no';")
if [ $check != "yes" ]; then
echo "Please use php 5.6.0 or above version! ($php)"
exit 0
fi
commands=(
"$php claw order:query"
"$php claw order:notifier"
)
root=$(cd "$(dirname "$0")"; cd ../; pwd)
logfile=$root/storage/logs/`date +%Y%m%d`/daemon-service.log
start() {
for ((i = 0; i < ${#commands[@]}; i++)) ; do
cmd=${commands[$i]}
pid=$(pgrep -f "$cmd")
if [ "$pid" ] ; then
echo -e "$cmd ($pid) \033[33;49;2m[ RUNNING ]\033[39;49;0m"
else
$cmd >> /dev/null &
echo -e "$cmd \033[32;49;2m[ STARTED ]\033[39;49;0m"
echo -e "$(date +'%Y-%m-%d %H:%M:%S') $cmd started." >> $logfile
fi
done
return 0
}
stop() {
for ((i = 0; i < ${#commands[@]}; i++)) ; do
cmd=${commands[$i]}
pid=$(pgrep -f "$cmd")
if [ "$pid" ] ; then
kill -15 $pid
echo -e "$cmd ($pid) \033[31;49;2m[ KILLED ] \033[39;49;0m"
echo -e "$(date +'%Y-%m-%d %H:%M:%S') $cmd killed." >> $logfile
else
echo -e "$cmd \033[37;49;2m[ NONE ]\033[39;49;0m"
fi
done
}
status() {
echo "Process status list:"
for ((i = 0; i < ${#commands[@]}; i++)) ; do
cmd=${commands[$i]}
pid=$(pgrep -f "$cmd")
if [ "$pid" ] ; then
echo -e "$cmd ($pid) \033[33;49;2m[ RUNNING ]\033[39;49;0m"
else
echo -e "$cmd \033[37;49;2m[ NONE ]\033[39;49;0m"
fi
done
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop && start
;;
*)
echo -e "Usage: $0 [start|stop|status|restart]\n"
status
;;
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment