几个启动和检测脚本 防止自己忘记


nginx 启动脚本:

#!/bin/sh
# Startup script forthe Nginx
# chkconfig: - 8863
# description: Nginx isa free,open-source,high-performance HTTP Server and reverse proxy.
# program:/usr/local/nginx/sbin/nginx
# config:/usr/local/nginx/conf/nginx.conf
# pidfile:/usr/local/nginx/logs/nginx.pid
# Synopsis:
#        nginx [--help] [--version] {start|stop|restart|reload|status|update}
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
# Define variable
nginx=/usr/local/nginx/sbin/nginx
pidfile=/usr/local/nginx/nginx.pid
PROGRAM=`basename $0`
VERSION=1.0
# Functions
usage(){
    echo "Usage: $PROGRAM [--help] [--version] {start|stop|restart|reload|status|update}"
}
version(){
    echo "Version:$VERSION"
}
start(){
if[ -e $pidfile ]
   then
    echo "Nginx already running..."
   else
    echo -e "Starting Nginx:"
    action "nginx start ..."/bin/true
    /usr/local/nginx/sbin/nginx
    echo -e "nginx is OK"
fi
}
stop(){
if[ -e $pidfile ]
   then
    echo -e "Stopping Nginx: kill nginx"
    kill -QUIT `cat ${pidfile}`
    echo -e "OK"
    action "nginx stop ..."/bin/true
    else
    echo "Nginx already stopped..."
fi
}
reload(){
if[ -e $pidfile ]
   then
    echo -e "Reloading Nginx: reload "
    kill -HUP `cat ${pidfile}`
    action "nginx reload OK "/bin/true
else
    echo "Nginx is not running..."
fi
}
status(){
    if[ -e $pidfile ]
       then
        PID=`cat $pidfile`
        echo  "Nginx (pid $PID) is running..."
        action "nginx status OK "/bin/true
       else
        echo  "Nginx is stopped"
    fi
}
update(){
if[ -e $pidfile ]
   then
    echo -e "Updateing Nginx:  nginx update"
    kill -USR2 `cat ${pidfile}`
    action "nginx update OK !!! "/bin/true
else
    echo "Nginx is not running..."
fi
}
if[ $# -gt 0]
   then
    case$1in
        start)
            start
            ;;
     stop)
           stop
           ;;
       restart)
           stop
           start
           ;;
       reload)
           reload
           ;;
       status)
           status
           ;;
       update)
           update
           ;;
       --help)
           usage
           ;;
       --version)
           version
           ;;
       *)
           usage
   esac
  else
   usage
fi


cp nginxd /etc/init.d/nginx
chkconfig --add nginx
service nginx restart




memcached 脚本:

启动脚本:

#!/bin/bash
# author:kuangl
# date:2013-05-30
# description: Starts and stops the Memcached services.
# pidfile: /tmp/memcached1.pid
# config:  /usr/local/memcached
# chkconfig: - 55 45
# source function library
. /etc/rc.d/init.d/functions
memcached="/usr/local/memcached/bin/memcached"
[ -e $memcached ] || exit 1
start()
{
echo "Starting memcached:"
daemon $memcached -d -m 2048 -u root -l 127.0.0.1 -p 11211 -c 10240 -P /tmp/memcached1.pid
}
stop()
{
echo "Shutting down memcached"
killproc memcached
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?


memcached 检测脚本:

这个脚本要配合crontab 来使用

#!/bin/bash
#Func:test memcached dead  reboot memcached
#date:2014/03/27
#BY:renzhenxing
#version:v0.1
mem=`ps -ef | grep mem | grep "mem" | egrep -v "vmmemctl|grep"| wc -l`
if [ $mem -eq 1 ];then
 echo  "mem is ok!!!"
 exit 0
else
 echo  "mem is dead !"
 /usr/local/memcached/bin/memcached -d -m 2048 -u root -l 127.0.0.1 -p 11211 -c 10240 -P /tmp/memcached1.pid
fi
~




php-fpm 启动脚本:

#!/bin/bash
#
# Startup script for the PHP-FPM server.
#
# chkconfig: 345 85 15
# description: PHP is an HTML-embedded scripting language
# processname: php-fpm
# config: /usr/local/php/etc/php.ini
 
# Source function library.
. /etc/rc.d/init.d/functions
 
PHP_PATH=/data/soft
DESC="php-fpm daemon"
NAME=php-fpm
# php-fpm路径
DAEMON=$PHP_PATH/php/sbin/$NAME
# 配置文件路径
CONFIGFILE=$PHP_PATH/php/etc/php-fpm.conf
# PID文件路径(在php-fpm.conf设置)
PIDFILE=/tmp/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
 
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
 
rh_start() {
  $DAEMON -y $CONFIGFILE || echo -n " already running"
}
 
rh_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
 
rh_reload() {
  kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
 
case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        rh_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        rh_stop
        echo "."
        ;;
  reload)
        echo -n "Reloading $DESC configuration..."
        rh_reload
        echo "reloaded."
  ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        rh_stop
        sleep 1
        rh_start
        echo "."
        ;;
  *)
         echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
         exit 3
        ;;
