![]() |
Need help with this command
I need some help with a little of python scripting. I'm trying to make my script to do this following.
Xterminal: root echo 78> /abc/abc/bca i've tried this but nothing work. If I run this from Xterminal as root: root python import os os.system('echo 78> /abc/abc/bca') it works! But if I run from script it doesnt. How can i get it to work? The file is not changing. How do I achieve this same effect with python? |
Re: Need help with this command
Anyone know where I did wrong?
|
Re: Need help with this command
You can't get root privileges inside the script: it is very insecure. You should write instead
Code:
import os Code:
open('/abc/abc/bca', 'w').write('78') |
Re: Need help with this command
Quote:
I've tried that, I'm trying to echo to swappiness os.system('echo 60> /proc/sys/vm/swappiness') You can't write to a file that only root can access. but result is not working via python. But it working via xterminal python as root. |
Re: Need help with this command
How do I echo this:
echo 60> /proc/sys/vm/swappiness in python as root? I've tried os.system('sudo echo 60> blah') os.system('root ech blah') but nothing worked |
Re: Need help with this command
Well, you are going to have to gainroot before launching the python script; if the python script is launched with root privs, then it should work.
Try: sudo gainroot python myscript.py edit: Then, if you need to be able to launch it from a non-root terminal; something you can't run "sudo gainroot" from first, you will need to add: ALL ALL= NOPASSWD: /path/to/python /path/to/myscript.py to the sudoers file. Then you can run "sudo python /path/to/myscript.py" with root privs, without having to enter your password. |
Re: Need help with this command
to start a script as root, you could use
Code:
echo /path/to/script | sudo gainroot (and just to mention it - there really is no point in using python if all you're doing are calls to os.system, better just use a shell script for that.) |
Re: Need help with this command
sh ./script.sh is faster to type than python ./script.py anyway ;)
|
Re: Need help with this command
Quote:
Code:
#!/bin/sh Code:
#!/usr/bin/python |
Re: Need help with this command
Quote:
Also knowing how to achieve this would also help my next script. |
Re: Need help with this command
#!/usr/bin/python
import gtk import gobject import hildondesktop import os import sys import stat class Swappiness(hildondesktop.StatusMenuItem): def __init__(self): hildondesktop.StatusMenuItem.__init__(self) #os.system('echo /usr/lib/hildon-desktop/Swappiness.py | root') os.system('sudo chmod +x /usr/lib/hildon-desktop/Swappiness.py') os.system('echo 30> /proc/sys/vm/swappiness') #os.system('sudo chmod -R echo 30 > /proc/sys/vm/swappiness') #swap_file = open('/proc/sys/vm/swappiness', 'w').write('30') icon_theme = gtk.icon_theme_get_default() pixbuf = icon_theme.load_icon("Swappiness", 22, gtk.ICON_LOOKUP_NO_SVG) # hide icon after 1 sec #gobject.timeout_add(1000, self.set_status_area_icon, None) self.set_status_area_icon(pixbuf) #label = gtk.Label("Show Me") #self.add(label) #self.show_all() hd_plugin_type = Swappiness This is my little simple script but nothing worked since I can't gain root access from the command os.system |
Re: Need help with this command
I want to make this work at reboot. And also place an Icon as well.
|
Re: Need help with this command
As said before, you can't gain root access inside the script. The script has to be run as root!
|
Re: Need help with this command
Quote:
|
All times are GMT. The time now is 22:19. |
vBulletin® Version 3.8.8