~/Library/LaunchAgents plist 手动运行但不会自动运行

发布于 2024-10-08 19:56:28 字数 1477 浏览 1 评论 0 原文

我开始使用 launchd 并想要设置一个 plist 文件,这样每当我将 SD 卡插入我的 Mac mini 服务器(带有 Snow Leopard 服务器)时,我想要运行一个 shell 脚本(它应该复制所有 jpg 文件) ,重命名它们等)。

因此,我在 ~/Library/LaunchAgents 中创建了一个 plist 文件(其内容见下文 - 它应该查找 /Volumes 的更改),并且创建了一个 shell 脚本,其中显示“beep” - 稍后它将执行一些更有用的操作。

plist文件是用launchctl注册的,当我运行它时(launchctl start com.peters.runwhenSDmount),每当插入存储卡时,计算机都会发出蜂鸣声,而当没有存储卡时,计算机会保持沉默。因此,显然 plist 确实调用了 shell 脚本,该脚本随后检查特定的 SD 卡是否存在。我想这也证明SD卡的权限没有问题。

但是,它似乎没有自己运行???知道为什么吗?


plist 文件:~/Library/LaunchAgents/com.peters.runwhenSDmount.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
    <key>Label</key>
    <string>com.peters.runwhenSDmount</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
    <string>/Users/peter/Library/Scripts/runwhenSDmount</string>
    </array>
    <key>ThrottleInterval</key>
    <integer>10</integer>
    <key>WatchPaths</key>
    <array>
    <string>/Volumes</string>
    </array>
</dict>
</plist>

shell 脚本:~/Library/Scripts/runwhenSDmount

#!/bin/bash
if [ -d "/Volumes/NIKON D40X" ]; then
    say beep
fi

I am starting to work with launchd and want to set up a plist file such that whenever I insert an SD card into my Mac mini server (with Snow Leopard Server), I want a shell script to run (which should copy all the jpg files, rename them etc).

So, I created a plist file in the ~/Library/LaunchAgents (see below for its contents - it should be looking for changes to /Volumes) and I created a shell script which says "beep" - later it will do something more useful.

The plist file is registered with launchctl, and when I run it (launchctl start com.peters.runwhenSDmount), the computer says beep whenever a memory card is plugged in, and stays silent when there is no memory card. So, apparantly the plist does call the shell script, which subsequently checks if the specific SD card is there. I assume this also proves that there is no problem with permissions for the SD card.

But, it doesnt seem to run by itself??? Any idea why??


plist file: ~/Library/LaunchAgents/com.peters.runwhenSDmount.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
    <key>Label</key>
    <string>com.peters.runwhenSDmount</string>
    <key>LowPriorityIO</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
    <string>/Users/peter/Library/Scripts/runwhenSDmount</string>
    </array>
    <key>ThrottleInterval</key>
    <integer>10</integer>
    <key>WatchPaths</key>
    <array>
    <string>/Volumes</string>
    </array>
</dict>
</plist>

shell script: ~/Library/Scripts/runwhenSDmount

#!/bin/bash
if [ -d "/Volumes/NIKON D40X" ]; then
    say beep
fi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

傾旎 2024-10-15 19:56:28

在 ~/Library/LaunchAgents 文件夹中创建新的 plist 后,您必须将其告知 launchd 应用程序。执行此操作的两种基本方法是:

  1. 注销然后重新登录。 - 每次登录时,launchd 都会扫描 ~/Library/LaunchAgents 文件夹的内容并添加它在其中找到的任何 plist。注销

  2. 使用“launchctl”从终端命令行加载 plist。该命令的语法为:

    launchctl load {Path-to-plist}
    

launchctl 命令还可用于阻止 launchd 使用 plist。为此,请使用:

launchctl unload {Path-to-plist}

launchctl 命令在开发 plist 时非常有用,因为它可以在更改之间快速轻松地卸载/加载它们。一旦你的 plist 按照你喜欢的方式工作,自动登录 launchd 加载就可以接管。

After you create a new plist in your ~/Library/LaunchAgents folder, you have to tell the launchd application about it. The two basic ways to do that are:

  1. Logout then log back in. - Every time you log in, launchd will scan the contents of your ~/Library/LaunchAgents folder and add any plist it finds there.

  2. Load the plist from a terminal command line with "launchctl". The syntax of the command is:

    launchctl load {Path-to-plist}
    

The launchctl command can also be used to stop launchd from using a plist. To do that, use:

launchctl unload {Path-to-plist}

The launchctl command is very useful when developing plists since it makes unloading/loading them between changes quick and easy. Once you have a plist working the way you like, the automatic login launchd loading can take over.

望喜 2024-10-15 19:56:28

我刚刚在自动启动 ~/Library/LaunchAgents 中的服务时遇到了类似的问题,但在我的情况下,*.plist 定义的服务都没有启动。

问题显然与目录 ~/Library/LaunchAgents 有关,而不是 plist 文件本身。解决方案是重置文件权限。

chmod 700 ~/Library/LaunchAgents

自制软件用户更新:(2015-02-23)

昨天我刚刚发现 LaunchRocket 这是一个 Mac PreferencePane使用 launchd 管理服务。它具有自制软件意识,并添加了一个漂亮的用户界面来管理启动的自制软件服务。

这可能无法帮助您处理不正确的用户权限,但它是开源的,因此您可以分叉该项目并添加权限检查作为一项功能。

I just had a similar problem with automatically launching the services in ~/Library/LaunchAgents, but in my case NONE of the *.plist defined services got started.

The problem was obviously connected to the directory ~/Library/LaunchAgents and not the plist files itself. The solution was to reset the file permissions.

chmod 700 ~/Library/LaunchAgents.

Update for homebrew users: (2015-02-23)

Yesterday I just found LaunchRocket which is a Mac PreferencePane for managing services with launchd. It is homebrew aware ands adds a nice UI for managing launchd homebrew services.

This may not help you with incorrect user permissions but it is open source so you can fork the project and add the permission check as a feature.

你另情深 2024-10-15 19:56:28

有助于创建和管理启动项的两个工具是:

  1. LaunchControl - " LaunchControl 是一个功能齐全的 launchd(8) 前端,允许您在 Mac 上创建、管理和调试系统和用户服务。"
  2. Lingon - “一个易于使用但功能强大的实用程序,可以在 Mac 上自动运行东西

注:Brett Terpstra(他做了很多很棒的 Mac 开发工作)致力于 nvAlt) 最近在他的帖子“使用notifyutil和launchd远程触发任务"他曾经使用Lingon,但最近一直在使用 LaunchControl。其中任何一个都值得研究。

Two tools that help with the creation and management of launchd items are:

  1. LaunchControl - "LaunchControl is a fully-featured launchd(8) frontend allowing you to create, manage and debug system- and user services on your Mac."
  2. Lingon - "An easy to use yet powerful utility that runs things automatically on your Mac"

As a note: Brett Terpstra (who does a bunch of great Mac work on things like nvAlt) recently commented in his post "Triggering tasks remotely with notifyutil and launchd" that he used to use Lingon, but has been using LaunchControl more recently. Either of them is worth looking into.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文