2

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?

2
  • Is this a GUI app or just a Unix executable?
    – mmmmmm
    Commented Apr 27, 2011 at 22:13
  • GUI app, they have a console mode, but it is an interactive console
    – at01
    Commented Apr 27, 2011 at 22:16

5 Answers 5

0

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.

1

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

0

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.

5
  • 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
    Commented Apr 27, 2011 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? Commented Apr 27, 2011 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
    Commented Apr 27, 2011 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? Commented Apr 27, 2011 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
    Commented Apr 27, 2011 at 22:29
0

use sudo open -a appThatNeedsPort25

2
  • I get this error: LSOpenURLsWithRole() failed for the application DevNullSmtp.jar with error -10810.
    – at01
    Commented Apr 27, 2011 at 22:08
  • Try sudo open -a /path/to/Application.app
    – styfle
    Commented Jun 27, 2011 at 1:22
0

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

1
  • I would actually prefer to type in my password in this case
    – at01
    Commented Apr 27, 2011 at 22:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .