Results 1 to 5 of 5

Thread: Run Python Script as start up - sudo

  1. #1
    Join Date
    Apr 2009
    Location
    Surrey, London
    Beans
    291
    Distro
    Ubuntu

    Run Python Script as start up - sudo

    Hi all,

    I am very new to Python scripting and i am starting to have a play as i have a Raspberry PI.

    I have written a simple script that makes some LEDs flash. I am looking how i can get this to run at startup (so i dont have to manually run the script)

    Currently to run the script i run the following command:

    'sudo python blink2.py'

    is there a way this can be done ? it has to run a sudo/root.....

    Many Thanks
    Ubuntu 18.04 VMs

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Run Python Script as start up - sudo

    Hi elliotbeken.

    You could use the root crontab, and use the @reboot custom tag:
    Code:
    sudo crontab -e
    And then add something like:
    Code:
    @reboot /path/to/script.py
    Hope it helps. Let us know how it goes.
    Regards.

  3. #3
    Join Date
    Apr 2009
    Location
    Surrey, London
    Beans
    291
    Distro
    Ubuntu

    Re: Run Python Script as start up - sudo

    Hi papibe,

    Nope this didnt work !

    I added this:

    @reboot /home/elliot/blink3.py

    any ideas ?

    Thanks
    Ubuntu 18.04 VMs

  4. #4
    Join Date
    Apr 2009
    Location
    Surrey, London
    Beans
    291
    Distro
    Ubuntu

    Re: Run Python Script as start up - sudo

    Managed to work this out ! Quite simple really placed this in the rc.local

    python /home/elliot/blink3.py
    Ubuntu 18.04 VMs

  5. #5
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Run Python Script as start up - sudo

    Glad you worked it out

    I few tips:

    Since this file is a little 'touchy', I'd recommend:
    • Using full paths.
    • Force a 'positive ending'.
    • Assume minimal environment on your script (some environment variables may not be available).
    • Don't forget the 'exit 0' line.

    So the last line would be something like:
    Code:
    /usr/bin/python /home/elliot/blink3.py || /bin/true
    exit 0
    Regards.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •