I have a python script that reboots the device at the end of the script. I got it working fine on my user account by giving the user rights to /sbin/shutdown in sudoers file. But the problem is that when I try to automate it with the user's crontab, I get the following error:
sudo: no tty present and no askpass program specified
Any ideas how to solve the problem? I thought the user's crontab would have same rights as the user, but it doesn't seem that way.
I can't use root crontab because part of the python script uses Selenium with geckodriver and geckodriver cannot be ran as root.
Here's the reboot part of the python script:
def restart():
command = "/usr/bin/sudo /sbin/shutdown -r now"
import subprocess
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
print output
I'm using Ubuntu 18.10
I managed to solve the issue. If someone is having similar problems, try editing the sudoers file so that your user ALL=NOPASSWD: /sbin/shutdown is the last line in the file so it takes priority over conflicting lines.
Related
I am trying to schedule python script through crontab. Functionality of python script is svn cleanup and update commands. When i manually try python script, it is working as expected. Same script scheduled through crontab it is working but not getting proper result (result will be same as of last python script output, i.e when i manually run python script that will be output eventhough there are checkin's)
Below is python script:
subprocess.call("/usr/bin/svn cleanup "+path,shell=True)
subprocess.call("/usr/bin/svn update "+path,shell=True)
subprocess.call("/usr/bin/svn log --username usrname --password
pass1 "+path+">filename.txt",shell=True)
Below is command which is used in crontab,
/usr/bin/python /path_to_script/filename.py
Please help me on this.
Thanks
I want to change the IP address of the machine continuously, SO after a lot of search i got this useful link https://github.com/jotyGill/openpyn-nordvpn . I have done the steps as per this URL and i tried the steps in terminal window(Mac) and it is successfully changing the IP.
But the issue is that need to manually run the script again to change the IP, So thought of automating it.
So what i want to do is ,
1. Open an terminal window in mac using Python and execute the following commands,
Enter this command :
sudo openpyn --init
it will prompt to enter password and i have to enter password manually,Is there a way where the program can do this password input automatically using subprocess or any other methods in Python?
After the user name and password is entered ,Need to enter the below command.
openpyn US --area “area”
This will change the IP and need to do the same again.While doing the same either we need to do it from the start or pass CTRL+C to end the current execution and pass
" openpyn US --area “area” "this command again.
I'm trying to execute few statements in terminal window from a python program.
These are the things i have done and no luck .Can anyone please guide me on how to do this?
import subprocess
import os
pid = os.fork()
os.system("""osascript -e 'tell application "Terminal" to do script "sudo openpyn --init;openpyn US --area “area”;os.kill(pid, signal.SIGINT)"'""")
subprocess.call(["sudo openpyn --init"], shell=True)
os.system("sudo openpyn --init")
I don't usually use mac but this might work:
import os
import time
os.system(Command)
time.sleep(0.25)
os.system(password)
Im trying to get python to run a terminal command which will change my desktop wallpaper. Running this command in a normal terminal will change my wallpaper. However when i try to make python run this command in terminal it doesnt work and gives me an error.
I can replicate this error when i open a terminal as root user. I logged in using su and typed my password. Typing in the command then gives me an error and it does not execute. The same when i try to execute the command with python using either the modules os or subprocess.
Is it because my command to change the wallpaper which starts with pcmanfm (which is the window manager) has probblems with its path and when i am root user the path is changed?
How can i make python open a terminal as "normal" user in my case "pi" on my raspberry pi, and run a command in it?
this is the terminal command which changes my desktop wallpaper:
pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg
this is what happens when i run my python code to open a terminal:
root#raspberrypi:/home/pi/Desktop#
instead of normally when it works:
pi#raspberrypi:~ $
here is my python code which is meant to open a new terminal with the command which changes my desktop wallpaper, however i end up as ROOT user as described above and get an error and nothing happens:
import os,random
import subprocess as sub
sub.call('lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"', shell=True)
executing the following from a normal terminal works perfectly:
lxterminal -e bash -c "pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/bridge.jpg; sleep 3;exec bash"
this is the result after running the python code, which is the error i always get:
** Message: 12:11:08.734: x-terminal-emulator has very limited support, consider choose another terminal
root#raspberrypi:/home/pi/Desktop#
So how can i make python open a terminal as user "pi" not as root?
or is this not the problem?
thanks!
The problem was calling the python script as sudo.
Opening the python script normally as:
Python3 script.py
Will open a terminal as a normal user and. change my desktop wallpaper,
Whereas sudo python3 script.py opens the terminal as root user and then the command to change wallpaper no longer works.
Ok, so when I run this command:
sudo python3.5 /path/to/my/script.py
It executes fine and returns:
select user:
Guest
My username
Choose the account that needs cleaning up (enter corresponding number):
After which I enter "2", press enter and the script runs great.
However, when I run:
osascript -e 'do shell script "python3.5 /Path/to/my/script.py" with administrator privileges'
I get the expected pop-up screen in which to enter my sudo-password, but afterwards it throws out the error:
OSError: [Errno 9] Bad file descriptor (1)needs cleaning up (enter
corresponding number):")
Anyone has any idea on this? Any help would be greatly appreciated!
PS; some background as to why I need this method --> I am using a pre-script (also written in python) from which myscript.py needs to be executed. My prescript.py does not need administrator privileges. Sometimes however, it will be required to call myscript.py which does need administrative privileges. Since I do not want to hard-code my sudo-password into any of my scripts I want to get a pop-up box asking me for it. I figured, telling my pre-script.py to run subprocess.Popen([]) on the osascript command I mentioned above would do the trick. However, for that, I first need to get the osascript command to work properly.
Ah, so I figured this one out myself. As expected it had to do with privileges. Somehow "do shell script" is run via other privileges then just entering the command in terminal. And "sudo command" in terminal does not equal (fully) to "do shell script [script] with administrator privileges".
For me, what fixed it, was navigating to:
System Preferences --> Security and Privacy --> Privacy tab --> Accessibility
And then dragging Python.app and Terminal.app to that window (you might need to unlock it first by pressing on the little lock in the lower-left corner of that window).
I wrote python script that uses subprocess.pOpen() module to run and manipulate with 2 GUI programs: Firefox and VLC player. I am using Ubuntu 14.04 LTS operating system in Desktop mode.
My problem is when I try to run that python script when system starts, script is running but Firefox or VLC don't start.
So far, I tried to make shell script to run my python script and then with crontab with #reboot /home/user/startup.sh to execute my python script. I set all permissions for every script that is using. I gave my user root permisions so everything is OK with that.
I also tried to run my script putting command "sudo python /path/to/my/script.py" in /etc/rc.local file but that also is not helping.
I googled and found out people using .desktop files that they put in ~/.config/autostart/ directory but that also failed. Example of what I wrote:
[Desktop Entry]
Type=Application
Exec="sudo python /home/user/path_to_my_script/my_script.py"
X-GNOME-Autostart-enabled=true
Name=screensplayer
Comment=screensplayer
And I saved this as program.desktop in ~/.config/autostart/ directory but it does not work. I am sure there is a way to fix this but don't know how. Any help will be appreciated!
Found solution to my problem. When you are running commands with pOpen in python like this:
FNULL = open(os.devnull, 'w')
_FIREFOX_START = ["sudo", "firefox", "test.com/index.html"]
subprocess.Popen(self._FIREFOX_START, stdout=self.FNULL, stderr=subprocess.STDOUT)
it won't run apps because of "sudo" word, when I removed it, it worked.
Also run gnome-session-properties in terminal and add new startup application, be aware that you have to execute python script without sudo, like this:
python /home/user/path_to_script/script.py
Also, I granted my user root privileges so kepp that in mind.