applescript实用程序问题/bash/进程检测
我制作了一个小型自动化应用程序来帮助我启动和结束 apache(另一个用于 mysql),这样我就不必去终端并执行此操作(是的,我就是那么懒)。
现在这是我使用的 applescript:
on run {input, parameters}
set apache2state to (do shell script "/bin/ps -arx |/usr/bin/grep apache2 |wc -l")
repeat until apache2state does not start with " "
set apache2state to text 2 thru -1 of apache2state
end repeat
if apache2state is equal to "3" then
do shell script "/opt/local/bin/port load apache2" with administrator privileges
else
do shell script "/opt/local/bin/port unload apache2" with administrator privileges
end if
end run
现在这是有效的,除了我实际上是在与相对于 apache 正在运行的进程数相关的整数值进行比较..所以它并不真正可靠。
有没有更好的方法来测试 apache (和 mysql,我有另一个相同的脚本)是否正在运行。问题是,当它们关闭时,shell 命令将返回一个相同的整数值(而不仅仅是 grep 进程的 1..)
感谢
编辑指定此处的解决方案效果不佳,因为 grep 将返回任何实例apache 线程和任何其他表示 apache 的进程(例如 tail -r /var/log/apache2/error.log )。
I made a small automator app to help me launch and end apache ( another for mysql), so that I don't have to go to the terminal and do it ( yes I'm that lazy).
now here is the applescript I use for it:
on run {input, parameters}
set apache2state to (do shell script "/bin/ps -arx |/usr/bin/grep apache2 |wc -l")
repeat until apache2state does not start with " "
set apache2state to text 2 thru -1 of apache2state
end repeat
if apache2state is equal to "3" then
do shell script "/opt/local/bin/port load apache2" with administrator privileges
else
do shell script "/opt/local/bin/port unload apache2" with administrator privileges
end if
end run
Now this works, except for the fact that I'm actually comparing to integer values that are relative to the number of processes that apache is running.. so it's not really reliable.
Is there a better way to test if apache (and mysql, I have another script just the same) is running. Problem is that when they are shut off the shell command will return an integer value just the same ( and not just a 1 for the grep process..)
thanks
EDITED to specify thatthe solution up here worked badly because the grep would return any instance of the apache threads and any other process that said apache ( like tail -r /var/log/apache2/error.log for instance).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以使用以下命令测试服务的运行状态:
但是,这可能不适用于随 MacPorts 安装的服务。
另请参阅此问题的答案。
由于命令
service
在 Snow Leopard 下已被弃用,因此您也可以使用 launchctl 获取服务的运行状态。可以通过以下方式从 AppleScript 调用该命令:The running state of a service can be tested with the following command:
This however might not work for services installed with MacPorts.
Also see the answer to the this question.
Since the command
service
is deprecated under Snow Leopard, you can alternatively use launchctl to obtain the running state of a service. The command can be invoked from AppleScript in the following way: