0

When I execute gksudo, I get some output in the terminal, I want to supress that output, but still get the output from the actual command. For example:

$ gksudo whoami
> #some gksudo related output
> root

When I try to dump the output to /dev/null like this:

$ gksudo &> /dev/null whoami

I get the prompt message without output, but the whoami result is also supressed.

angrykoala
  • 101
  • 4
  • 2
    You should include the **"#some gksudo related output"** because on my system there are no error messages. What version of Ubuntu are you using? – WinEunuuchs2Unix Jul 30 '17 at 20:43
  • The warnings are related to the gtk theme, however, is not really important what the errors are as I just want to hide the output, not fix the warnings – angrykoala Jul 31 '17 at 07:57
  • 1
    I get warnings related to gtk theme all the time when running **gedit** which I suppress programmatically so I totally understand your annoyance at them. My point is **there are no errors with whoami** on my system which makes me concerned about your system. I tried `gksudo`, `gksu` and `pkexec` all of them have no errors. – WinEunuuchs2Unix Jul 31 '17 at 10:45
  • whoami is just an example as well, it could be any other command, which I want to get the output, including errors – angrykoala Jul 31 '17 at 10:56
  • 1
    I understand. I'm just trying to duplicate the problem on my side. Are you using Ubuntu? In the mean time look at: https://askubuntu.com/questions/896935/how-to-make-zenity-transient-parent-warning-disappear-permanently and: https://askubuntu.com/questions/505594/how-to-stop-gedit-and-other-programs-from-outputting-gtk-warnings-and-the-like/572827#572827 as possible duplicates. – WinEunuuchs2Unix Jul 31 '17 at 11:49
  • This is for a script, so I would rather get a programmatic solution to avoid the output on any system. Instead of whoami, any command that send stderr would be valid to recreate the problem – angrykoala Jul 31 '17 at 12:38
  • 1
    What version of Ubuntu are you using? Please provide the output of `lsb_release -a` as well as the full error message you are getting. – Mark Kirby Jul 31 '17 at 23:14
  • 1
    `gksudo` is deprecated and you should not really use it any more. Its purpose was to launch GUI applications as root, which is generally frowned upon and shall no longer be officially supported. That's also why the `gksu` package is no longer installed by default. Please include more detail in your question, like the exact output message you get as well as what exact command you are trying to run. Also why do you want to use `gksudo` instead of plain `sudo`? – Byte Commander Jul 31 '17 at 23:32
  • @ByteCommander I want to launch root commands from a GUI application, if there is a better way to prompt graphically for the sudo password I would like to know – angrykoala Aug 01 '17 at 07:33
  • 2
    It would be useful if you share your script with us to find the best alternative for this specific use case. You also still have not provided the exact error message and the exact output of `lsb_release -a` and the exact command you want to run as root. We can't help you if you don't give us the information we need. – Byte Commander Aug 01 '17 at 10:10

2 Answers2

6

I realise this is resolved now but just picking on your comment:

I want to launch root commands from a GUI application, if there is a better way to prompt graphically for the sudo password I would like to know

pkexec is a relatively modern alternative that works slightly out of band compared to sudo/gksu. It should be available on all the Ubuntu desktops.

enter image description here

Oli
  • 289,791
  • 117
  • 680
  • 835
  • Excellent answer. What software do you use to create the clip/animation? – pzkpfw Aug 01 '17 at 10:17
  • That was using [Peek](http://www.omgubuntu.co.uk/2016/08/peek-desktop-gif-screen-recorder-linux). Wouldn't advocate it for every answer but it seemed to make sense here. – Oli Aug 01 '17 at 10:24
3

All errors in a terminal are sent to stderr. To prevent displaying an error, you might need to redirect stderr, like this:

gksudo whoami 2> /dev/null
Eliah Kagan
  • 116,445
  • 54
  • 318
  • 493
ob2
  • 3,517
  • 17
  • 16
  • Thanks. This works fine for the case I commented, however, is there a way to still get the errors from the command whoami? – angrykoala Jul 31 '17 at 07:55
  • 3
    @angrykoala if you examine `/proc//fd` for `gksudo` and the command you ran with it, you'll see that stderr of the command is a pipe to gksudo. So if you suppress gksudo's stderr, you suppress the command's stderr as well. – muru Aug 01 '17 at 01:43