如何在 Perl 中运行脚本而不等待它?
我在 Perl 中有一个系统调用,如下所示:
system('/path/to/utility >> /redirect/to/log file');
但这会等待 utility
完成。我只想触发此操作并让 Perl 脚本完成,无论 utility
调用是否完成。
我该如何执行此操作?
我尝试将行更改为
system('/path/to/utility >> /redirect/to/log file &');
,但此语法仍在 Windows 上等待调用完成。我需要让它在 Linux 和 Windows 上都能工作。
I have a system call in Perl that looks like this:
system('/path/to/utility >> /redirect/to/log file');
But this waits for utility
to complete. I just want to trigger this and let the Perl script finish irrespective of whether the utility
call finished or not.
How can I do this?
I tried changing the line to
system('/path/to/utility >> /redirect/to/log file &');
but this syntax still waits for the call to finish on Windows. I need to make it work on Linux as well as Windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
或者
or
您可以尝试查看
fork
关键字,并从分叉进程启动系统命令。有关示例,请参阅perlipc
联机帮助页。You could try looking at the
fork
keyword, and launch your system command from the forked process. See theperlipc
manpage for examples.这个常见任务已被抽象到 CPAN 模块
Proc::Background
中This common task has been abstracted into the CPAN module
Proc::Background
在类 POSIX 系统上最简单的方法实际上是您已经尝试过的方法:
在 Windows 上获取在后台执行的工具的最简单方法是使用
start.exe
作为启动帮助程序:但是,我不知道以这种方式将输出重定向到日志文件是否有效,而且目前我没有 Windows 系统来测试它。
是的,这意味着您将需要一个代码分支,具体取决于当前系统,但其他解决方案可能也需要一个。如果您的 Perl 有
fork()
模拟,您实际上可以在两个系统上使用fork()
(这有点复杂,因为您无法将 stdout 重定向到日志文件很简单,您首先必须在 Perl 中打开它,并在调用新进程之前将其设置为分叉子进程的标准输出)。如果您的 Windows Perl 没有fork()
模拟,您还需要一个代码分支,因为在这种情况下您只能在 UNIX 上使用fork()
并且您需要在 Windows 上将Win32::Process::Create
与DETACHED_PROCESS
选项结合使用。但也许您可以先让我们知道使用 start 是否已经适合您。如果没有,可能
start.exe
不处理斜杠。在这种情况下,您可能必须使用类似的东西(双反斜杠很重要!单个反斜杠在字符串文字中具有特殊含义;它是转义字符)。
要了解您是否在 Windows 上运行,请查看变量
$OSNAME
或$^OS
,在 Windows 上它应该显示类似“MSWin32”的内容。The easiest way on POSIX-like systems is in fact the one you already tried:
The easiest way to get a tool to execute in background on Windows is to use
start.exe
as a launch helper:However, I don't know if it works to redirect the output to log file that way and I have no Windows system around this very moment for testing it.
Yes, that means you would need a code branch depending on current system, but other solutions may need one, too. If your Perl has
fork()
emulation, you can in fact usefork()
on both systems (this is a bit more complicated, since you cannot redirect stdout to a logfile that easy, you first have to open it in Perl and make it stdout of the forked child before calling the new process). If your Windows Perl has nofork()
emulation, you also need a code branch, since in that case you can usefork()
only on UNIX and you'll need to useWin32::Process::Create
with theDETACHED_PROCESS
option on Windows.But maybe you can first let us know if using start is already working for you. If it does not, maybe
start.exe
doesn't handle slashes. In that case you may have to use something likeinstead (double backslash is important! A single backslash has a special meaning in string literals; it's the escape character).
To find out if you are running on Windows or not, take a look at the variable
$OSNAME
or$^OS
, it should say something like "MSWin32" on Windows.