linux通过脚本运行项目,Linux 编写脚本运行项目

#!/bin/bash

#定义一些用户变量

APP_HOME=/home/work/project/sys #程序主目录

APP_NAME=project-admin.jar #程序(脚本目录)

LOG_FILE=out.log #输出日志文件

#使用说明,用来提示输入参数

usage() {

echo "Usage: sh startup.sh [start|stop|restart|status|log]"

exit 1

}

#检查程序是否在运行

is_exist(){

pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `

#如果不存在返回1,存在返回0

if [ -z "${pid}" ]; then

return 1

else

return 0

fi

}

#启动

start(){

is_exist

if [ $? -eq "0" ]; then

echo "${APP_NAME} is already running. pid=${pid} ."

else

nohup java -jar $APP_HOME/$APP_NAME >$APP_HOME/$LOG_FILE 2>&1 &

tail -f $APP_HOME/$LOG_FILE

echo "${APP_NAME} start success"

fi

}

#停止

stop(){

is_exist

if [ $? -eq "0" ]; then

kill -9 $pid

else

echo "${APP_NAME} is not running"

fi

}

#打印日志

log(){

is_exist

if [ $? -eq "0" ]; then

tail -f $APP_HOME/$LOG_FILE

else

echo "${APP_NAME} is not running"

fi

}

#输出运行状态

status(){

is_exist

if [ $? -eq "0" ]; then

echo "${APP_NAME} is running. Pid is ${pid}"

else

echo "${APP_NAME} is NOT running."

fi

}

#重启

restart(){

stop

start

}

#根据输入参数,选择执行对应方法,不输入则显示使用说明

case "$1" in

"start")

start

;;

"stop")

stop

;;

"status")

status

;;

"restart")

restart

;;

"log")

log

;;

*)

usage

;;

esac

第二种方式:

tomcat_home=/opt/usr/local/tomcat-8.5.57

SHUTDOWN=$tomcat_home/bin/shutdown.sh

STARTTOMCAT=$tomcat_home/bin/startup.sh

echo $2 $1 $tomcat_home

#pidlist1=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'

#echo $pidlis1t1

case $1 in

start)

echo "start $tomcat_home"

$STARTTOMCAT

tail -f $tomcat_home/logs/catalina.out

;;

stop)

echo "stop $tomcat_home"

$SHUTDOWN

pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'`

kill -9 $pidlist

#删除日志文件,如果你不先删除可以不要下面一行

rm $tomcat_home/logs/* -rf

#删除tomcat的临时目录

rm $tomcat_home/work/* -rf

;;

restart)

echo "stop $tomcat_home"

$SHUTDOWN

pidlist=`ps -ef |grep tomcat |grep -v "grep"|awk '{print $2}'`

kill -9 $pidlist

#删除日志文件,如果你不先删除可以不要下面一行

rm $tomcat_home/logs/* -rf

#删除tomcat的临时目录

rm $tomcat_home/work/* -rf

sleep 5

echo "start $tomcat_home"

$STARTTOMCAT

#看启动日志

tail -f $tomcat_home/logs/catalina.out

;;

logs)

cd $tomcat_home/logs

tail -f catalina.out

;;

执行出现:-bash: ./startup.sh: Permission denied

执行命令:chmod u+x *.sh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值