0

Im programming on a Raspberry Pi 4 and trying to make a python script start automatically, when powering on. The python script is working and has no errors.

I tried to insert a starting command in /.bashrc and the same in lxterminal. I used following command:

python3 /path/to/script I also tried to open it via crontab.

The /.bashrc method is working partly. The script is only starting automatically, when im opening the terminal. I want it to open automatically, when im powering my raspberry pi 4 on.

5
  • have a look at e,g, github.com/thagrol/Guides (the 'boot' guide)
    – Dirk
    Nov 10, 2023 at 9:45
  • @Dirk frankly a brief glance indicates major issues "# ~/.bashrc: executed by bash(1) for non-login shells" - the "guide"claims it runs on "every login" It is NOT designed to run scripts but to configure bash.
    – Milliways
    Nov 10, 2023 at 10:48
  • @Dirk thanks du macher. It was very helpful
    – En99
    Nov 10, 2023 at 10:51
  • 1
    @Milliways if you have any comments etc please open an issue on his github repo. I still think his guides are generally correct and well written
    – Dirk
    Nov 10, 2023 at 13:34
  • @Dirk I don't feel obliged to correct all the erroneous information on the web about the Pi (there aren't enough hours in the day) although I do note erroneous or misleading answers on this site. This isn't even Pi specific - it is standard Linux. This has been asked hundreds of times - although it would be good if someone wrote a canonical answer. The Pi site used to have a good section on boot until all general Linux stuff was deleted in the great documentation purge. The first line of .bashrc says it won't run on login (still less on boot).
    – Milliways
    Nov 10, 2023 at 22:41

2 Answers 2

1

If I understand your issue, you want to start your Python script when your RPi starts (boots) - not when you log in... is that correct?

If so, then the crontab is a proper way to do that. This would be easier to figure out if you'd edit your question to add the job (line) you put in your crontab. But we'll give it a go, and create a "job" in crontab from scratch.

Begin by opening your user crontab from the command line of your terminal window:

crontab -e

Assuming that you're using Raspberry Pi OS, and you've not changed the defaults, your default editor (nano) should open with a copy of your current crontab file. I'll assume you're familiar enough with your editor to follow along...

Add a new line to the bottom of your editor window/screen with the following:

@reboot /path/to/script >> /home/pi/myscriptlog.txt 2>&1

What does this do?

  1. When your machine boots up, this job in your crontab will run (@reboot)

  2. The cron daemon (service) will attempt to execute whatever file is located at /path/to/script. (NOTE be sure this file is set to be executable; use chmod to set the executable bits if necessary)

  3. Since the cron job cannot communicate with you through the terminal, we redirect (>>) the output to a file in your home directory (here assumed to be /home/pi).

  4. The 2>&1 bit at the end of the job is another redirect, but this one tells cron to redirect any error messages thrown by your script (i.e. stderr = 2) into the stream with stdout (i.e. stdout = 1).

Try that, and let us know how it works - or, if this answer completely misses the point of your question, please let me know, and I'll delete the answer.

0

A real easy way to to start a program is to use autostart. Make a directory in .config name autostart; mkdir .config/autostart. In that directory make a file in desktop format:

[Desktop Entry]
Type=Application
Name=Clock               # your name
Exec=/home/pi/bin/dclk2  # your full path program

Also see [https://forums.raspberrypi.com/viewtopic.php?t=294014]

1
  • On your Python program, it can start with '#!/usr/bin/env python3' and make it executable with 'chmod +x pgmname'
    – bstipe
    Nov 11, 2023 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.