applescript实用程序问题/bash/进程检测

发布于 2024-11-18 07:11:20 字数 883 浏览 0 评论 0原文

我制作了一个小型自动化应用程序来帮助我启动和结束 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始于初秋 2024-11-25 07:11:21

可以使用以下命令测试服务的运行状态:

/sbin/service --test-if-configured-on "org.apache.httpd"

但是,这可能不适用于随 MacPorts 安装的服务。

另请参阅此问题的答案。

由于命令 service 在 Snow Leopard 下已被弃用,因此您也可以使用 launchctl 获取服务的运行状态。可以通过以下方式从 AppleScript 调用该命令:

try
    do shell script "/bin/launchctl list | grep -q org.apache.httpd" with administrator privileges
    set apache2Running to true
on error
    set apache2Running to false
end try

The running state of a service can be tested with the following command:

/sbin/service --test-if-configured-on "org.apache.httpd"

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:

try
    do shell script "/bin/launchctl list | grep -q org.apache.httpd" with administrator privileges
    set apache2Running to true
on error
    set apache2Running to false
end try
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文