Friday, January 27, 2012

CentOS PHP FastCGI init script

#!/bin/sh  
#
# php-fcgi      Startup script for PHP FCGI
#  
# chkconfig:   345 86 14   
# description:  PHP FCGI server   
# pidfile:     /var/run/php-fcgi/pid  
  
PATH=/sbin:/bin:/usr/bin:/usr/sbin
  
# Source function library.  
. /etc/rc.d/init.d/functions
  
# Get config.   
test -f /etc/sysconfig/network && . /etc/sysconfig/network
  
# Check that we are root ... so non-root users stop here  
[ `id -u` = 0 ] || exit 1
  
# Check that networking is up.  
[ "${NETWORKING}" = "yes" ] || exit 0
 
USER=apache
RUNDIR_OWNER=root:apache

PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000
ALLOWED_ENV="PATH USER"
  
RUNDIR="/var/run/php-fcgi"
BIND="$RUNDIR/socket"
PIDFILE="$RUNDIR/pid"SUBSYS_FILE=/var/lock/subsys/php-fcgi
PHP_CGI=/usr/bin/php-cgi

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"

RETVAL=0

start() {
        if [ ! -d "$RUNDIR" ] ; then
            if [ ! -e "$RUNDIR" -a ! -h "$RUNDIR" ]
            then
                mkdir -p "$RUNDIR" || exit 1
            fi
        fi
        chown -R $RUNDIR_OWNER "$RUNDIR"
        chmod 0775 "$RUNDIR"

        # clean environment
        E=
        for i in $ALLOWED_ENV; do E="$E $i=${!i}"; done


        echo -n "Starting PHP FastCGI: "
        daemon --user $USER --pidfile $PIDFILE "env - $E $PHP_CGI -q -b $BIND&> /dev/null &"
        RETVAL=$?
        pid=`pidof php-cgi`
        if [ -n "$pid" ]; then
            echo $pid > $PIDFILE
        fi
        echo  
        touch $SUBSYS_FILE
        return $RETVAL
}

stop(){
        echo -n "Stopping PHP FastCGI: "
        killproc -p $PIDFILE php-fcgi
        RETVAL=$?
        echo  
        rm -f $SUBSYS_FILE
        return $RETVAL
}

restart(){
        stop
        start
}

condrestart(){
    [ -e $SUBSYS_FILE ] && restart
    return 0
}


# See how we were called.  
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p $PIDFILE php-fcgi
        ;;
    restart)
        restart
        ;;
    condrestart)
        condrestart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"  
        RETVAL=1
esac

exit $RETVAL

Sunday, December 12, 2010

RHEL/Centos init script for a Catalyst FCGI server

UPDATE I've made a Gist
#!/bin/sh
#
# chkconfig:   345 86 14 
# description:  MyApp FCGI server 
# pidfile:     /var/run/myapp/pid

PATH=/sbin:/bin:/usr/bin:/usr/sbin

# Source function library.
. /etc/rc.d/init.d/functions

# Get config.
test -f /etc/sysconfig/network && . /etc/sysconfig/network

# Check that we are root ... so non-root users stop here
[ `id -u` = 0 ] || exit 1

# Check that networking is up.
[ "${NETWORKING}" = "yes" ] || exit 0

APP_HOME=/opt/myapp
RUN_DIR=/var/run/myapp
USER=myapprun

PID_FILE=$RUN_DIR/pid
SOCKET_FILE=$RUN_DIR/socket
SUBSYS_FILE=/var/lock/subsys/myapp

SCRIPT=$APP_HOME/script/myapp_fastcgi.pl
SCRIPT_ARGS="-l $SOCKET_FILE -p $PID_FILE -d"

# Check that the script is executable
[ -x $SCRIPT ] || exit 5

start(){
    echo -n $"Starting myapp: "

    daemon --user $USER $SCRIPT "$SCRIPT_ARGS >/dev/null 2>&1"
    RETVAL=$?
    echo
    touch $SUBSYS_FILE
    return $RETVAL
}

stop(){
    echo -n $"Stopping myapp: "
    killproc -p $PID_FILE myapp
    RETVAL=$?
    echo
    rm -f $SUBSYS_FILE
    return $RETVAL
}

restart(){
    stop
    start
}

condrestart(){
    [ -e $SUBSYS_FILE ] && restart
    return 0
}


# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p $PID_FILE myapp
        ;;
    restart)
        restart
        ;;
    condrestart)
        condrestart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        RETVAL=1
esac

exit $RETVAL

macbook, virtualbox and italian keyboard

I had problems with the italian keyboard when using virtualbox on my mac to run a windows guest, with some keys swapped... it was a very annoying issue (I need a lot my \ and < keys!) but I could easily solve it with this custom layout from the guys at life.innove.it. Thank you!

Wednesday, September 22, 2010

MacOS X Terminal Page Up/Down Fix

By default in the terminal application the “Page Up” and “Page Down” keys do not send an actual page up sequence to a session but are used to scroll the terminal buffer. I find this is not acceptable when I have to use programs that have their own buffer, like when I run an editor in a ssh session or I'm reading a man page.


This is how to get an x-term like behaviour:


  • Go to Terminal->Preferences->[Settings]->[Keyboard]
  • Find pg up, and pg down and change 'Action' to 'send string to shell:'.
    Click on the entry field and enter \033[5~ for Page Up
    and \033[6~ for Page Down. Remember that \033 is the Esc key!
  • Assign the buffer scrolling actions to Shift-Page Up and Shift-Page Down.
  •  

Other useful codes are \033[1~ for Home and \033[4~ for End.

Tuesday, September 7, 2010

RHEL/Centos init script for a starman web application

#!/bin/bash
#
# myapp-http        This starts and stops myapp http
#
# chkconfig: 345 56 50
# description: http server for myapp
#
# pidfile: /var/run/myapp-http.pid

PATH=/sbin:/bin:/usr/bin:/usr/sbin

# Source function library.
. /etc/init.d/functions

# Get config.
test -f /etc/sysconfig/network && . /etc/sysconfig/network

# Check that we are root ... so non-root users stop here
[ `id -u` = 0 ] || exit 1

# Check that networking is up.
[ "${NETWORKING}" = "yes" ] || exit 0

RETVAL=0

STARMAN="/usr/bin/starman"
MYAPP_HOME="/home/myapp/production"
PID_FILE="/var/run/myapp-http.pid"
PORT=8000
HOST=127.0.0.1
STARMAN_OPTS="-D -E deploy --pid $PID_FILE -I$MYAPP_HOME/lib --listen $HOST:$PORT $MYAPP_HOME/app.psgi"

start(){
    echo -n $"Starting myapp-http: "

    daemon --pidfile $PID_FILE $STARMAN "$STARMAN_OPTS"
    RETVAL=$?
    echo
    touch /var/lock/subsys/myapp-http
    return $RETVAL
}

stop(){
    echo -n $"Stopping $prog: "
    killproc -p $PID_FILE $prog  
    RETVAL=$?
    echo
    rm -f /var/lock/subsys/myapp-http
    return $RETVAL

}

restart(){
    stop
    start
}

condrestart(){
    [ -e /var/lock/subsys/myapp-http ] && restart
    return 0
}


# See how we were called.
case "$1" in
    start)
 start
 ;;
    stop)
 stop
 ;;
    status)
 status -p $PID_FILE starman
 ;;
    restart)
 restart
 ;;
    condrestart)
 condrestart
 ;;
    *)
 echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
 RETVAL=1
esac

exit $RETVAL