ubuntu twemproxy 服务编写
1. 编写服务脚本
#! /bin/sh ### BEGIN INIT INFO # Provides: twemproxyredis # Required-Start: $all # Required-Stop: # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: start twemproxyredis ### END INIT INFO prog="nutcracker" count=`ps -ef|grep ${prog} | grep -v grep|wc -l`; pids=`ps -ef | grep ${prog} | grep -v grep | awk '{print $2}'` start() { while : do local_host="`hostname --fqdn`" local_ip=`host $local_host 2>/dev/null | awk '{print $NF}'` index=`expr index "$local_ip" NX` length=`expr ${index} - 4` newip=${local_ip:0:${length}} if [ "${newip}" == "192.168.13.128" ] then break; else sleep 1s fi done if [ $count -eq 0 ]; then echo "${prog} start" cd /usr/local/twemproxy/sbin nutcracker -d -c /usr/local/twemproxy/sbin/conf/nutcracker.yml -p /usr/local/twemproxy/run/nutcracker.pid -o /usr/local/twemproxy/run/nutcracker.log fi } stop() { if [ $count != 0 ]; then echo "${prog} stop" for skill in $pids; do kill -9 ${skill} done fi } case "$1" in start) start exit 0 ;; stop) stop exit 0 ;; reload|restart|force-reload) stop start exit 0 ;; **) echo "Usage: $0 {start|stop|reload}" 1>&2 exit 1 ;; esac
2 我上面编写脚本未 bin/bash 脚本,而默认ubuntu 执行的是bin/dash 下的。这里需要执行命令进行切换到/bin/bash
sudo dpkg-reconfigure dash
3 把服务添加到开机启动中
sudo update-rc.d twemproxy defaults 90 10
4 移除开机启动命令
sudo update-rc.d -f twemproxy remove
5 开启服务,并设置开机启动
service twemprxoy start systemctl enable twemproxy