开发者

How to "do shell script with administrator privileges" as a regular user and not as root?

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

I want to be able to us开发者_StackOverflowe "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


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