Running a startup program in terminal with sudo
I need to run a python script in a terminal, myscript.py at startup (on Lubunt). This script requires root.
I've set up 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?
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.
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!