使Mac OSX launchctl在启动时以root身份启动进程 [英] Making Mac OSX launchctl launch a process as root on startup

查看:636
本文介绍了使Mac OSX launchctl在启动时以root身份启动进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,重启后如何自动使用root特权启动启动的MacOSX守护进程?

My question is how to make a MacOSX daemon process using launchd start with root privileges automatically after a reboot?

我正在编写一个内部使用的应用程序,该应用程序阻止对网站的访问.它是用python编写的,并且修改了/ect/hosts文件以禁用或启用列出的url.主要应用程序在django中,我创建了一个python扭曲的守护程序,该守护程序实际对/etc/hosts文件进行了修改,因为需要root访问权限.

I'm writing an application for in house use that blocks access to web sites. It is written in python and modifies the /ect/hosts file to disable or enable listed urls. The main application is in django and I created a python twisted daemon that does the actual modification of the /etc/hosts file as root access privileges are required.

我创建了一个plist文件,该文件可以解决一个小问题.重新引导后,守护进程将具有我的常规登录特权,而不是root特权.

I have created a plist file which works with one minor issue. After rebooting the daemon process has my normal logon privileges instead of root privileges.

一种解决方法是使用我的普通特权停止该进程,然后使用sudo启动该进程.

A workaround is to stop the process with my normal privileges then startup the process with sudo.

launchctl unload /Library/LaunchAgents/com.balanceinfosystems.socialshields.twisted.plist 
sudo launchctl load /Library/LaunchAgents/com.balanceinfosystems.socialshields.twisted.plist 

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 >
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.balanceinfosystems.socialshields.twisted</string>
    <key>Program</key>
    <string>/source/social_shields/social_shields_twisted.py</string>
    <key>KeepAlive</key>
    <true/>
  </dict>
</plist>

推荐答案

您将其放在错误的目录中. LaunchAgent是按用户运行的进程.系统进程进入/Library/LaunchDaemons.它们以root身份运行.如果您希望此代码在启动时运行,建议您通过以下方式对此进行明确显示:

You've put this in the wrong directory. LaunchAgents are processes run per-user. System processes go in /Library/LaunchDaemons. They are run as root. If you want this to run at startup, I recommend making that explicit with:

<key>RunAtLoad</key>
<true/>

这篇关于使Mac OSX launchctl在启动时以root身份启动进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