#!/bin/sh # 編譯安裝管理Redis App=redis AppName=Redis AppBase=/App AppDir=$AppBase/$App AppProg=$AppDir/bin/redis-server AppConf=$AppDir/redis.conf AppSrcBase=/App/src AppSrcFile=$App-*.tar.* AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///') AppPort=$(grep "^port" $AppConf | awk '{print $2}') RemoveFlag=0 InstallFlag=0 # 獲取PID fpid() { AppPid=$(ps ax | grep "$AppProg" | grep -v "grep" | awk '{print $1}' 2> /dev/null) } # 查詢狀態 fstatus() { fpid if [ ! -f "$AppProg" ]; then echo "$AppName 未安裝" else echo "$AppName 已安裝" if [ -z "$AppPid" ]; then echo "$AppName 未啓動" else echo "$AppName 正在運行" fi fi } # 刪除 fremove() { fpid RemoveFlag=1 if [ -z "$AppPid" ]; then if [ -d "$AppDir" ]; then rm -rf $AppDir && echo "刪除 $AppName" else echo "$AppName 未安裝" fi else echo "$AppName 正在運行" && exit fi } # 備份 fbackup() { Day=$(date +%Y-%m-%d) BackupFile=$App.$Day.tgz if [ -f "$AppProg" ]; then cd $AppBase tar zcvf $BackupFile --exclude=logs/* $App --backup=numbered [ $? -eq 0 ] && echo "$AppName 備份成功" || echo "$AppName 備份失敗" else echo "$AppName 未安裝" fi } # 安裝 finstall() { fpid InstallFlag=1 if [ -z "$AppPid" ]; then test -f "$AppProg" && echo "$AppName 已安裝" [ $? -ne 0 ] && fupdate && fcpconf else echo "$AppName 正在運行" fi } # 拷貝配置 fcpconf() { cp -vf --backup=numbered $ScriptDir/redis.conf $AppConf } # 更新 fupdate() { Operate="更新" [ $InstallFlag -eq 1 ] && Operate="安裝" [ $RemoveFlag -ne 1 ] && fbackup cd $AppSrcBase test -d "$AppSrcDir" && rm -rf $AppSrcDir tar zxf $AppSrcFile || tar jxf $AppSrcFile || tar Jxf $AppSrcFile cd $AppSrcDir make PREFIX=$AppDir install if [ $? -eq 0 ]; then echo "$AppName $Operate成功" else echo "$AppName $Operate失敗" exit 1 fi } # 啓動 fstart() { fpid if [ -n "$AppPid" ]; then echo "$AppName 正在運行" else $AppProg $AppConf && echo "啓動 $AppName" || echo "$AppName 啓動失敗" fi } # 中止 fstop() { fpid if [ -n "$AppPid" ]; then $AppDir/bin/redis-cli -p $AppPort shutdown && echo "中止 $AppName" || echo "$AppName 中止失敗" else echo "$AppName 未啓動" fi } # 重啓 frestart() { fpid [ -n "$AppPid" ] && fstop && sleep 1 fstart } # 終止進程 fkill() { fpid if [ -n "$AppPid" ]; then kill -9 $AppPid && echo "終止 $AppName 進程" || echo " 終止 $AppName 進程失敗" else echo "$AppName 進程未運行" fi } ScriptDir=$(cd $(dirname $0); pwd) ScriptFile=$(basename $0) case "$1" in "install" ) finstall;; "update" ) fupdate;; "reinstall" ) fremove && finstall;; "remove" ) fremove;; "backup" ) fbackup;; "start" ) fstart;; "stop" ) fstop;; "restart" ) frestart;; "status" ) fstatus;; "cpconf" ) fcpconf;; "kill" ) fkill;; * ) echo "$ScriptFile install 安裝 $AppName" echo "$ScriptFile update 更新 $AppName" echo "$ScriptFile reinstall 重裝 $AppName" echo "$ScriptFile remove 刪除 $AppName" echo "$ScriptFile backup 備份 $AppName" echo "$ScriptFile start 啓動 $AppName" echo "$ScriptFile stop 中止 $AppName" echo "$ScriptFile restart 重啓 $AppName" echo "$ScriptFile status 查詢 $AppName 狀態" echo "$ScriptFile cpconf 拷貝 $AppName 配置" echo "$ScriptFile kill 終止 $AppName 進程" ;; esac