skip to Main Content

I have a python3 script running on a Raspberry Pi on Debian 10 Buster, I can run in a terminal window but not on start up?

I am using the following line of code at the end of the file in /etc/profile

nohup python3 statlogger.py -db=stat_logger_db -sn=test

whilst the line of code works in the terminal window when I run it manually it will not work on starting the pi

I have also tried

nohup python3 statlogger.py & -db=stat_logger_db -sn=test

The python code needs to run at startup and then continuously whilst the pi is powered on, any thought

2

Answers


  1. I spent around 1 day and test 4 solutions for Linux/raspberry startup run script issues. Then here is the solution (I choose crontab)

    1. open crontab (use linux@crontab -e -> not use sudo crontab -e … thats could be error in future some access for script etc.)

    pi@ crontab -e

    Then choose nano and edit like this… It is tricky, ı found after test may be more than 10x times:

    @reboot cd /home/pi/beetool && /usr/bin/python3 run.py &
    

    My code is waiting for me under /home/pi/beetool. If you don’t change the directory that occurs error.

    And my code uses infinite loop so I have to finish with "&" as you see above.

    So I hope that helps someone, too …

    I aslo shared in: Run Terminal Commands on Raspberry Startup

    Login or Signup to reply.
  2. The best way I have found to run a program on Raspberry Pi at startup is to use the systemd files.

    1. Create A Unit File

    Open a new sample unit file using the following command:

    sudo nano /lib/systemd/system/sample.service
    
    1. Add the following text to the new file.(Remember to change the
      python file path to your file):
    [Unit]
    Description=My Sample Service
    After=multi-user.target
        
    [Service]
    Type=idle
    ExecStart=/usr/bin/python /home/pi/sample.py
        
    [Install]
    WantedBy=multi-user.target
    

    Save and exit using "CTRL+E" then when asked to save "Y" and "Enter"

    1. Set the permission of the file to 644 :
    sudo chmod 644 /lib/systemd/system/sample.service
    
    1. tell systemd to start it during the boot sequence using:
    sudo systemctl daemon-reload
    sudo systemctl enable sample.service
    
    1. Reboot the Pi using
    sudo reboot now
    

    source:
    https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/#systemd

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search