Lua:如何检查进程是否正在运行
2015-6-4 14:23:56
收藏:0
阅读:312
评论:4
我想从 Lua 开始一个进程,并监控这个进程是否仍在运行。
[编辑] 我知道可以通过 os:execute 来启动该进程,但这是阻塞的。我想找到一种非阻塞的方式来启动进程并监控它是否仍在运行。
点赞
用户2704138
Lua中的协程允许您执行所需操作(自Lua 5.0以来):
local function Body()
print("第一次恢复")
coroutine.yield()
print("第二次恢复")
coroutine.yield()
print("最终恢复")
end
local co = coroutine.create(Body)
print(type(co)) -- 输出 "thread"
coroutine.resume(co) -- 输出 "第一次恢复"
print("第一个yield后")
coroutine.resume(co) -- 输出 "第二次恢复"
print("第二个yield后")
coroutine.resume(co) -- 输出 "最终恢复"
print(coroutine.status(co)) -- 输出 "suspended", "dead"
2015-06-05 16:49:06
用户134702
如果你使用的是 luajit,那么你可以使用外部函数接口直接调用操作系统 API 函数,例如 Linux 或类似系统。
local ffi = require("ffi")
local C = ffi.C
ffi.cdef[[
int fork(void);
int execlp(const char* file, const char *arg, ...);
int waitpid(int pid, int *status, int options);
void _exit(int status);
unsigned int sleep(unsigned int seconds);
]]
local pid = C.fork()
if pid > 0 then -- parent
print("Waiting for child process:");
local status = ffi.new('int[1]')
local WNOHANG = 1
local done = false
while not done do
local id = C.waitpid(-1, status, WNOHANG)
if id == pid then
done = true
elseif pid < 0 then
print("error occurred")
os.exit(-1)
else
print("status=",status[0])
C.sleep(1)
-- do other stuff. We aren't waiting
end
end
print("Child exited with status=", status[0])
elseif pid == 0 then -- child
print("Starting child")
C.execlp('sleep', 'sleep', '5')
C._exit(-1) -- exec never returns
elseif pid < 0 then -- failure
print("failed to fork")
end
你会发现使用 WNOHANG = 1 时,你仍然可以获得结果来判断子进程是否已经退出然后继续做其他事情。
2016-07-08 02:02:12
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
一种简单粗暴的方式是直接使用
[io.popen()](http://www.lua.org/manual/5.1/manual.html#pdf-io.popen)并监控它的输出,或者使用类似[ps](http://linuxcommand.org/man_pages/ps1.html)这样的其他Linux工具处理进程。另一种方式是使用一些Lua POSIX绑定,例如
[POSIX.signal](https://luaposix.github.io/luaposix/modules/posix.signal.html)和[POSIX.unistd#fork()](https://luaposix.github.io/luaposix/modules/posix.unistd.html)。