首先需要搞清楚redis在本地是怎么安装配置、然后在根据如下步骤编写bat脚本:
思路
1.下载zip格式redis
2.查看windows server服务是否已安装redis
3.启动查看服务是否正常
bat脚本
@echo off
echo windows10 x64 server redis init
REM 请求管理员权限并隐藏窗口
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 :: & echo 执行中,请稍候... & timeout 2 > nul","","runas",0)(window.close)&&exit
setlocal enabledelayedexpansionrem 设置 Redis 目录变量,方便后续引用和修改
%~d0
cd %~dp0
set "REDIS_HOME=%~dp0Redis-5.0.14.1"
set "ServiceName=REDIS5_LINSN"rem 判断服务是否存在
echo query %ServiceName% state info ...
sc query "%ServiceName%" >nul 2>&1
if %errorLevel% equ 0 (rem 服务存在,检查服务状态for /f "tokens=2 delims=: " %%A in ('sc query "%ServiceName%" ^| find "STATE"') do (set "serviceState=%%A"if /i "!serviceState!"=="RUNNING" ( echo Redis server state is RUNNING...) else if /i "!serviceState!"=="STOPPED" ( echo Redis server state is STOPPED...net start %ServiceName% ) else (echo Redis server state is !serviceState!...))
) else (rem 进入 Redis 目录cd "%REDIS_HOME%"rem 服务不存在,进行安装echo redis-server server is not exist, init server %ServiceName%"%REDIS_HOME%\redis-server.exe" --service-install "%REDIS_HOME%\redis.windows.conf" --service-name %ServiceName% --loglevel verboseecho redis-server start server %ServiceName%net start %ServiceName%
)echo windows-server-redis success
endlocalREM 执行完成后自动关闭
timeout /t 2 >nul
exit
亲测有效。