gpt4 book ai didi

ruby - 如何运行显示一些信息的 ruby​​ shell 脚本,然后要求输入并进入后台?

转载 作者:行者123 更新时间:2023-12-04 18:51:51 26 4
gpt4 key购买 nike

我有一个要在后台运行的 ruby​​ shell 脚本。
但是,在进入后台之前,我需要询问用户输入。

可能吗?如何实现?

提前致谢!

最佳答案

这是一个简单的例子:

def do_stuff(input)
puts "Input received: #{input}"
sleep 10
end

print "Enter input value: "
input = gets
pid = fork { do_stuff(input) }
puts "test.rb started, pid: #{pid}"
Process.detach pid

注意 fork 的使用开始一个新进程, detach允许子进程在主进程退出时继续运行。

某些平台 (Windows) 不支持 fork .

关于ruby - 如何运行显示一些信息的 ruby​​ shell 脚本,然后要求输入并进入后台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31033137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com