Run sudo command in bash script and run it with run

I have a bash script that I would like to run with a plist file on OS X. The problem is that the bash script contains sudo commands and it stops it from running. So, for example, my bash script looks like this:

#!/bin/bash sudo /opt/local/bin/bindfs -u user1 /Library/WebServer/Documents/user1 /vhosts/user1/public_html sudo /opt/local/bin/bindfs -u user2 /Library/WebServer/Documents/user2 /vhosts/user2/public_html 

and my com.test.bindfs.plist file looks like this (created using Lingon):

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>GroupName</key> <string>admin</string> <key>Label</key> <string>com.jamespayne.bindfs</string> <key>ProgramArguments</key> <array> <string>/usr/bin/bindfs.sh</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist> 

I checked that the script works by running it after launch and entering the password, but it does not start when it starts. The launch plugin is started, but the following error occurs:

sudo: no tty is present and the requested program is not specified

Does anyone know how to make this work or why I can get this error. Thanks.

+4
source share
3 answers

sudo is an interactive command that requires the user to enter a password to continue. I would suggest that sudo , being unable to find tty, just fails.

The command you need is su , which is not interactive, except that you need to be root to run it. However, in your case, you probably do not need it at all, given that the script is run by a privileged user?

Also why do you put scripts in /usr/bin ? Bad idea; use /usr/local/bin instead (or /usr/local/sbin if one exists).

+1
source

Perhaps you just move the property list to /Library/LaunchDaemons/ and remove the sudo commands from the script.

See man launchd , man launchd.plist and this blog post .

+4
source

If you came here from Google looking to launch a LaunchAgent user account with sudo / root privileges, you can do the following:

  • Put plist in /Library/LaunchAgents if it is running for all users or ~/Library/LaunchAgents if it is running for only one user.
  • The first one has ProgramArguments be sudo , and the rest of the team
  • Configure NOPASSWORD in /etc/sudoers.d to allow launchctl escalate your specific command without prompting for an interactive password.

See this answer for a more detailed walkthrough.

+1
source

Source: https://habr.com/ru/post/1439111/


All Articles