I'm trying to create shortcut keys for some commonly used sudo shell commands (for example, having C-c s
run (shell-command "sudo /etc/init.d/apache2 restart")
).
I tried using a straight-up shell-command call as above, but it just outputs the following to the *Shell Command Output*
buffer:
[sudo] password for Inaimathi:
Sorry, try again.
[sudo] password for Inaimathi:
Sorry, try again.
[sudo] password for Inaimathi:
Sorry, try again.
sudo: 3 incorrect password attempts
It doesn't actually ask for a password. I don't want to have to start up Emacs using sudo emacs
, but I guess that's an option if nothing else will work.
The ideal solution would be a function from within Emacs (as opposed to OS jiggery-pokery to change the behaviour of the shell or the sudo
command). Something like (sudo-shell-command "dostuff")
, or (with-password-prompt (shell-command "sudo dostuff"))
.
EDIT: Scott's answer above is vastly preferable to this hack. Use that.
A possible solution:
I found out that setting a default password-asking utility solves this problem.
What I had to do is add
Defaults:ALL askpass=/usr/lib/openssh/gnome-ssh-askpass
to my/etc/sudoers
file (usingsudo visudo
, obviously).Eval-ing
(shell-command "sudo /etc/init.d/apache2 restart")
now prompts me for a password instead of trying to guess it unsuccessfully.I'm not accepting this answer unless it becomes clear that there's no better solution; ideally I'd like something that solves the problem internally to Emacs instead of requiring you to poke around your
/etc
directory.