shell脚本启动和停止jar项目

参考地址

  1. #!/bin/bash
  2. app='jfarm-provider_cn.jar'
  3. # args='-Xms2g -Xmx2g -Dspring.profiles.active=dev'
  4. cmd=$1
  5. pid=`ps -ef|grep java|grep $app|awk '{print $2}'`
  6. startup(){
  7. nohup java -jar $app > log_cn.out &
  8. # nohup java -jar $args $app &
  9. # tail -f nohup.out
  10. }
  11. if [ ! $cmd ]; then
  12. echo "Please specify args 'start|restart|stop'"
  13. exit
  14. fi
  15. if [ $cmd == 'start' ]; then
  16. if [ ! $pid ]; then
  17. startup
  18. else
  19. echo "$app is running! pid=$pid"
  20. fi
  21. fi
  22. if [ $cmd == 'restart' ]; then
  23. if [ $pid ]
  24. then
  25. echo "$pid will be killed after 3 seconds!"
  26. sleep 3
  27. kill -9 $pid
  28. fi
  29. startup
  30. fi
  31. if [ $cmd == 'stop' ]; then
  32. if [ $pid ]; then
  33. echo "$pid will be killed after 3 seconds!"
  34. sleep 3
  35. kill -9 $pid
  36. fi
  37. echo "$app is stopped"
  38. fi