如何評估來自 bash/shell 腳本的 http 響應代碼? - How to evaluate http response codes from bash/shell script?

問題:

I have the feeling that I'm missing the obvious, but have not succeeded with man [curl|wget] or google ("http" makes such a bad search term).我有一種感覺,我錯過了明顯的東西,但沒有成功使用man [curl|wget]或 google(“http”使搜索詞變得如此糟糕)。 I'm looking for a quick&dirty fix to one of our webservers that frequently fails, returning status code 500 with an error message.我正在尋找對我們的一個經常失敗的網絡服務器的快速和骯髒的修復,返回狀態代碼 500 並顯示錯誤消息。 Once this happens, it needs to be restarted.一旦發生這種情況,它需要重新啓動。

As the root cause seems to be hard to find, we're aiming for a quick fix, hoping that it will be enough to bridge the time until we can really fix it (the service doesn't need high availability)由於根本原因似乎很難找到,我們的目標是快速修復,希望這足以縮短我們真正修復它的時間(服務不需要高可用性)

The proposed solution is to create a cron job that runs every 5 minutes, checking http://localhost:8080/ .建議的解決方案是創建一個每 5 分鐘運行一次的 cron 作業,檢查http://localhost:8080/ If this returns with status code 500, the webserver will be restarted.如果返回狀態碼 500,則網絡服務器將重新啓動。 The server will restart in under a minute, so there's no need to check for restarts already running.服務器將在一分鐘內重新啓動,因此無需檢查已在運行的重新啓動。

The server in question is a ubuntu 8.04 minimal installation with just enough packages installed to run what it currently needs.有問題的服務器是 ubuntu 8.04 最小安裝,只安裝了足夠的軟件包來運行它當前需要的東西。 There is no hard requirement to do the task in bash, but I'd like it to run in such a minimal environment without installing any more interpreters.在 bash 中執行任務沒有硬性要求,但我希望它在這樣一個最小的環境中運行,而無需安裝更多的解釋器。

(I'm sufficiently familiar with scripting that the command/options to assign the http status code to an environment variable would be enough - this is what I've looked for and could not find.) (我對腳本非常熟悉,將 http 狀態代碼分配給環境變量的命令/選項就足夠了 - 這是我一直在尋找但找不到的東西。)


解決方案:

參考一: https://en.stackoom.com/question/9JbJ
參考二: https://stackoom.com/question/9JbJ
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章