{"id":590,"date":"2023-02-14T09:45:08","date_gmt":"2023-02-14T09:45:08","guid":{"rendered":"https:\/\/www.asianux.org.vn\/?p=590"},"modified":"2023-02-14T09:45:09","modified_gmt":"2023-02-14T09:45:09","slug":"viet-bash-scipt-check-logs-nodejs-realtime","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2023\/02\/14\/viet-bash-scipt-check-logs-nodejs-realtime\/","title":{"rendered":"Vi\u1ebft bash scipt check logs nodejs realtime"},"content":{"rendered":"\n<p>\u0110\u1ec3 ki\u1ec3m tra log c\u1ee7a \u1ee9ng d\u1ee5ng NodeJS trong th\u1eddi gian th\u1ef1c v\u00e0 g\u1eedi c\u1ea3nh b\u00e1o qua Telegram n\u1ebfu xu\u1ea5t hi\u1ec7n d\u00f2ng log &#8220;error&#8221;, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng script bash k\u1ebft h\u1ee3p v\u1edbi l\u1ec7nh <code>tail<\/code> v\u00e0 <code>grep<\/code>. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 m\u1ed9t v\u00ed d\u1ee5:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\r\n\r\n# Set the Telegram bot token and chat ID\r\nTELEGRAM_BOT_TOKEN=\"your_bot_token\"\r\nTELEGRAM_CHAT_ID=\"your_chat_id\"\r\n\r\n# Set the path to the log file\r\nLOG_FILE_PATH=\"\/path\/to\/your\/logfile.log\"\r\n\r\n# Set the polling interval (in seconds)\r\nPOLLING_INTERVAL=10\r\n\r\n# Run an infinite loop\r\nwhile true; do\r\n    # Get the last line of the log file that contains the word \"error\"\r\n    last_error_line=$(tail -n 1 \"$LOG_FILE_PATH\" | grep \"error\")\r\n\r\n    # Check if the last error line is not empty\r\n    if &#91; -n \"$last_error_line\" ]; then\r\n        # Send a Telegram message\r\n        message=\"ERROR: $last_error_line\"\r\n        curl -s -X POST \"https:\/\/api.telegram.org\/bot${TELEGRAM_BOT_TOKEN}\/sendMessage\" -d \"chat_id=${TELEGRAM_CHAT_ID}\" -d \"text=$message\"\r\n    fi\r\n\r\n    # Wait for the polling interval\r\n    sleep $POLLING_INTERVAL\r\ndone\r\n<\/code><\/pre>\n\n\n\n<p>Gi\u1ea3i th\u00edch c\u00e1c b\u01b0\u1edbc trong script:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Thi\u1ebft l\u1eadp m\u00e3 th\u00f4ng b\u00e1o bot Telegram v\u00e0 ID tr\u00f2 chuy\u1ec7n.<\/li>\n\n\n\n<li>Thi\u1ebft l\u1eadp \u0111\u01b0\u1eddng d\u1eabn \u0111\u1ebfn t\u1ec7p nh\u1eadt k\u00fd.<\/li>\n\n\n\n<li>Thi\u1ebft l\u1eadp kho\u1ea3ng th\u1eddi gian gi\u1eefa c\u00e1c l\u1ea7n ki\u1ec3m tra t\u1ec7p nh\u1eadt k\u00fd.<\/li>\n\n\n\n<li>Ch\u1ea1y m\u1ed9t v\u00f2ng l\u1eb7p v\u00f4 h\u1ea1n \u0111\u1ec3 ki\u1ec3m tra t\u1ec7p nh\u1eadt k\u00fd.<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng l\u1ec7nh <code>tail<\/code> \u0111\u1ec3 l\u1ea5y d\u00f2ng cu\u1ed1i c\u00f9ng c\u1ee7a t\u1ec7p nh\u1eadt k\u00fd.<\/li>\n\n\n\n<li>S\u1eed d\u1ee5ng l\u1ec7nh <code>grep<\/code> \u0111\u1ec3 ki\u1ec3m tra xem d\u00f2ng cu\u1ed1i c\u00f9ng c\u00f3 ch\u1ee9a t\u1eeb &#8220;error&#8221; kh\u00f4ng.<\/li>\n\n\n\n<li>N\u1ebfu c\u00f3 l\u1ed7i, g\u1eedi th\u00f4ng b\u00e1o qua Telegram b\u1eb1ng c\u00e1ch s\u1eed d\u1ee5ng <code>curl<\/code> \u0111\u1ec3 g\u1ecdi API Telegram.<\/li>\n\n\n\n<li>Ch\u1edd trong kho\u1ea3ng th\u1eddi gian gi\u1eefa c\u00e1c l\u1ea7n ki\u1ec3m tra t\u1ec7p nh\u1eadt k\u00fd b\u1eb1ng l\u1ec7nh <code>sleep<\/code>.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u0110\u1ec3 ki\u1ec3m tra log c\u1ee7a \u1ee9ng d\u1ee5ng NodeJS trong th\u1eddi gian th\u1ef1c v\u00e0 g\u1eedi c\u1ea3nh b\u00e1o qua Telegram n\u1ebfu xu\u1ea5t hi\u1ec7n d\u00f2ng log &#8220;error&#8221;, b\u1ea1n c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng script bash k\u1ebft h\u1ee3p v\u1edbi l\u1ec7nh&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-programing"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":1,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":591,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/590\/revisions\/591"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}