Running a startup program in terminal with sudo

14,696

Solution 1

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!

Solution 2

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:
14,696

Related videos on Youtube

Brandon
Author by

Brandon

Updated on September 18, 2022

Comments

  • Brandon
    Brandon over 1 year

    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?

  • Brandon
    Brandon over 11 years
    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?
  • kroq-gar78
    kroq-gar78 over 11 years
    @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
  • Daniel Harris
    Daniel Harris over 6 years
    This solved my problem for autostarting programs on Raspbian.