jar_name=xxx.jarlog_path=xx/log/xx.logjar_path=xxx/xxxstop(){echo""pid=$(ps-ef|grep$jar_name|grep-vgrep|awk'{print $2}')if[-z$pid];then# -z 是判断是否为空echo"${jar_name}未运行!"elseecho"准备关闭进程$pid"kill-9$pidsleep0.5echo"${jar_name}已关闭!"fiecho""}start(){echo""pid=$(ps-ef|grep$jar_name|grep-vgrep|awk'{print $2}')if[-z$pid];thencd$jar_path# 这里转到jar包目录执行命令,是为了使用jar_path下的config文件,貌似java程序只能识别当前执行命令目录下的配置,否则就是使用已打入Jar包的配置文件。nohupjava-jar$jar_name>$log_path2>&1&# 'nohup'代表命令后台执行;#'>$log_path 2>&1' 代表将标准输出和错误输出同时重定向到同一位置,这里也可以用'&> $log_path';#最后的'&'代表shell关闭后,程序不关闭pid=$(ps-ef|grep$jar_name|grep-vgrep|awk'{print $2}')echo"应用${jar_name}启动成功,pid=${pid}"# tail -f $log_pathelseecho"应用${jar_name}正在运行,pid =${pid}"fiecho""}status(){echo""pid=$(ps-ef|grep$jar_name|grep-vgrep|awk'{print $2}')if[-z$pid];thenecho"应用${jar_name}未运行"elseecho"应用${jar_name}正在运行,pid =${pid}"fiecho""}restart(){stopsleep2echo"停止完成,准备启动jar包"start}action(){echo"请输入数字选择要执行的操作:1=启动,2=重启,3=停止,4=查看运行状态,5=退出"echo'你输入的数字为:'readacase$ain"1")start;;"2")restart;;"3")stop;;"4")status;;"5")exit1;;*)# *代表所有echo"输入错误,请重新输入"action;;esac}action启动jar包的shell脚本(包括启动|重启|停止|状态)
RELATED READING
延伸阅读
更多一线实战笔记与深度复盘,助您持续精进