![]() |
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. |
All times are GMT. The time now is 17:18. |
vBulletin® Version 3.8.8