如何“以管理员权限执行shell脚本”作为普通用户而不是 root?

发布于 2024-10-13 12:38:51 字数 1041 浏览 3 评论 0原文

我正在运行一个 applscript,它通过终端运行 shellscript。

我希望能够使用“以管理员权限执行 shell 脚本”,但终端以 root 身份运行我的脚本,而不是以普通用户(即管理员)身份运行。

我不想以 root 身份运行的原因(除了明显的原因)是我使用 ~ 符号,以便任何用户都可以登录并在本地运行脚本(如果我将日志文件直接写入用户的桌面)。

我不想以 root 身份运行的另一个原因是,我在 shell 脚本中使用了 ViseX 安装程序,但该安装程序不能以 root 身份运行良好。

这是我使用的 applescript(感谢 regulus6633 的 applescirpt):

set theFile to "myscriptname.sh"

-- launch the application with admin privileges and get the pid of it
set thePID to (do shell script "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal > /dev/null 2>&1 & echo $!" with administrator privileges) as integer

-- get the bundle identifier of that pid so we can do something with the application
delay 0.2
tell application "System Events"
 set theProcess to first process whose unix id is thePID
 set bi to bundle identifier of theProcess
end tell

-- runs the Terminal with the shellscript
set theFileAlias to (POSIX file theFile) as alias
tell application id bi
 activate
 open theFileAlias
end tell

I'm running an applscript that runs a shellscript through Terminal.

I want to be able to use "do shell script with administrator privileges" but the Terminal runs my script as root, and not as regular user (which is an Admin).

The reason I want to run not as root (except for the obvious reasons) is that I use the ~ sign so any user can log in and run the script locally (f.e. I write a log file straight to the user's Desktop).

The other reason I want to run not as root is because I use a ViseX installer during my shell script that does not run well as root.

This is the applescript I use (Thanks to regulus6633 for the applescirpt):

set theFile to "myscriptname.sh"

-- launch the application with admin privileges and get the pid of it
set thePID to (do shell script "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal > /dev/null 2>&1 & echo $!" with administrator privileges) as integer

-- get the bundle identifier of that pid so we can do something with the application
delay 0.2
tell application "System Events"
 set theProcess to first process whose unix id is thePID
 set bi to bundle identifier of theProcess
end tell

-- runs the Terminal with the shellscript
set theFileAlias to (POSIX file theFile) as alias
tell application id bi
 activate
 open theFileAlias
end tell

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

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

发布评论

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

评论(2

忆沫 2024-10-20 12:38:51

我已经找到了成功的方法:

do shell script "sudo -H -u virtustilus /bin/bash -c \"/bin/bash -c '. ~/.bash_profile; /Users/vis/my_big_script.sh > /Users/vis/someLog.log 2>&1'\"" with administrator privileges

这里发生了什么:

  • 使用“root”用户(管理员权限)启动shell脚本。
  • 将用户更改为“virtustilus”(我的主用户)并更改主目录(标志 -H)。
  • 加载<代码>。 ~/.bash_profile 环境变量(如 PATH)。
  • 启动脚本 my_big_script.sh,并将所有输出重定向到文件 someLog.log。

它有效(我现在在自动化应用程序中使用它)。

I have found approach to get success with next:

do shell script "sudo -H -u virtustilus /bin/bash -c \"/bin/bash -c '. ~/.bash_profile; /Users/vis/my_big_script.sh > /Users/vis/someLog.log 2>&1'\"" with administrator privileges

What is going here:

  • Start shell script with 'root' user (administrator privileges).
  • Change user to "virtustilus" (my main user) and change home directory too (flag -H).
  • Load . ~/.bash_profile environment variables (like a PATH).
  • Start script my_big_script.sh with redirected all output to the file someLog.log.

It works (I'm using it in automator application now).

紫罗兰の梦幻 2024-10-20 12:38:51

不幸的是,具有管理员权限意味着“作为root”。在 Mac OS X 中,与大多数其他 Unix 派生操作系统一样,无法以非 root 用户身份获得管理员权限。当您通过 sudo 运行某些内容时,它只是以 root 身份运行该命令。

不过,您不需要以 root 身份运行整个终端窗口。只需以普通用户身份启动终端并告诉它(通过 tell application "Terminal" 块内的 do script)以 root 身份执行所需的命令。我不知道 do script 是否接受具有管理员权限,但如果没有,您可以随时在前面加上 sudo

Unfortunately, with administrator privileges means "as root". In Mac OS X, as in most other Unix-derived operating systems, there's no way to get administrator privileges as a non-root user. When you run something via sudo, it just runs the command as root.

You don't need to run an entire Terminal window as root, though. Just launch Terminal as the regular user and tell it (via do script inside a tell application "Terminal" block) to execute just the desired command as root. I don't know offhand if do script accepts with administrator privileges, but if not, you can always stick a sudo on the front.

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