2

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)

1 Answers1

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
Byte Commander
  • 107,489
  • 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. –  Jun 24 '16 at 07: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 09: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
  • @ByteCommander Hmm.. Peculiar. It doesn't work in the terminal either. I didn't forget the quotes. I don't get any error message, the graphical prompt appears, I enter the password, but right after (checking with free -m) there is no change in memory. Immediately after, trying sudo sh -c "sync; echo 3 > /proc/sys/vm/drop_caches" works. Is there a problem with the syntax, I wonder... –  Jun 24 '16 at 10:28
  • 1
    @ByteCommander then you should do gksudo -- sh -c '…' – muru Jun 24 '16 at 10:31
  • @muru this syntax solved it! It now works as intended. Thanks everyone, I'll mark this as solved –  Jun 24 '16 at 10:34