esac
exit 0


jenkins 启动脚本:

#!/bin/sh
#chkconfig:345 90 90
#description:jenkins start scripts

. /etc/rc.d/init.d/functions
jenkins_num=`ps aux | grep jenkins.war | grep -v grep | wc -l`
jenkins_pid=`ps aux | grep jenkins.war | grep -v grep | awk '{print $2}'`
dir=/data/soft/jenkins
jen_boot=`nohup java -jar ${dir}/jenkins.war --httpPort=8000 >> /data/logs/jenkins/jenkins.log  >/dev/null 2>&1 &`

case $1 in
  start)
    if [ $jenkins_num -eq 1 ]
    then
       echo "Error:  jenkins is running."
    else
       ${jen_boot}
       echo "jenkins started successfully."
    fi
  ;;

  stop)
    if [ $jenkins_num -eq 1 ]
    then
       ${jenkins_pid}
       kill -9 $jenkins_pid
       echo "jenkins stoped successfully."
    else
echo "Error:  jenkins  is stoping."
    fi
  ;;

  restart)
    if [ $jenkins_num -eq 1 ]
    then
       ${jenkins_pid}
       kill -9 $jenkins_pid
       action " jenkins  停止成功." /bin/true
       ${jen_boot}
       action " jenkins  启动完成." /bin/true
    else
       echo "Error:nrpe is stoping"
       ${jen_boot}
       echo "nrpe started successfully."
    fi
   ;;

  help)
    echo $"Usage: $0 { start | stop | restart }" 
    exit 2
   ;;

  *)
    echo $"Usage: $0 { start | stop | restart }" 
    exit 2
esac



nagios client nrpe:

#!/bin/sh
#chkconfig:2345 90 90
#description:nrpe
nrpe_num=`ps aux | grep /bin/nrpe | grep -v grep | wc -l`
case $1 in
  start)
    if [ $nrpe_num -eq 1 ]
    then
       echo "Error:nrpe is running."
    else
       /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
       echo "nrpe started successfully."
    fi
  ;;
  stop)
    if [ $nrpe_num -eq 1 ]
    then
       nrpe_pid=`ps aux | grep /bin/nrpe | grep -v grep | awk '{print $2}'`
       kill -9 $nrpe_pid
       echo "nrpe stoped successfully."
    else
       echo "Error:nrpe is stoping."
    fi
  ;;
  restart)
    if [ $nrpe_num -eq 1 ]
    then
       nrpe_pid=`ps aux | grep /bin/nrpe | grep -v grep | awk '{print $2}'`
       kill -9 $nrpe_pid
       echo "nrpe stoped successfully."
       /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
       echo "nrpe started successfully."
    else
       echo "Error:nrpe is stoping"
       /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
       echo "nrpe started successfully."
    fi
