You are not logged in.

#1 2009-10-26 10:45:54

clementis
Member
Registered: 2009-08-26
Posts: 37

problem running sudo command in script

Hello,

I try to run this script.

#!/bin/bash
sudo /bin/cat /dev/input/event1 >> /home/tom/.log/log.bin &

The sudoers entry

%users   ALL= NOPASSWD: /bin/cat /dev/input/event1 >> /home/tom/.log/log.bin

I get the following error!

$  Passwort: sudo: pam_authenticate: Kommunikationsfehler

Kommunikationsfehler = eng: communication error

What's wrong with that?

Thank you!
clementis

Offline

#2 2009-10-26 16:06:17

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: problem running sudo command in script

Can't you issue any sudo command without an error?

Offline

#3 2009-10-27 00:52:17

mjheagle8
Member
From: /home/mjheagle8
Registered: 2009-07-12
Posts: 186

Re: problem running sudo command in script

i dont know if it matters, but in my sudoers, i have the syntax

%users ALL=(ALL)NOPASSWD: command

dont know if the (ALL) makes a difference, but give that a shot.


Desktop/Laptop - DWM :: VM - screen
Registered Linux User Number 483137 :: Victory! :: GitHub

Offline

#4 2009-10-27 14:44:20

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: problem running sudo command in script

mjheagle8 wrote:

i dont know if it matters, but in my sudoers, i have the syntax

%users ALL=(ALL)NOPASSWD: command

dont know if the (ALL) makes a difference, but give that a shot.

I don't have that, and it works fine for me. So, that's probably not the case.

Offline

#5 2009-10-27 17:02:17

clementis
Member
Registered: 2009-08-26
Posts: 37

Re: problem running sudo command in script

yes, I can run command as sudo without error!

Just this script doesn't work.

I changed sudoers to

tom      ALL=(ALL) ALL
%users   ALL= NOPASSWD: /bin/cat /dev/input/event1

just to get certain that sudoers is not faulty.

I also created an alias

alias event1='sudo /bin/cat /dev/input/event1 >> $HOME/.cblog/log.bin &'

Now the script and the alias are running without problems.

But I can't autostart it in xfce!

Any hints?

Thank you!
Clementis

Offline

#6 2009-10-27 19:12:41

SIGTERM
Member
Registered: 2009-09-19
Posts: 67
Website

Re: problem running sudo command in script

I don't really understand your /etc/sudoers.

alias event1='sudo /bin/cat /dev/input/event1 >> $HOME/.cblog/log.bin &'

You are aliasing something inside your alias...

What do you mean, "can't autostart"? Won't it autostart, don't you know how to autostart? Please be more precise.

Offline

#7 2009-11-08 22:11:41

clementis
Member
Registered: 2009-08-26
Posts: 37

Re: problem running sudo command in script

Sorry, for being not precise.

I created the alias in .bashrc in my $home directory. In sudoers all users are allowed to key in this command without a passwort entry.

I know how to autostart in xfce and so created an autostart entry but unfortunatly it doesn't work for my "special" command.

What I did:

1. I wrote a script (first post) and tried to autostart it in xfce. For that command I created an entry in sudoers in order to avoid enter root passwort. The script didn't start.
Then...
2. I created an autostart entry (deleted the first one - see point 1) and entered the whole command (cat .../event1 >> $home...) in the xfce autostart input window. The idea was to run the command directly and not through a script. But it didn't work.
Therefore I tried to run alter the command to "bash cat .../event1 >> $home ...." Didn't work either.
3. I also deleted the autostart entry no 2. and created the alias in .bashrc in my home directory. Now created a new autostart entry trying to start the alias. Didn't work.

I also tried to set the Terminal option in the file $home/.config/autostart/log.desktop to "true".
So it looked like this:

