如何以 root 身份运行 launchd 命令

Emm*_*ngi 34 mac root launchd cron macos

我有以下 launchctl 命令作为 .plist 文件。它已加载并设置为每天运行一次,但是它需要以 root 身份运行,我不知道如何验证这一点。

此外,这个 cron 作业基本上 CD 到一个目录中并运行一个命令。我确信 launchd 有更好的方法来指定它应该运行命令的目录。

我怎么知道它是以 root 身份运行的,有没有更好的方法来编写它?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>dev.project.frontpage.feedparser</string>
    <key>ProgramArguments</key>
    <array>
        <string>cd</string>
        <string>/Users/eman/src/project/trunk/includes/;</string>
        <string>./feed-parser.php</string>
        <string>-c</string>
        <string>./feed-parser-config.xml</string>
    </array>
    <key>QueueDirectories</key>
    <array/>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>12</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>WatchPaths</key>
    <array/>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

Che*_*ion 49

.plist存放在什么文件夹?

launchd以 root 身份运行守护进程(/Library/LaunchDaemons/System/Library/LaunchDaemons),无论用户是否登录都会运行它们。启动代理(/Library/LaunchAgents/~/Library/LaunchAgents/)在用户以该用户身份登录时运行。您不能使用 setuid 来更改在守护程序上运行脚本的用户。

因为您想将它添加进来,所以/Library/LaunchDaemons您需要确保launchd以管理员权限加载它(例如sudo launchctl load -w /Library/LaunchDaemons/com.apple.samplelaunchdscript.plist

查看man launchd更多信息。

  • @克劳迪克斯:没错。将 launchd 配置复制到位是不够的 - 您仍然必须“打开它”(launchctl load) (2认同)