#!/usr/bin/lua local socket = require("socket") function check_port(port) local sock = socket.tcp() sock:settimeout(1) local result, err = sock:connect("localhost", port) sock:close() if result then return true else return false end end function check_directory(path) local result = os.execute("test -d " .. path) if result == true or result == 0 then return true else return false end end local check_functions = { {name = "检查80端口", func = check_port, param = {80}}, {name = "检查4840端口", func = check_port, param = {4840}}, {name = "检查数据库目录", func = check_directory, param = {"/app/database"} } } function run_self_check() print("业务应用开始自检...") for i, check_item in ipairs(check_functions) do local result = check_item.func(unpack(check_item.param)) if not result then print(check_item.name .. ":失败") else print(check_item.name .. ":成功") end end return 0 end local exit_code = run_self_check() os.exit(exit_code)