0

I am trying to run a script in Django. This script needs to be run with sudo and I have setup the sudoers file so that the command can be executed without entering the sudo password. When I am using the default test server, ie >python manage.py runserver 0.0.0.0:8000, the script executes with no problem. However, after deploying Django with Nginx and uWSGI, the command fails and returns the response

sudo: no tty present and no askpass program specified

Is there some sort of configuration that I have missed?

I am using subprocess to execute the script and the code inside the Django view is like this:

subprocess.check_output( "sudo /path/to/file/fileName.sh", shell=True)
Youpe1949
  • 1
  • 2
  • Are you sure even the user that uwsgi is running as has permission to run the script without providing a password? Did you check that your sudoers file does not contain the `requiretty` option? – koniiiik Apr 18 '16 at 08:25
  • Its better, if you can write a python code which will do what the script does and put that python code on your view. – ruddra Apr 18 '16 at 08:28

1 Answers1

0

You need to add the user you are using to the sudoers list:

Granting the user to use that command without prompting for password should resolve the problem. First open a shell console and type:

sudo visudo

Then edit that file to add to the very end:

username ALL = NOPASSWD: /fullpath/to/command, /fullpath/to/othercommand

eg

john ALL = NOPASSWD: /sbin/poweroff, /sbin/start, /sbin/stop

will allow user 'john' to sudo poweroff, start and stop without being prompted for password.

Look at the bottom of the screen for the keystrokes you need to use in visudo - this is not vi by the way - and exit without saving at the first sign of any problem. Health warning: corrupting this file will have serious consequences, edit with care!

source: How to fix 'sudo: no tty present and no askpass program specified' error?

Community
  • 1
  • 1
ddalu5
  • 401
  • 4
  • 17
  • I have already done that. The problem is the command doesn't work in the deployed Django – Youpe1949 Apr 18 '16 at 08:31
  • Which user is running the script? it's that user that have to be added to the sudoers list, (btw it's really not secure to run a web app using a sudoer user). – ddalu5 Apr 18 '16 at 08:34
  • The user running the script is the same as the sudoers file. John like the example above. – Youpe1949 Apr 18 '16 at 08:44
  • But in normal cases Nginx uses the "www-data" user, if your script is automated so it run's on some web related events it will be executed by www-data. – ddalu5 Apr 18 '16 at 08:53