1 登录linux 后 编辑.bashrc
export PATH=$PATH:/mnt/spoon
/mnt/spoon 为使用到的应用的环境,下面包含.sh等文件
2 设置要运行sh文件的路径
echo 获取当前sh文件的父级目录
basepath=$(cd `dirname $0`; pwd)
echo $basepath
kjbpath=$(cd `dirname $0`; pwd)/Templates/programsSqlServer/syncBocDataToMysql.kjb
echo $kjbpath
logpath=$(cd `dirname $0`; pwd)/logs/syncBocDataToMysql`date '+%Y%m%d%H%M%S'`.log
echo $logpath
$basepath/kitchen.sh -file:$kjbpath -level:Basic >$logpath
3 给该sh文件设置可运行权限
chmod 777 *.sh
4 如果生成的日志文件有? 是因为linux和windows环境的问题
直接在liunx服务器上编辑创建该sh文件并记得授权
basepath=$(cd `dirname $0`; pwd) 解释
dirname $0,取得当前执行的脚本文件的父目录
cd `dirname $0`,进入这个目录(切换当前工作目录)
pwd,显示当前工作目录(cd执行后的)
basepath=$(cd `dirname $0`; pwd)
echo $basepath
kjbpath=$(cd `dirname $0`; pwd)/Templates/programsSqlServer/syncBocDataToMysql.kjb
echo $kjbpath
logpath=$(cd `dirname $0`; pwd)/logs/syncBocDataToMysql`date '+%Y%m%d%H%M%S'`.log
echo $logpath
$basepath/kitchen.sh -file:$kjbpath -level:Basic > $logpath
在spring定时任务中sh里面的路径需要写全路径,且代码如下
try {
ProcessBuilder builder = new ProcessBuilder(new String[]{path});
pcs = builder.start();
String line = null;
buf = new BufferedReader(new InputStreamReader(pcs.getInputStream()));
while ((line = buf.readLine()) != null) {
logger.info(line);
}
stdError = new BufferedReader(new InputStreamReader(pcs.getErrorStream()));
while ((line = stdError.readLine()) != null) {
logger.error(line);
}
try {
runningStatus = pcs.waitFor();
} catch (InterruptedException e) {
runningStatus = 1;
logger.error("等待shell脚本执行状态时,报错...", e);
}
if(runningStatus == 0){//成功
logger.info("本次shell脚本执行成功");
}else{
logger.info("本次shell脚本执行失败");
}
} catch (Exception e) {
logger.error("执行shell脚本执行报错...", e);
}finally{
try {
if(buf!=null){
buf.close();
}
} catch (Exception e) {
logger.error("关闭pcs getInputStream失败", e);
}
try {
if(stdError!=null){
stdError.close();
}
} catch (Exception e) {
logger.error("关闭pcs getErrorStream失败", e);
}
try {
if(pcs!=null){
pcs.destroy();
logger.info("关闭pcs成功");
}
} catch (Exception e) {
logger.error("关闭pcs失败", e);
}
}