Skip to content

Instantly share code, notes, and snippets.

@anon5r
Last active February 3, 2018 13:02
Show Gist options
  • Save anon5r/5131581 to your computer and use it in GitHub Desktop.
Save anon5r/5131581 to your computer and use it in GitHub Desktop.
Simple control daemons installed by homebrew running on OS X
#!/bin/sh
#VERSION_PHP="5.6"
VERSION_PHP="7.2"
function help(){
echo "ex: $0 <daemon> <command>"
echo
echo "Support daemons:"
echo "\tnginx\t\tnginx"
echo "\tphp-fpm\t\tphp fast cgi php module"
echo "\tmysql\t\tmysql daemon"
echo "\tmariadb\t\tmariadb daemon"
echo "\tmemcached\tmemcached daemon"
echo "\tredis\t\tredis daemon"
echo "\tmongo\t\tmongodb daemon"
echo "\tpgsql\t\tpostgresql daemon"
echo "Support sub commands: { start, stop, restart, config, plist }"
echo
exit 1
}
function config_file() {
case "$1" in
"nginx")
echo "/usr/local/etc/nginx/nginx.conf"
;;
"php")
echo "/usr/local/etc/php/$VERSION_PHP/php.ini"
;;
"php-fpm")
echo "/usr/local/etc/php/$VERSION_PHP/php-fpm.conf"
;;
"memcached")
echo "/usr/local/etc/memcached.conf"
;;
"redis")
echo "/usr/local/etc/redis.conf"
;;
"mysql")
echo "/usr/local/etc/my.cnf"
;;
"mariadb")
echo "/usr/local/etc/my.cnf"
;;
"mongo")
echo "/usr/local/etc/mongod.conf"
;;
*)
echo "Unsupported config file specified."
exit 1
;;
esac
}
function check_config() {
case "$1" in
"nginx")
/usr/local/opt/nginx/bin/nginx -t
;;
"php")
/usr/local/opt/php$(echo $VERSION_PHP|sed -e "s/\.//g")/bin/php -r "echo 'Check done' . PHP_EOL;"
;;
"php-fpm")
/usr/local/opt/php$(echo $VERSION_PHP|sed -e "s/\.//g")/sbin/php-fpm -t
;;
*)
echo "Unsupported config file specified."
;;
esac
}
function config() {
case "$2" in
"show")
less `config_file $1`
;;
"edit")
vim `config_file $1`
;;
"path")
echo `config_file $1`
;;
"check")
check_config $1
;;
*)
echo "$0 $1 config <option>"
echo
echo "Supported options: "
echo "\tshow\t\tshow config file"
echo "\tedit\t\tedit config file by vim"
echo "\tcheck\t\tcheck config if supported by daemon"
echo "\tpath\t\treturn config file path"
echo
exit 1
;;
esac
}
function get_plist(){
case "$1" in
"nginx")
#PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.nginx.plist"
PLIST="/Library/LaunchDaemons/homebrew.mxcl.nginx.plist"
;;
"php-fpm")
#PLIST="$HOME/Library/LaunchAgents/php-fpm.plist"
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.php$(echo $VERSION_PHP|sed -e "s/\.//g").plist"
;;
"memcached")
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
;;
"redis")
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.redis.plist"
;;
"mysql")
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
;;
"mariadb")
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.mariadb.plist"
;;
"mongo")
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.mongodb.plist"
;;
"pgsql")
PLIST="$HOME/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
;;
*)
help
;;
esac
echo $PLIST
}
function check_daemon(){
case "$1" in
"nginx")
;;
"php-fpm")
;;
"memcached")
;;
"redis")
;;
"mysql")
;;
"mariadb")
;;
"mongo")
;;
"pgsql")
;;
*)
help
exit 1
;;
esac
}
function does_need_sudo(){
case "$1" in
"nginx")
echo 1
;;
*)
echo 0
;;
esac
}
function start(){
if [ $(does_need_sudo $1) -eq 1 ]; then
sudo launchctl load -w `get_plist $1`
else
launchctl load -w `get_plist $1`
fi
}
function stop(){
if [ $(does_need_sudo $1) -eq 1 ]; then
sudo launchctl unload `get_plist $1`
else
launchctl unload `get_plist $1`
fi
}
if [ "$1" = "" ]; then
help
fi
check_daemon $1
case "$2" in
"start")
if [[ "$TMUX" != "" ]]; then
echo "Please detach or exit tmux."
exit 1
fi
echo "Start $1...."
start $1
;;
"stop")
if [[ "$TMUX" != "" ]]; then
echo "Please detach or exit tmux."
exit 1
fi
echo "Stop $1...."
stop $1
;;
"restart")
if [[ "$TMUX" != "" ]]; then
echo "Please detach or exit tmux."
exit 1
fi
stop $1
start $1
;;
"config")
config $1 $3
;;
"plist")
echo "plist path is..."
get_plist $1
;;
*)
help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment