I have a shell script named test.sh. How can I trigger the test.sh from Ruby?
我有一个名为test.sh的shell脚本。如何从Ruby触发test.sh?
I want test.sh to run as a background process, what means in Ruby it is a ansync call.
我希望test.sh作为后台进程运行,这意味着在Ruby中它是一个ansync调用。
STDERR and STDOUT also need to be written to a specific file.
还需要将STDERR和STDOUT写入特定文件。
Any ideas?
有任何想法吗?
2 个解决方案
#1
25
@TanzeebKhalili's answer works, but you might consider Kernel.spawn(), which doesn't wait for the process to return:
@ TanzeebKhalili的答案有效,但您可能会考虑Kernel.spawn(),它不会等待进程返回:
pid = spawn("./test.sh")
Process.detach(pid)
Note that, according to the documentation, whether you use spawn()
or manually fork()
and system()
, you should grab the PID and either Process.detach()
or Process.wait()
before exiting.
请注意,根据文档,无论是使用spawn()还是手动fork()和system(),都应该在退出之前获取PID和Process.detach()或Process.wait()。
Regarding redirecting standard error and output, that's easy with spawn()
:
关于重定向标准错误和输出,使用spawn()很容易:
pid = spawn("./test.sh", :out => "test.out", :err => "test.err")
Process.detach(pid)
#2
8
Try this:
尝试这个:
Process.fork { system "./test.sh" }
Won't work on windows, for which you can use threading.
不适用于Windows,您可以使用线程。
#1
25
@TanzeebKhalili's answer works, but you might consider Kernel.spawn(), which doesn't wait for the process to return:
@ TanzeebKhalili的答案有效,但您可能会考虑Kernel.spawn(),它不会等待进程返回:
pid = spawn("./test.sh")
Process.detach(pid)
Note that, according to the documentation, whether you use spawn()
or manually fork()
and system()
, you should grab the PID and either Process.detach()
or Process.wait()
before exiting.
请注意,根据文档,无论是使用spawn()还是手动fork()和system(),都应该在退出之前获取PID和Process.detach()或Process.wait()。
Regarding redirecting standard error and output, that's easy with spawn()
:
关于重定向标准错误和输出,使用spawn()很容易:
pid = spawn("./test.sh", :out => "test.out", :err => "test.err")
Process.detach(pid)
#2
8
Try this:
尝试这个:
Process.fork { system "./test.sh" }
Won't work on windows, for which you can use threading.
不适用于Windows,您可以使用线程。