2

This question already has an answer here:

I've created a file containing these two lines of code:

#!/bin/sh 
sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

I have ticked (from 'properties') the appropriate box to run the file as a program. Clicking it, I am asked to either run it or run in terminal. The second option works as intended, with the terminal asking for my password then running the command.

Is there a way to run the program directly? Currently it doesn't work (presumably because it cannot run the sudo command without password)

marked as duplicate by Byte Commander, muru command-line Jun 24 '16 at 10:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Replace sudo with gksudo -- in your script, like this:

gksudo -- sh -c "sync; echo 3 > /proc/sys/vm/drop_caches"

That will open a simple dialogue window to ask for your password instead of prompting you for it on STDERR which is not connected to a terminal if you run the script from your desktop.

On newer releases of Ubuntu, gksudo might not be preinstalled any more. If it doesn't work, you will have to install the gksu package first, using this command:

sudo apt-get install gksu
  • Although this seems like it should work, it doesn't. I do get the graphical password prompt, but the command doesn't seem to be executed - checking with free -m to check the results, nothing happens when using the shortcut, but it does respond when using the terminal. – Digital Dracula Jun 24 '16 at 7:52
  • And what happens if you run the gksudo command I gave you in the terminal? Do you get any error/debug messages there? Did you maybe forget the single quotes I added? – Byte Commander Jun 24 '16 at 9:31
  • @ByteCommander I don't think you should have quotes around sh -c as well, that's su -c syntax. – muru Jun 24 '16 at 10:22
  • @muru Try it, if you don't add the quotes, gksudo tries to interpret the -c as its own argument and fails. – Byte Commander Jun 24 '16 at 10:28
  • 1
    @ByteCommander then you should do gksudo -- sh -c '…' – muru Jun 24 '16 at 10:31

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