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:


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


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

My script status-radeon.sh

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       
root    ALL=(ALL:ALL) ALL 
Greg    ALL=(ALL:ALL)   ALL 
Greg    ALL= NOPASSWD: /home/Greg/Scripts/status-radeon.sh

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.

| 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/. – Greg Witczak 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. – Greg Witczak 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

| 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. – Greg Witczak 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. – Greg Witczak 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. – Greg Witczak Nov 23 '12 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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