You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to run a shell script in automator as root?

I am trying to create a fallback application in automator that will purge system memory and do so other things.

'#/bin/sh

sudo purge' is my shell script, and when I run it the error: 'no tty present and askpass program specified (1)' appears. I am running Mavericks (which is the whole reason I have to run it as root). Is there a way to run this without modifying the sudoers file?

iMac, OS X Mavericks (10.9.4), Mid 2011 2.7 Ghz and 4 GB memory

Posted on Oct 11, 2014 8:16 PM

Reply
11 replies

Oct 12, 2014 12:11 PM in response to DrDominodog51

You may want to describe what problems you are trying to solve. There could be a better way of doing what you want.


"will purge system memory" doesn't sound right to me. Mac OS X make use off all the memory it can. Any excess memory is used to store copies of applications that you have quit, so if you restore the appl it will load faster.


Anyway, here is an applescript with root access which you could invoke from automator:


(*


It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on. Here is an example.



Author: rccharles

For testing, run in the Script Editor.

1) Click on the Event Log tab to see the output from the log statement

2) Click on Run

For running shell commands see:

http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html



*)



on run

-- Write a message into the event log.

log " --- Starting on " & ((current date) as string) & " --- "

-- debug lines

set unixDesktopPath to POSIX path of "/System/Library/User Template/"

log "unixDesktopPath = " & unixDesktopPath

set quotedUnixDesktopPath to quoted form of unixDesktopPath

log "quoted form is " & quotedUnixDesktopPath

try

set fromUnix to do shell script "sudo ls -l " & quotedUnixDesktopPath with administrator privileges

display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix

on error errMsg

log "ls -l error..." & errMsg

end try

end run

Oct 13, 2014 10:31 AM in response to DrDominodog51

Hello


You may simply combine a Run AppleScript action to ask for password and a Run Shell Script action to invoke sudo with -S option to get password from STDIN.



Run AppleScript action:


- code =


tell application "System Events" activate return text returned of (display dialog "Enter password" default answer "" with hidden answer) end tell



Run Shell Script action:


- shell = /bin/bash

- pass input = to stdin

- code (e.g.) =


sudo -S id -u



Workflow will look something like this:


User uploaded file



Regards,

H

May 15, 2015 8:48 AM in response to weswait

Well, I'm not an expert (I came across the thread when trying to do similar) - others will hopefully chime in.


Is it possible? Yes. Should you do it? Be very careful (as the applescript will contain your password, so if someone accesses your machine, they can edit via Automator and just read your password).


This code worked for me:

______________________________

on run{}

tell "System Events"

activate

return "yourpasswordhere"

end tell

end run

______________________________


Once again, and I'll write it in CAPS just to be clear, THIS SOLUTION CREATES A SECURITY VULNERABILITY IF YOUR MACHINE IS ACCESSED!!! 🙂

Vince

Apr 11, 2016 10:36 PM in response to kanmax92

Hello


You can read manual pages of sudo(8) and id(1) commands in Terminal by man(1) command:


man sudo man id



sudo -S lets sudo read password from stdin.


id -u lets id print effective user id, which should be 0 for root.


Thus sudo -S id -u is just a test code to demonstrate id is successfully run as root.


The Run AppleScript action presents password input dialogue and returns the inputted text, which is then passed to the next Run Shell Script action's stdin from which sudo is to read the password by -S option.



I'm not really sure this workflow is safe, though.


Regards,

H

How to run a shell script in automator as root?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.