Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm trying to open an app that listens to port 25 within a shell script. In order to listen to port 25, I need to run the app with sudo. So I tried the following:

sudo open appThatNeedsPort25

It asks me for my password and opens the app. Problem is the app is NOT being run as root, so it is unable to listen to port 25...

What do I do to open an app from a shell script so that it can listen to port 25?

share|improve this question
    
Is this a GUI app or just a Unix executable? –  Mark Apr 27 '11 at 22:13
    
GUI app, they have a console mode, but it is an interactive console –  at01 Apr 27 '11 at 22:16
add comment

5 Answers 5

up vote 0 down vote accepted

sudo -b makes the application run in the background.

sudo -b appThatNeedsPort25

Unlike with sudo appThatNeedsPort25 &, sudo itself will run in the foreground, so you'll have no issues with its password prompt. Note that sudo also has a -A option to make it ask for a password through a GUI instead of in the terminal, you can use this when sudo isn't running from a terminal.

share|improve this answer
add comment

Just try sudo appthatneedsport25, no need to use open when dealing with bash or shell scripts. If it is an interpreted script (python ruby etc) and it isnt set to executable, you can always do sudo python scriptname.

share|improve this answer
    
hmm, ok, I can run it in the background by appending > /dev/null 2>&1 &. I guess it's not asking me for a password because I've been playing with sudo... my concern is running something in the background with & will have issues when prompting me for a password with sudo.. –  at01 Apr 27 '11 at 22:07
    
It shouldnt, because it gets those privs when you run it, and the rest of that is redirected to nothing at this point. Does this application require future input from you? Can you tell us what it is and what you use it for? –  hobs Apr 27 '11 at 22:14
    
It is a dummy smtp server for dev purposes. So I just tried it after the sudo password remembering timed out and it just presents 3 password prompts and exits. I guess I have to run a dummy app first with sudo... –  at01 Apr 27 '11 at 22:17
    
If you wanted to run it without asking for a password, then yes, would need to run an app beforehand to cache it, but why not just put the password in after you start the smtp server? –  hobs Apr 27 '11 at 22:27
    
The problem is the script that runs my app just quits. You can't do sudo appname &. Running it in the background doesn't give you the opportunity to enter your password. –  at01 Apr 27 '11 at 22:29
add comment

use sudo open -a appThatNeedsPort25

share|improve this answer
    
I get this error: LSOpenURLsWithRole() failed for the application DevNullSmtp.jar with error -10810. –  at01 Apr 27 '11 at 22:08
    
Try sudo open -a /path/to/Application.app –  styfle Jun 27 '11 at 1:22
add comment

As an (slightly insecure) alternative to sudo add your ssh key to your root user then you can do 'ssh root@localhost appThatNeedsPort25'.

The first time you do this it may prompt you for your ssh key passphrase but after that it's cached until you reboot.

As a further optimisation add this to your ~/.ssh/config file:

Host root
    Hostname 127.0.0.1
    User root

Then you can just type 'ssh root appThatNeedsPort25' instead.

I've been doing this 'ssh root' trick for years and it's saved me countless minutes of re-typing my password. :-)

share|improve this answer
    
I would actually prefer to type in my password in this case –  at01 Apr 27 '11 at 22:28
add comment

You do it like this

sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

Alternatively

#   sudoapp: Runs .app with root privileges
#   --------------------------------------------------------------------
    sudoapp () {
        sudo "$1/Contents/MacOS/$(defaults read "$1/Contents/Info.plist" CFBundleExecutable)" $2
    }

$ sudoapp /Applications/TextEdit.app /etc/hosts

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.