esac


redis 启动脚本:

#!/bin/bash 
# Init file for redis 
# chkconfig: -345 80 12 
# description: redis daemon 
# processname: redis 
# config: /etc/redis.conf 
# pidfile: /var/run/redis.pid 

source /etc/init.d/functions 
BIN="/usr/local/bin" 
CONFIG="/etc/redis.conf" 
PIDFILE="/var/run/redis.pid" 

### Read configuration 
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG" 
RETVAL=0 
prog="redis-server" 
desc="Redis Server" 
start() { 
        if [ -e $PIDFILE ];then 
             echo "$desc already running...." 
             exit 1 
        fi 
        echo -n $"Starting $desc: " 
        daemon $BIN/$prog $CONFIG 
        RETVAL=$? 
        echo 
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog 
        return $RETVAL 
} 
stop() { 
        echo -n $"Stop $desc: " 
        killproc $prog 
        RETVAL=$? 
        echo 
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE 
        return $RETVAL 
} 
restart() { 
        stop 
        start 
} 
case "$1" in 
  start) 
        start 
        ;; 
  stop) 
        stop 
        ;; 
  restart) 
        restart 
        ;; 
  condrestart) 
        [ -e /var/lock/subsys/$prog ] && restart 
        RETVAL=$? 
        ;; 
  status) 
        status $prog 
        RETVAL=$? 
        ;; 
   *) 
        echo $"Usage: $0 {start|stop|restart|condrestart|status}" 
        RETVAL=1 
esac 
exit $RETVAL


zabbix_agentd 启动脚本:

#!/bin/bash  
#  
# chkconfig: - 55 45  
# description: zabbix_agentd  
# probe: false  
 
# Source function library.  
. /etc/rc.d/init.d/functions  
 
# Source networking configuration.  
. /etc/sysconfig/network  
 
# Check that networking is up. If you are running without a network, comment this out.  
[ "${NETWORKING}" = "no" ] && exit 0  
 
RETVAL=0 
progdir="/opt/zabbix/sbin/" 
prog="zabbix_agentd" 
 
start() {  
        # Start daemons.  
        if [ -n "`/sbin/pidof $prog`" ]; then  
                echo -n "$prog: already running"  
                failure $"$prog start"  
                echo  
                return 1  
        fi  
        echo -n $"Starting $prog: "  
        # we can't seem to use daemon here - emulate its functionality  
        su -c $progdir$prog - $USER  
        RETVAL=$?  
        usleep 100000  
        if [ -z "`/sbin/pidof $progdir$prog`" ]; then  
                # The child processes have died after fork()ing, e.g.  
                # because of a broken config file  
                RETVAL=1 
        fi  
        [ $RETVAL -ne 0 ] && failure $"$prog startup"  
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog && success $"$prog startup"  
        echo  
        return $RETVAL  
}  
stop() {  
        RETVAL=0 
        pid=  
        # Stop daemons.  
        echo -n $"Stopping $prog: "  
        pid=`/sbin/pidof -s $prog`  
        if [ -n "$pid" ]; then  
         kill -TERM $pid  
        else  
         failure $"$prog stop"  
         echo  
         return 1  
        fi  
        RETVAL=$?  
        [ $RETVAL -ne 0 ] && failure $"$prog stop"  
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog && success $"$prog stop"  
        echo  
        return $RETVAL  
}  
restart() {  
        stop  
# wait for forked daemons to die  
        usleep 1000000  
        start  
}  
 
# See how we were called.  
case "$1" in  
        start)  
                start  
                ;;  
        stop)  
                stop  
                ;;  
        restart)  
                restart  
                ;;  
        condrestart)  
                [ -f /var/lock/subsys/$prog ] && restart  
                ;;  
        *)  
                echo $"Usage: $0 {start|stop|restart|condrestart}"  
                exit 1  
esac  
 
exit $?


#测试 
#chkconfig --add zabbix_agentd
#chkconfig zabbix_agentd on