Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'd like to read small text file, located at: /sys/kernel/debug/vgaswitcheroo/switch without root permissions. It contains information about graphic cards (dedicated ATI Radeon and integrated Intel HD3000). I wrote a simple script displaying infomation I need, but it works only with root privileges. I already tried changing file & folder permissions, but when I restart my computer, the problem remains. Is it possible to permanently change that file permissions or owner?

Why do I need that? To bind that script with keyboard shortcut and get following Ubuntu 'dialog' after pressing key combination:

image

What is inside switch file? As I said before, it's just simple text file, with following content:

0:IGD:+:Pwr:0000:00:02.0

1:DIS: :Off:0000:01:00.0

My script status-radeon.sh

#!/bin/bash
CONTENT=`cat /sys/kernel/debug/vgaswitcheroo/switch | cut -d ':' -f 1-4 `
DISPLAY=:0 notify-send -t 4000 -i /home/Greg/Pictures/ati_radeon.png "GPU status" "$CONTENT"
echo "$CONTENT"

My /etc/sudoers.tmp content, without comments starting with #. It starts with nano editor, but I don't think it's relevant.

Defaults        env_reset                                             
Defaults        mail_badpass Defaults       
secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
root    ALL=(ALL:ALL) ALL 
Greg    ALL=(ALL:ALL)   ALL 
Greg    ALL= NOPASSWD: /home/Greg/Scripts/status-radeon.sh
share|improve this question

2 Answers 2

up vote 0 down vote accepted

Changing the permissions of system folders is not a good idea. Why don't you just setup a crontab for root that copies the file's contents somewhere else and have your script read from that?

$ sudo crontab -e

Add a line like this:

 */5 * * * * cp /sys/kernel/debug/vgaswitcheroo/switch /home/YOUR_USER/switch

Now, every time you reboot, that file will be copied to your home directory (remember to replace YOUR_USER with your actual username) and you can access it there.

share|improve this answer
    
I've added at the end of file line: @reboot cp /sys/kernel/debug/vgaswitcheroo/switch /home/Greg/Scripts/switch. After saving file and rebooting computer, no new file was added to /home/Greg/Scripts/. –  gogowitczak Nov 23 '12 at 15:38
    
@gogowitczak, not sure what's wrong here and I don't want to debug it in this question. Try posting a new question about why a crontab fails to run and I would be happy to help you there. Also, try adding a similar crontab with a simple commmand, for example @reboot ls /usr > /home/Greg/usr_contents and check if that is executed after reboot. –  terdon Nov 23 '12 at 16:15
    
@gogowitczak, see updated answer. –  terdon Nov 23 '12 at 18:08
    
When I ran script with sudo, it works fine (see screenshot). After adding your modifications, when I execute script ./status-radeon.sh, it prompts for root password. –  gogowitczak Nov 23 '12 at 19:14
    
@gogowitczak, sorry, my updated answer is just wrong (and will be deleted), I tried it on my machine and it worked because I still had root priviledges (idiot). Maybe the problem with cron is that it is running before the file is created. Try replacing the @reboot line with */5 * * * * cp /sys/kernel/debug/vgaswitcheroo/switch /home/YOUR_USER/switch to make it run every 5 minutes. –  terdon Nov 23 '12 at 19:27

You can use sudo to allow some users (e.g., you) to run the script as root

$ visudo

and then add

username  ALL= NOPASSWD: /path/to/your/script

For a short introduction to sudo: http://www.sudo.ws/sudo/intro.html

share|improve this answer
    
It's not working. I've added at the end of file: Greg ALL=NOPASSWD: /home/Greg/Scripts/status-radeon.sh and rebooted my machine. –  gogowitczak Nov 23 '12 at 15:23
    
No need to reboot. And what do you mean by "it's not working". Any error? Which system are you using? BTW there is a space between the = and NOPASSWD: –  Matteo Nov 23 '12 at 15:25
    
@Matteo: I've added the missing space. Error: cat: /sys/kernel/debug/vgaswitcheroo/switch: Access denied. I'm using Ubuntu 12.10 x64. @terdon: I've ran successfully sudo chmod 4755 status-radeon.sh, still not working. –  gogowitczak Nov 23 '12 at 16:33
    
@gogowitczak, hmm,I think we are falling afoul of the XY Problem. What exactly are you trying to do here? Why do you need to read that file? There should be a workaround... –  terdon Nov 23 '12 at 16:51
    
@terdon - I've added a detailed info to original post. –  gogowitczak Nov 23 '12 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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