[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=log
Comment=event1
Exec= sudo cat /dev/input/event1 >> /home/tom/.cblog/log.bin &
StartupNotify=false
Terminal=true
Hidden=false

The alias for example works well if I enter it in xterm.

So, I don't know why it isn't working! :-(

Thank you for any help

Clementis

Offline

#8 2009-11-09 17:36:26

IgnorantGuru
Member
Registered: 2009-11-09
Posts: 640
Website

Re: problem running sudo command in script

Not sure if this is your problem, but if you issue a command like

sudo cat /dev/input/event1 >> /home/tom/.cblog/log.bin

The command "cat /dev/input/event1" is executed as root, but the >> redirect only has user permissions.  If you want the redirect to write as root use something like:

sudo sh -c "cat /dev/input/event1 >> /home/tom/.cblog/log.bin"

Offline

#9 2009-11-09 18:07:20

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: problem running sudo command in script

regarding the alias not working in the xfce autostart, this might shed some light on that.

//blue/0/~/ grep ^alias\ tester .bashrc
alias tester='echo this is from an alias'
//blue/0/~/ tester
this is from an alias
//blue/0/~/ bash -c 'tester'
bash: tester: command not found
//blue/127/~/ bash -cli 'tester'
this is from an alias

man bash to see what the -l and -i options are actually doing.  (for instance, -i might not be needed depending on how your .bashrc is set up).

edit: i also dunno how xfce likes spaces in the autostart thing, worth a double check:

your code wrote:

Exec= sudo cat /dev/input/event1 >> /home/tom/.cblog/log.bin &

Last edited by brisbin33 (2009-11-09 18:27:02)

Offline

#10 2009-11-10 01:45:05

perbh
Member
From: Republic of Texas
Registered: 2005-03-04
Posts: 765

Re: problem running sudo command in script

Hmmm - maybe put double quotes around it?

Offline

#11 2009-11-10 01:59:33

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: problem running sudo command in script

mjheagle8 wrote:

dont know if the (ALL) makes a difference, but give that a shot.

"man sudoers"

Offline

#12 2009-11-11 22:47:48

clementis
Member
Registered: 2009-08-26
Posts: 37

Re: problem running sudo command in script

Thx!

I gave all your suggestions a try but nothing worked. A Terminal is autostarted but the command is not executing. I only see the black xterm window, the prompt is missing i.e. the cursor is at the beginning of the line without a prompt.

If I execute the command in the Alt+F2 window I get the same result.
If executing the command directly in the terminal it works perfectly.

Any hints! Please!
Clementis

Offline

#13 2009-11-11 23:01:02

masterkoppa
Member
Registered: 2009-04-14
Posts: 92

Re: problem running sudo command in script

You might want to try this:

Exec= sudo xterm -e cat /dev/input/event1 >> /home/tom/.cblog/log.bin &
OR
Exec= xterm -e sudo cat /dev/input/event1 >> /home/tom/.cblog/log.bin &

I used something similar with another terminal emulator when using Open Box. It should work out perfectly and the console will exit after the command is executed.

Offline

#14 2009-11-12 14:51:58

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: problem running sudo command in script

You cannot use sudo with a redirection. The redirection is parsed by bash, it is not an argument to sudo. The only way is to use su, or write a script.

Offline

#15 2009-11-13 22:43:24

clementis
Member
Registered: 2009-08-26
Posts: 37

Re: problem running sudo command in script

I created an alias for the command in bash.rc. Running the alias in a terminal window is no problem. If I press Alt+F2 and key in "bash -cli "event1"" (event1 is my alias) it also works perfect.

Nothing worked, i.e. it seems that autostarting that command is not possible.

Maybe someone knows a way to run the command before x starts.

Offline

#16 2009-11-13 22:56:56

anonymous_user
Member
Registered: 2009-08-28
Posts: 3,059

Re: problem running sudo command in script

Have you tried putting the command in your rc.local file?

Offline

#17 2009-11-14 06:58:06

llcawthorne
Member
From: Columbia, SC
Registered: 2009-10-16
Posts: 142

Re: problem running sudo command in script

I wasn't aware you could redirect output with sudo'd permissions, but the sudoers manpage educated me with an example.  I got the following from 'man sudoers' but modified the command:

Run visudo.  Add the line:

%wheel  ALL=NOPASSWD: /bin/sh -c "cat /etc/fstab.secure >> /joe.txt"

And now I can sudo with that command from a terminal or from a run box with no problems and no password prompt.  The big things are the full path to sh and the quotes around the command, but nowhere else.

I just had to make an /etc/fstab.secure (copy of my fstab set to chmod 700) and was able to write the joe.txt file to my root (which my user cannot write to).  I deleted /joe.txt and ran the command again from a run box.  Nothing displayed on the screen at all, but when I checked, joe.txt had been successfully written to my root directory.  Maybe it'll work for you (with your command in the quotes, obviously).

Lewis


To understand recursion, you must understand recursion.

Offline

#18 2009-11-14 07:00:44

llcawthorne
Member
From: Columbia, SC
Registered: 2009-10-16
Posts: 142

Re: problem running sudo command in script

Oh, if you use my example, you might what to do %users instead of %wheel, but that shouldn't change anything.  (I just noticed you were trying users in your example).


To understand recursion, you must understand recursion.

Offline

#19 2009-11-16 00:35:17

clementis
Member
Registered: 2009-08-26
Posts: 37

Re: problem running sudo command in script

@Lewis: running the command in a terminal is *not* a problem - it works!

The problem is the autostart. The autostart does not work!

Any idea?

@anonymous user: yes I tried putting the command in rc.local but then I had problems booting the notebook. That means that the boot process didn't finish. It stopped while executing the command. I had to run a live cd to correct it.

Offline

#20 2009-11-16 00:45:21

llcawthorne
Member
From: Columbia, SC
Registered: 2009-10-16
Posts: 142

Re: problem running sudo command in script

Oh.  I figured it wasn't working from your log.desktop file because of the alias, and you wanted to know how to do it without a bash alias...  That's why my example was of redirection without an alias.

Hmm...  Pre-X...  What login manager do you use?

Since I use slim and it calls xinit, I just put things that I want to autostart in my ~/.xinitrc


To understand recursion, you must understand recursion.

Offline

Board footer

Powered by FluxBB