Scripts de inicialização do Apache 2 e MySQL para Mac OS X Leaopard
Para facilitar o processo de inicialização do Apache e MySQL no OSX Leopard eu resolvi criar dois scripts que iniciam e finalizam os daemons de ambos os serviços, abaixo estou descrevendo o código fonte e as instruções de como utilizar cada um...
A
Admin
29 Jan, 2008 ·
5 min de leitura
Para facilitar o processo de inicialização do Apache e MySQL no OSX Leopard eu resolvi criar dois scripts que iniciam e finalizam os daemons de ambos os serviços, abaixo estou descrevendo o código fonte e as instruções de como utilizar cada um deles:
Criando o scripts:
Primeiramente é necessário criar um diretório para armazenar os scripts de inicialização eu optei em manter o padrão de estrutura de arquivos e diretórios Unix armazenando todos os arquivos dentro do diretório /etc, criei um diretório chamado lamp2.inits
sudo -s
mkdir /etc/lamp2.inits
Criando script de inicialização do Apache 2
Dentro do diretório lamp2.inits, crie um script chamado apache2 e dentro dele adicione o conteúdo abaixo:
#!/bin/sh
#
# ======================================.
# APACHE 2 Control
# desenvolvido por Andre Jaccon
# Developer Labs Blue Systems Tecnologia
# [email protected]
# =====================================.
APACHECTL=/usr/sbin/apachectl
TIME_START=`date +%D-%M-%Y`
TIME_STOP=`date +%D-%M-%Y`
function apache_start ()
{
if ( `$APACHECTL start` 2> /dev/null ) ;then
tput cup 0 30
echo "[OK]"
else
tput cup 0 30
echo "[ERROR]"
fi
}
function apache_stop ()
{
if ( `$APACHECTL stop` 2> /dev/null ) ;then
tput cup 0 30
echo "[OK]"
else
tput cup 0 30
echo "[ERROR]"
fi
}
function apache_status ()
{
if ( `ps -aef |grep httpd ` 2> /dev/null ) ;then
tput cup 0 30
echo "[OK] SERVER ON..."
else
tput cup 0 30
echo "[ERROR] SERVER DOWN..."
fi
}
case $1 in
start)
tput clear
echo " Iniciando daemon Apache2 ..."
apache_start
echo "$TIME_START"
;;
stop)
tput clear
echo " Iniciando daemon Apache2 ..."
apache_stop
echo "$TIME_STOP"
;;
status)
tput clear
apache_status
;;
*)
echo "Entre com as opcoes [ start ou stop ]"
;;
esac
Feito isso entre com o comando abaixo para iniciar o daemon através do script:
chmod +x apache2 ; ./apache
Com isso o Apache 2 irá iniciar, para verificar se o daemon está rodando você poderá contar com várias opções:
1) rodando o comando: netstat -na, e procurando pela porta 80 no estado LISTENING;
2) rodando o comando: ps aux |grep httpd, procurando pelo PID do processo do Apache 2
3) rodando o comando: telnet localhost 80
Configurando script de inicialização do MySQL
Seguindo o mesmo padrão do script do apache2, crie um arquivo com o nome MySQL e adicione o conteúdo abaixo:
( script optimizado para o Mac OS X )
!/bin/sh
# Set some defaults
datadir=/usr/local/mysql/data
pid_file=
if test -z "$basedir"
then
basedir=/usr/local/mysql
bindir=./bin
else
bindir="$basedir/bin"
fi
#
# Use LSB init script functions for printing messages, if possible
#
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
source $lsb_functions
else
log_success_msg()
{
echo " SUCCESS! $@"
}
log_failure_msg()
{
echo " ERROR! $@"
}
fi
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
export PATH
mode=$1 # start or stop
case `echo "testing\c"`,`echo -n testing` in
*c*,-n*) echo_n= echo_c= ;;
*c*,*) echo_n=-n echo_c= ;;
*) echo_n= echo_c='\c' ;;
esac
parse_arguments() {
for arg do
case "$arg" in
--basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
--pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
esac
done
}
wait_for_pid () {
i=0
while test $i -lt 35 ; do
sleep 1
case "$1" in
'created')
test -s $pid_file && i='' && break
;;
'removed')
test ! -s $pid_file && i='' && break
;;
*)
echo "wait_for_pid () usage: wait_for_pid created|removed"
exit 1
;;
esac
echo $echo_n ".$echo_c"
i=`expr $i + 1`
done
if test -z "$i" ; then
log_success_msg
else
log_failure_msg
fi
}
# Get arguments from the my.cnf file,
# groups [mysqld] [mysql_server] and [mysql.server]
if test -x ./bin/my_print_defaults
then
print_defaults="./bin/my_print_defaults"
elif test -x $bindir/my_print_defaults
then
print_defaults="$bindir/my_print_defaults"
elif test -x $bindir/mysql_print_defaults
then
print_defaults="$bindir/mysql_print_defaults"
else
# Try to find basedir in /etc/my.cnf
conf=/etc/my.cnf
print_defaults=
if test -r $conf
then
subpat='^[^=]*basedir[^=]*=\(.*\)$'
dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
for d in $dirs
do
d=`echo $d | sed -e 's/[ ]//g'`
if test -x "$d/bin/my_print_defaults"
then
print_defaults="$d/bin/my_print_defaults"
break
fi
if test -x "$d/bin/mysql_print_defaults"
then
print_defaults="$d/bin/mysql_print_defaults"
break
fi
done
fi
# Hope it's in the PATH ... but I doubt it
test -z "$print_defaults" && print_defaults="my_print_defaults"
fi
#
# Test if someone changed datadir; In this case we should also read the
# default arguments from this directory
#
extra_args=""
if test "$datadir" != "/usr/local/mysql/data"
then
extra_args="-e $datadir/my.cnf"
fi
parse_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
#
# Set pid file if not given
#
if test -z "$pid_file"
then
pid_file=$datadir/`/bin/hostname`.pid
else
case "$pid_file" in
/* ) ;;
* ) pid_file="$datadir/$pid_file" ;;
esac
fi
# Safeguard (relative paths, core dumps..)
cd $basedir
case "$mode" in
'start')
# Start daemon
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script may
# be overwritten at next upgrade.
echo $echo_n "Starting MySQL"
$bindir/mysqld_safe --datadir=$datadir --pid-file=$pid_file >/dev/null 2>&1 &
wait_for_pid created
# Make lock for RedHat / SuSE
if test -w /var/lock/subsys
then
touch /var/lock/subsys/mysql
fi
else
log_failure_msg "Can't execute $bindir/mysqld_safe"
fi
;;
'stop')
# Stop daemon. We use a signal here to avoid having to know the
# root password.
if test -s "$pid_file"
then
mysqld_pid=`cat $pid_file`
echo $echo_n "Shutting down MySQL"
kill $mysqld_pid
# mysqld should remove the pid_file when it exits, so wait for it.
wait_for_pid removed
# delete lock for RedHat / SuSE
if test -f /var/lock/subsys/mysql
then
rm -f /var/lock/subsys/mysql
fi
else
log_failure_msg "MySQL PID file could not be found!"
fi
;;
'restart')
# Stop the service and regardless of whether it was
# running or not, start it again.
$0 stop
$0 start
;;
'reload')
if test -s "$pid_file" ; then
mysqld_pid=`cat $pid_file`
kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
touch $pid_file
else
log_failure_msg "MySQL PID file could not be found!"
fi
;;
*)
# usage
echo "Usage: $0 start|stop|restart|reload"
exit 1
;;
esac
Depois execute: chmod +x mysql , ./mysql
Pronto agora é só adicionar os scripts na inicialização do sistema, ou caso queira rodar manualmente os daemons chame eles pelo terminal.
Até a próxima.