1. 一个简单的bat脚本例子,用java启动可运行jar
%~d0
cd %~dp0
java -Dname=Runtime-Name -server -Xms128M -Xmx512M -XX:MetaspaceSize=128M -XX:MaxMetaspaceSize=256M -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -jar app.jar
pause
%~d0
%~dp0 当前bat脚本所在的路径
cd 进入
2. run.bat 同级目录下 有 app1/app1.bat, app2/app2.bat
这是需要用run.bat 先后启动 app1.bat, app2.bat,两个脚本之间间隔8秒,或者任意键触发第2个脚本
%~d0
cd %~dp0
set cur_path=%~dp0
set app1_path=%cur_path%/app1
set app2_path=%cur_path%/app2
cd %app1_path%
start app1.bat
timeout /t 8
cd %app2_path%
start app2.bat
pause
3. postgresql 数据库初始化脚本
在与该脚本同级的目录下有许多schema 和 data 脚本,需要遍历并运行这些子文件夹中的SQL,先执行所有的schema SQL, 再执行所有的data SQL:
initialize-database.bat 脚本如下:
@echo off
%~d0
cd %~dp0
set host=localhost
set username=postgres
REM pgpassword 是postgrel的系統环境变量,设置后,客户端连接数据库时,优先使用此密码。
set pgpassword=123456
set port=5432
set client_encoding=utf8
echo ----start init database schema----
for /R %%s in (*database_schema.sql) do (
echo %%s
psql -h %host% -U %username% -p %port% -f %%s
)
echo ----end init database schema----
echo ----start init database data----
for /R %%s in (*database_data.sql) do (
echo %%s
psql -h %host% -U %username% -p %port% -f %%s
)
echo ----end init database data----
pause
4. 可以让手动输入,并有默认值的脚本
@echo off
REM Copyright (c) 2012-2016, EnterpriseDB Corporation. All rights reserved
REM PostgreSQL server psql runner script for Windows
:: 设置默认值
SET server=localhost
:: 手动输入其他值,如果不输入,则采用默认值
SET /P server="Server [%server%]: "
SET database=postgres
SET /P database="Database [%database%]: "
SET port=5432
SET /P port="Port [%port%]: "
SET username=postgres
SET /P username="Username [%username%]: "
for /f "delims=" %%a in ('chcp ^|find /c "932"') do @ SET CLIENTENCODING_JP=%%a
if "%CLIENTENCODING_JP%"=="1" SET PGCLIENTENCODING=SJIS
if "%CLIENTENCODING_JP%"=="1" SET /P PGCLIENTENCODING="Client Encoding [%PGCLIENTENCODING%]: "
REM Run psql
"D:\Program Files (x86)\PostgreSQL\9.5\bin\psql.exe" -h %server% -U %username% -d %database% -p %port%
pause