编写Java程序启动脚本最佳实践

InfoQ 2021-11-25 17:37:57
java 程序 启动 编写 动脚

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在实际的工作中经常会遇到给java项目编写启动脚本,现在就来记录一下编写此类脚本的最佳实践。先上一个模板:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"#!/bin/bash\n\n# 参数校验如果校验失败输出脚本使用方法\n\nbin=`dirname \"${BASH_SOURCE-$0}\"`\nscript_prefix=`cd \"$bin\"; pwd`\n\n# 或 nohup java {虚拟机参数} -jar {jar包路径} {参数} > \"${script_prefix}\"/start.out 2>&1 &\nnohup java {虚拟机参数} -cp {classpath} {主类} {参数} > \"${script_prefix}\"/start.out 2>&1 &","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"参数校验的最佳实践还没有仔细研究,有了一些实践后再做补充。下面解释一下脚本中的一些关键点。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"nohup是不挂断的意思( no hang up),用它启动进程会忽略所有的挂(SIGHUP)信号,比如用户退出终端进程不会结束","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"命令最后面的&,表示后台运行,nohup和&结合使用就是让进程在后台运行且不挂断。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从上面模板可以看到启动进程时把标准输出重定向到文件\"${script_prefix}\"/start.out,那么2>&1又是什么呢?这里解释一下,在shell中标准输入和输出是这样表示的:0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) 。2>&1就是把stderr重定向到stdout,即stdout和stderr输出到同一个地方。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"另外在上面模板中start.out所在目录是脚本所在的上下文环境,它是通过第5、6行代码生成的,${BASH_SOURCE-$0}变量表示脚本调用的路径,dirname \"${BASH_SOURCE-$0}\"即脚本所在目录,然后通过第6行代码生成脚本所在目录的全路径。下面我们给出第5、6行代码的测试例子。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有一个/home/test/目录下的脚本test.sh:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"#!/bin/bash\n\necho \"${BASH_SOURCE-$0}\"\ncurr=`dirname \"${BASH_SOURCE-$0}\"`\necho \"${curr}\"\ncurr_dir=`cd \"$curr\"; pwd`\necho \"$curr_dir\"","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果执行脚本:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"cd /home/test/ && ./test.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" 输出如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"./test.sh\n.\n/home/test","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果执行脚本:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"shell"},"content":[{"type":"text","text":"/home/test/test.sh","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"输出如下:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"/home/test/test.sh\n/home/test\n/home/test","attrs":{}}]},{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"你会发现调用脚本的路径不同,${BASH_SOURCE-$0}变量的值也不同,但最终输出的全路径是一致的,所以可以放心使用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"参考:1.","attrs":{}},{"type":"link","attrs":{"href":"https://www.jianshu.com/p/93a45927f013","title":"","type":null},"content":[{"type":"text","text":"https://www.jianshu.com/p/93a45927f013","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"2.","attrs":{}},{"type":"link","attrs":{"href":"https://www.cnblogs.com/greencollar/p/13668464.html#:~:text=Linux%20nohup%20%E5%AE%9E%E7%8E%B0%E5%91%BD%E4%BB%A4%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C%E5%B9%B6%E8%BE%93%E5%87%BA%E6%88%96%E8%AE%B0%E5%BD%95%E5%88%B0%E6%8C%87%E5%AE%9A%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6.%20%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C%E5%91%BD%E4%BB%A4%EF%BC%9Anohup.%20nohup%E5%91%BD%E4%BB%A4%EF%BC%9Anohup%E6%98%AF%E4%B8%8D%E6%8C%82%E6%96%AD%E7%9A%84%E6%84%8F%E6%80%9D%20%28%20no%20hang,%E8%AF%A5%E5%91%BD%E4%BB%A4%E5%BF%BD%E7%95%A5%E6%89%80%E6%9C%89%E6%8C%82%EF%BC%88SIGHUP%EF%BC%89%E4%BF%A1%E5%8F%B7%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%9C%A8%E4%BD%A0%E9%80%80%E5%87%BA%E5%B8%90%E6%88%B7%2F%E5%85%B3%E9%97%AD%E7%BB%88%E7%AB%AF%E4%B9%8B%E5%90%8E%E7%BB%A7%E7%BB%AD%E8%BF%90%E8%A1%8C%E7%9B%B8%E5%BA%94%E7%9A%84%E8%BF%9B%E7%A8%8B%E3%80%82.%20%E8%AF%A5%E5%91%BD%E4%BB%A4%E7%9A%84%E4%B8%80%E8%88%AC%E6%A0%BC%E5%BC%8F%E4%B8%BA%EF%BC%9A.%20nohup%20yourcommand%20%26.%20%23%20yourcommand%3A%E5%90%AF%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%91%BD%E4%BB%A4%E3%80%82.%20%E5%8F%AF%E4%BB%A5%E8%B7%9F%E8%AF%A5%E5%91%BD%E4%BB%A4%E9%9C%80%E8%A6%81%E7%9A%84%E5%90%84%E7%A7%8D%E5%8F%82%E6%95%B0%E3%80%82.","title":"","type":null},"content":[{"type":"text","text":"https://www.cnblogs.com/greencollar/p/13668464.html#:~:text=Linux%20nohup%20%E5%AE%9E%E7%8E%B0%E5%91%BD%E4%BB%A4%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C%E5%B9%B6%E8%BE%93%E5%87%BA%E6%88%96%E8%AE%B0%E5%BD%95%E5%88%B0%E6%8C%87%E5%AE%9A%E6%97%A5%E5%BF%97%E6%96%87%E4%BB%B6.%20%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C%E5%91%BD%E4%BB%A4%EF%BC%9Anohup.%20nohup%E5%91%BD%E4%BB%A4%EF%BC%9Anohup%E6%98%AF%E4%B8%8D%E6%8C%82%E6%96%AD%E7%9A%84%E6%84%8F%E6%80%9D%20%28%20no%20hang,%E8%AF%A5%E5%91%BD%E4%BB%A4%E5%BF%BD%E7%95%A5%E6%89%80%E6%9C%89%E6%8C%82%EF%BC%88SIGHUP%EF%BC%89%E4%BF%A1%E5%8F%B7%EF%BC%8C%E5%8F%AF%E4%BB%A5%E5%9C%A8%E4%BD%A0%E9%80%80%E5%87%BA%E5%B8%90%E6%88%B7%2F%E5%85%B3%E9%97%AD%E7%BB%88%E7%AB%AF%E4%B9%8B%E5%90%8E%E7%BB%A7%E7%BB%AD%E8%BF%90%E8%A1%8C%E7%9B%B8%E5%BA%94%E7%9A%84%E8%BF%9B%E7%A8%8B%E3%80%82.%20%E8%AF%A5%E5%91%BD%E4%BB%A4%E7%9A%84%E4%B8%80%E8%88%AC%E6%A0%BC%E5%BC%8F%E4%B8%BA%EF%BC%9A.%20nohup%20yourcommand%20%26.%20%23%20yourcommand%3A%E5%90%AF%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%91%BD%E4%BB%A4%E3%80%82.%20%E5%8F%AF%E4%BB%A5%E8%B7%9F%E8%AF%A5%E5%91%BD%E4%BB%A4%E9%9C%80%E8%A6%81%E7%9A%84%E5%90%84%E7%A7%8D%E5%8F%82%E6%95%B0%E3%80%82.","attrs":{}}]}]}]}
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://xie.infoq.cn/article/e84632ca8333981914d9f9161

  1. http://lx.gongxuanwang.com/sszt/32.htm
  2. 回顾我两个月面试阿里,携程,小红书,美团,网易等等(Java岗)
  3. JavaScript高级程序设计读后感(一)之零碎知识点查漏补缺
  4. http://lx.gongxuanwang.com/sszt/7.htm
  5. #yyds干货盘点#设计模式之【工厂模式】
  6. Java * SpringBoot实现万能文件在线预览,已开源,真香
  7. Redis | 第4章 Redis中的数据库《Redis设计与实现》
  8. Redis | 第4章 Redis中的数据库《Redis设计与实现》
  9. 关于centos docker版本过低导致 is not a valid repository/tag: invalid reference format
  10. Redis 源码简洁剖析 02 - SDS 字符串
  11. 回顧我兩個月面試阿裏,攜程,小紅書,美團,網易等等(Java崗)
  12. Rétrospectivement, j'ai passé deux mois à interviewer Ali, ctrip, Little Red Book, meituan, NetEase, etc. (Java post)
  13. Docker + webhook Automation Deployment Front End Project
  14. Java技术之Spring、Hibernate框架整合方法
  15. http://lx.gongxuanwang.com/sszt/32.htm
  16. 亚马逊自己的 Linux 发行版现在完全基于 Fedora 了
  17. Redis 源码简洁剖析 02 - SDS 字符串
  18. Java技術之Spring、Hibernate框架整合方法
  19. Méthode d'intégration des cadres de printemps et d'hibernation de la technologie Java
  20. Redis source Concise Analysis 02 - SDS String
  21. La distribution Linux d'Amazon est maintenant entièrement basée sur Fedora
  22. org.springframework.web.bind.MissingServletRequestParameterException
  23. Built in constraints and functions of MySQL Foundation (2)
  24. Basic operation of MySQL Foundation (I)
  25. Introduction to Java zero foundation 3: Java data types
  26. 从零开始搭建EasyDarwin环境——Linux系统开发环境搭建Golang
  27. Redis source Concise Analysis 02 - SDS String
  28. Construire l'environnement easydarwin à partir de zéro - - construire l'environnement de développement du système Linux golang
  29. javaweb代码是正确的,但是第一行代码就报错了
  30. **** | Java | 后端开挂:3行代码写出8个接口
  31. Java || 看了大二学长写的代码,我竟开始默默的模仿了。。。
  32. Java | 手把手教你实现一个抽奖系统(Java版)
  33. Java | Manuel pour vous apprendre à mettre en œuvre un système de loterie (version Java)
  34. Java | | après avoir lu le Code que j'ai écrit en deuxième année, j'ai commencé à imiter silencieusement...
  35. Java | back - end Pending: 3 - line Code write 8 Interfaces
  36. Le Code Web Java est correct, mais la première ligne de code est incorrecte
  37. Android网络编程之Http通信
  38. Android網絡編程之Http通信
  39. Http communication for Android Network Programming
  40. 数据结构实验八 领会图的两种主要储存结构和图的基本运算算法设计
  41. Hibernate数据校验简介
  42. The story of spring
  43. Il a dépensé 270 000 yuans pour soulever Xiaopeng p7 et a parcouru 3 627 km. Le propriétaire du véhicule a partagé 6 avantages et inconvénients.
  44. 阿里蚂蚁花呗团队面试题:spring+分布式+jvm+session+redis
  45. 【Java入门100例】14.字符串排序——compareTo()
  46. 【Java入门100例】13.修改文件扩展名——字符串替换
  47. Leetcode 79. Word Search [C + + / java detailed problem]
  48. Introduction à la vérification des données hibernantes
  49. Expérience de la structure des données
  50. Spring cloud gateway practice 2: more routing configuration methods
  51. Java network programming - summary overview
  52. 基于语法树的 Java 代码自动化插桩
  53. 云原生 Spring Boot 应用配置 Prometheus + Grafana 监控(保姆级)
  54. Spring cloud gateway practice 2: more routing configuration methods
  55. Jenkins file one line of code to deploy. Net program to k8s
  56. Java network programming - summary overview
  57. Cloud Native Spring Boot application configuration Prometheus + grafana Monitoring (baby - sitter)
  58. Insertion automatique de code Java basée sur l'Arbre syntaxique
  59. Le SUV phare de Xiaopeng, Xiaopeng G9, a fait ses débuts au salon de l'automobile et s'est tenu en position C dans la nouvelle force?
  60. Docker 从入门到实践系列四 - Docker 容器编排利器 Docker Compose