如何使用 os.run 启动一个守护进程?
#4111
-
尝试 os.run("nohup server >log 2>&1 &") 发现会卡住。 需求是这样的。使用 xmake run target 的时候,在 target:before_run 中想启动一个跑在后台的 server,然后在 after_run 再 kill 掉 但是发现会在前台卡住。 target("test_session_ut")
set_kind("binary")
add_files("test_session_*.cc")
add_deps("session")
before_run(function (target)
print(os.workingdir()) -- 这里发现 workingdir 也没生效
local config = path.join("$(projectdir)", "src/test/nvmf.json")
os.exec("nohup nvmf_tgt -m 0x2 --json %s >nvmf_tgt.log 2>&1 &", config) -- 这一行会卡住,并不会在后台跑
os.run("sleep 3")
end)
after_run(function (target)
print("after run")
os.exec("kill -9 $(pidof nvmf_tgt)")
end) |
Beta Was this translation helpful? Give feedback.
Answered by
waruqi
Aug 23, 2023
Replies: 2 comments 3 replies
-
不是这么用的,它不是 shell ,仔细看文档 https://xmake.io/#/zh-cn/manual/builtin_modules?id=osexecv |
Beta Was this translation helpful? Give feedback.
0 replies
-
找到个方案了。os.execv 的 opt 参数,设置 {detach = true} os.execv("nvmf_tgt", {"-m", "0x10", "-r", "/var/tmp/spdk_test.sock", "--json", config}, {detach = true, stdout = "nvmf.log", stderr = "nvmf.log"}) |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你改的是当前运行目录,target:rundir() ,会自动传入内置的 os.execv("", {}, {curdir = rundir})
不会污染整个脚本的运行目录,你自己的脚本里面。自己处理