编程知识 cdmana.com

编写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

Scroll to Top