Ruby 命令行:如何通过命令行中的文本发送 CTRL-C 命令?
我正在尝试创建一个简单的 Automator Droplet,它将把 style.less
文件放入其中,并在其上运行以下 LESS 命令:
$ LESSC {DROPPED_FILE} --watch
它将监视我放入的文件是否有任何更改,然后自动更新输出的 style.css
仅供参考:我正在使用 LESS 来动态编写 CSS 文件。更多信息请参见此处。
基本的水滴效果很好。
- 删除的文件被传递到一个变量中;为此目的:
{MY_VAR}
。 - 我在
/usr/bin/ruby
shell 中运行一个 shell 脚本,如下system("lessc {MY_VAR} --watch &")
这很好用,但是我想要--watch 在退出 automator 应用程序时停止。
LESS 文档说在命令行 shell 中按 CTRL-C 快捷键可以中止脚本。
但由于我不在终端窗口内(命令在后台传递),我不知道如何中止脚本。
即使在 automator.app 关闭后,仍会监视 style.less
文件的更改,并且仍会重写相应生成的 style.css
。
所以基本上我需要在 .app 退出时传递 abort 命令。
我生成了一个简单的弹出窗口,单击该窗口将在将另一个命令传递到终端 shell 后关闭应用程序。
这是我所有尝试都未能成功阻止脚本的部分。
是否有一个命令行功能与按 CTRL-C 命令的作用相同? 我如何将最好的传递给 shell?
I am trying to create a simple Automator droplet that will take the style.less
file dropped into it and run the following LESS command on it:
$ LESSC {DROPPED_FILE} --watch
which will monitor the file I have dropped in for any changes and will then automatically update the outputted style.css
FYI: I am using LESS to have dynamically written CSS files. More info is here.
The basic droplet works great.
- The dropped file is passed into a variable; For this purpose:
{MY_VAR}
. - I run a shell script in the
/usr/bin/ruby
shell as followssystem("lessc {MY_VAR} --watch &")
this works great however I want the --watch to be stopped upon quitting the automator app.
The LESS documentation says to press the CTRL-C shortcut while in the command line shell to abort the script.
But since I am not inside the terminal window (command is being passed in the background) I don't know how to abort the script.
Even after the automator.app has been closed the style.less
file is still being watched for changes and the correspondingly generated style.css
is still being rewritten.
So basically I need to pass the abort command on exit of the .app.
I have generated a simple pop up that on click will close the app after passing another command to the terminal shell.
This is the part where all my tries have been unsuccessful to stop the script.
Is there a command line function that acts the same as pressing the CTRL-C command?
How would I pass this the best to the shell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在控制台中按 CTRL-C,则不会通过 STDIN 发送到您的程序。 Bash(或任何您使用的)将 CTRL-C 视为特殊的。 bash 向进程发送信号。如果是 CTRL-C,则为 SIGINT。要向程序发送信号,您需要知道它的 pid。然后就可以向pid发送信号了。为了能够获取 pid,您可以使用 ruby 启动该过程。
在 Ruby 中,至少有 3 种不同的方式来执行某些操作。
您使用了
system("lessc #{file} --watch&")
,它也返回 imidietly 但始终返回 0。If you press CTRL-C in your console, this is not send via STDIN to your programm. Bash(or whatever you use) treats CTRL-C special. The bash sends a signal to the process. In case of CTRL-C this is SIGINT. To send a signal to a program you need to no it's pid. Then you can send the signal to the pid. To be able to get the pid you can start the process with ruby.
In ruby there are at least 3 different ways, of executing something.
You used
system("lessc #{file} --watch&")
which also returns imidietly but always returns 0.提示:^C Control-C 在 shell 中到底做什么?
(它不发送字符)。
提示说明:
此 PDF 讨论了信号。请注意其中还谈到了 ^C。您可以根据需要手动发送信号。
当您在终端中按 ^C 时,它通常会向正在运行的程序发送 SIGINT 信号。您也可以在 Ruby 中发送这些信号。有关在 Ruby 中使用进程(和发送信号)的更多信息:http://whynotwiki.com/Ruby_/_Process_management< /a>.
快乐编码。
Hint: What does ^C Control-C really do in a shell?
(It doesn't send a character).
Clarification on hint:
This PDF talks about signals. Note where it also talks about ^C. Your can manually send a signal as required.
When you press ^C in a terminal it normally sends the SIGINT signal to the program that is running. You can send these signals in Ruby as well. More information about working with processes (and sending signals) in Ruby: http://whynotwiki.com/Ruby_/_Process_management.
Happy coding.
我认为 less 当它收到“q”字符时也会终止。
less also terminates when it receives the "q" character, I think.