Raspberry Pi SBCs (single-board computers) are popular devices for a wide variety of uses. There are many instances where you want a process to run every time it boots up; the last thing you want to have to do is start it yourself. The good news is, you don't need to.
Three ways to start a program on boot are explained here, with slightly different uses. The crontab method is especially useful, as it not only starts a program upon bootup, but it can also run things at a set interval after bootup. Let's look at three methods to set up your programs to run automatically via the command line or SSH (Secure Shell).
Testing Setup
Let's assume you're working from the command line, or logging into your Pi via SSH.
For this setup, install an LED on BCM pin 14 (aka physical pin 8 for testing) directly or via a transistor. I've written test programs similar to the blink.py routine below, which blinks the LED twice. This is accomplished by navigating to the home directory (cd /home if needed) then typing sudo nano blink.py. Enter the program below and hit ctrl+x to exit and save.
To test your setup, add lines as shown to the bottom of the text file. * * * * * will run a command every minute, which in my case is the test program: python3 /home/blink.py. Additionally, cron can run a program every time on boot by using @reboot instead of numbers or stars for the time. The @reboot python3 /home/blink3.py command will run a slightly modified blink3.py program (which blinks three times so you can tell which routine is running) each time it reboots.
bashrc Raspberry Pi
While not as versatile as crontab, bashrc has a unique feature — it runs on boot and every time a new terminal is opened or you log on via SSH. That can be useful in some situations. To try it out, type in sudo nano /home/pi/.bashrc. You'll be taken to a text editor containing a variety of commands.
On a new line at the bottom of the file, simply add your commands, such as python3 /home/blink3.py. There's no need for anything else; just hit ctrl+x to exit, and y then enter to save the file. Restart your Pi with sudo reboot now then log in to see the results. Note that this script won't run until you log in, which means you must enter your password and username on SSH or via the terminal; however, it will automatically occur if booting into the desktop environment.
Test your script out by entering source ~/.bashrc to run the file. You can even add echo commands to confirm that the routine is run on your terminal, as shown in the above images.
Raspberry Pi AutoStart rc.local
Similar in function to bashrc, you can add functions to rc.local to have them run on bootup. Note that rc.local isn't dependent upon you logging in. To edit, enter sudo nano /etc/rc.local into the terminal, then insert the program you want to run before the exit 0 text. As seen below, I've entered python3 /home/blink3.py &. The ampersand here allows other processes to execute while the program is executing. If your rc.local script gets stuck, your boot sequence will not proceed, so be cautious about how you implement this functionality.
Conclusion
Raspberry Pi systems, and Linux in general, give you a variety of ways to automate running programs. Whether you want to turn on a light at a certain time, back up your hard drive every night, or run a heavily modified plant-watering Keurig, you can get your trusty Pi-based assistant to handle it for you!