LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-14-2008, 09:34 PM   #1
CJS
Member
 
Registered: May 2008
Location: California, USA
Distribution: Ubuntu 8.10
Posts: 247

Rep: Reputation: 49
gksudo vs. sudo strangeness


How do you redirect the output of gksudo as the root user? For instance, "gksudo cmd > file1" outputs file1 under the original user, not root. Trying:
Code:
gksudo 'cmd > file1'
does not work at all. There are tricks with sudo that work, such as:
Code:
sudo bash -c 'cmd > file'
but:
Code:
gksudo bash -c 'cmd > file'
does not work. I also noticed that gksudo does not seem to output stderr; if you run "gksudo cmd" in the terminal window (and assuming cmd normally outputs to the terminal), then cmd will output stdout to the terminal but not stderr. This is unlike sudo which handles both stdout and stderr.

Unfortunately I can't just simply use sudo, because I'm using it in a bash script that will be run from a desktop shortcut--there is no terminal involved, so I need the graphical input of gksudo for accepting the password.

Any ideas of how to make gksudo work in this case? Any insight/help would be greatly appreciated.
 
Old 05-15-2008, 06:03 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Quote:
there is no terminal involved
stderr and stdout are terminal streams. GUI windows accept messages from user input devices, not stdin.

You've kind of answered your own question here.
 
Old 05-15-2008, 08:07 AM   #3
CJS
Member
 
Registered: May 2008
Location: California, USA
Distribution: Ubuntu 8.10
Posts: 247

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by jim mcnamara View Post
stderr and stdout are terminal streams. GUI windows accept messages from user input devices, not stdin.

You've kind of answered your own question here.
OK, fair enough, but I think that still leaves me with my main question--is there any way to run commands (that use stdin and stderr) in scripts as root, and redirect their output as root, while running the script from a desktop shortcut? If I use sudo that obviously doesn't work because I have no chance to input a password. I could make the script call up a terminal to ask for the password, but I would much rather have the nice GUI input of gksudo. So is it simply not possible to do with gksudo?
 
Old 05-15-2008, 08:28 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You could do the re-direction inside a script. e.g.
Code:
#!/bin/bash
some_command | tee /some/log/file
(tee outputs to standard output and the log file. Add the -a option to append to the log file, rather than overwriting it.)

Call that script with gksudo.
 
Old 05-15-2008, 06:29 PM   #5
CJS
Member
 
Registered: May 2008
Location: California, USA
Distribution: Ubuntu 8.10
Posts: 247

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by matthewg42 View Post
You could do the re-direction inside a script. e.g.
Code:
#!/bin/bash
some_command | tee /some/log/file
(tee outputs to standard output and the log file. Add the -a option to append to the log file, rather than overwriting it.)

Call that script with gksudo.
Yes, I think that's a great way to do it if one doesn't mind running the whole script as root. I also figured out at least one solution to my original problem of running single commands, and their redirections, as root in a script (while using the GUI password input of gksudo):
Code:
#!/bin/bash
gksudo true
sudo bash -c 'cmd > file'
Basically, just run gksudo with some placeholder command like "true", which then allows the gksudo GUI to handle getting the password to run as root, and then sudo actually gets the job done since the password has all ready been entered.
 
Old 05-15-2008, 06:47 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Then I think you need to patch gksudo to pipe output to stdout (if it has a tty), or request such a feature from the gksudo team.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Ultimate Sudo FAQ — To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 01:36 AM
LXer: sudo, or not sudo: that is the question LXer Syndicated Linux News 0 02-07-2008 05:40 PM
gksudo problems FBorges22 Linux - Software 1 09-07-2007 04:59 PM
gksudo nautilus ronbrooks Linux - Software 8 02-02-2007 02:00 PM
Restricting Editing in Sudo (Advanced Sudo Question) LinuxGeek Linux - Software 4 11-04-2006 03:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration