编程知识 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://cdmana.com/2021/11/20211125173733557Y.html

Scroll to Top