Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I need to run a python script in a terminal, myscript.py at startup (on Lubunt). This script requires root.

I've setup a .desktop file that runs the following command:

lxterminal --command="python /home/d/Jarvis/alarm.py && /bin/bash"

The terminal window opens at startup and runs the script, but then closes when the Python script returns an error (because it's not being run as root). When I change the Exec= to this...

lxterminal --command="sudo python /home/d/Jarvis/alarm.py && /bin/bash"

... (prefixing command with 'sudo') which works. However, the terminal opens on startup and displays the

[sudo] password for d: \

prompt, requiring me to input my password. I would like the execution of the python script at startup to be completely automatic with no user interaction.

How can I accomplish this?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

If you want it at login and not startup (as I don't see how LXTerminal can be opened without X server being up), you have to add an exception to the /etc/sudoers file so that you won't be prompted for your password.

To do this, run sudo visudo and then add the following:

<your username> ALL = NOPASSWD: /home/d/Jarvis/alarm.py

Make sure that you add this at the end of the file for this to work. I would also set the permissions of alarm.py to executable for this to work. So, do this to set it as executable:

chmod +x /home/d/Jarvis/alarm.py

Hope it helps!

share|improve this answer
    
This works! Thank you so much! I still have one issue... When I run "alarm.py" instead of "python alarm.py", I get 'import: not found' errors for core python modules (ex. datetime). I don't think I'm able to copy those modules into /Jarvis/, so what do I do? –  Brandon Oct 7 '12 at 21:20
    
@Brandon You might have to set the PYTHONPATH variable, but this is just a guess. For this, I would create a script called 'alarm_starter.sh' with the following contents: PYTHONPATH={what ever it should be} sudo /home/d/Jarvis/alarm.py. I'm really not sure about this, though. Maybe this will help: docs.python.org/tutorial/modules.html#the-module-search-path –  kroq-gar78 Oct 7 '12 at 22:24

Custom scripts that get executed on startup as root can be run via rc.local.

Edit /etc/rc.local with root rights:

sudo nano /etc/rc.local

and put the line

python /home/d/Jarvis/alarm.py

just before the last line, which should say exit 0. Reboot to see if it worked.

share|improve this answer

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.