安裝完redis-2.8.13 之後發現沒有自啟動指令碼,自己動手寫了一個:
指令碼一:
#!/bin/sh
# chkconfig: 345 86 14# description: Startup and shutdown script for RedisREDIS_DIR=/usr/local/bin#export $REDIS_DIRREDIS_CONF=/etc/redis.confREDIS_PID=/var/run/redis.pidcase $1 in start|s) redis-server $REDIS_CONF ;; 'stop' ) echo "Stopping Redis..." kill `cat $REDIS_PID` ;; 'restart'|'reload'|'r' ) ${0} stop ${0} start ;; 'list'|'l' ) ps aux | egrep '(PID|redis-server)' ;; *)echo "usage: `basename $0` {start|restart|reload|stop|list}"esac
指令碼二:
#!/bin/sh# chkconfig: 345 86 14# description: Startup and shutdown script for RedisREDIS_DIR=/usr/local/bin#export $REDIS_DIRREDIS_CONF=/etc/redis.confREDIS_PID=/var/run/redis.pidcase $1 in start|s) if test -x $REDIS_DIR/redis-server then echo "Starting Redis..." if $REDIS_DIR/redis-server $REDIS_CONF then echo "OK" else echo "failed" fi else echo "Couldn't find Redis Server ($REDIS_DIR/redis-server)" fi ;; 'stop' ) echo "Stopping Redis..." kill `cat $REDIS_PID` ;; 'restart'|'reload'|'r' ) ${0} stop ${0} start ;; 'list'|'l' ) ps aux | egrep '(PID|redis-server)' ;; *)echo "usage: `basename $0` {start|restart|reload|stop|list}"esac
cp ./redisd /etc/rc.d/init.d/redisd
註冊服務:
chmod +x /etc/rc.d/init.d/redisd
chkconfig --add redisd
chkconfig --level 345 redisd on
chkconfig --list redisd
啟動服務:
/etc/init.d/redis2 start
帶上不同的參數代表不同的服務需求,參數分別為:start,restart,reload,stop,list
在執行shell指令碼時提示這樣的錯誤:
/bin/bash^M: bad interpreter: No such file or dire
主要是由於shell指令檔是dos格式,即每一行結尾以\r\n來標識,而unix格式的檔案行尾則以\n來標識;
解決方案:
(1)使用linux命令dos2unix filename,直接把檔案轉換為unix格式
(2)使用sed命令sed -i "s/\r//" filename 或者 sed -i "s/^M//" filename直接替換結尾符為unix格式
(3)vi filename開啟檔案,執行 : set ff=unix 設定檔案為unix,然後執行:wq,儲存成unix格式。
本人傾向使用第三中方案,最簡單實用。
這樣整個redis自啟動服務指令碼就完成了。