0

I've had success with many ways of running a python script at boot on a Raspberry Pi (rc.local, cronjob, etc). However, how could I have the pi boot into the text console mode (not GUI), and automatically start running a python script?

The reason for this is to have a python script that runs indefinitely and accepts text input; however, the pi will be running headless (just a keyboard attached, no mouse or monitor). I wish to boot into the text console and run from there so that there is no worries of the correct terminal not being active, etc.

Just putting the command in rc.local resulted in the modules for the python script not being found (But the script runs fine if I manually run it after boot is completed).

  • Install python stuff with sudo pip ... or sudo pip3 ... and they're available for all users. rc.local is fully supported on DebIan systems with systemd. – Dougie Sep 16 at 9:49
1

Most current OS like, for example, Raspbian use the systemd init system by default. Assuming you have an OS with systemd (check you have it installed with command systemd --version) you can easily create a service that runs your python script at startup.

  1. Change your settings to have the RPi boot into text console mode instead of GUI
    sudo systemctl set-default multi-user.target and reboot
    (you can change back to GUI anytime by systemctl set-default graphical.target and reboot)
  2. Create a service that starts your python script at start-up. To do so, execute the following steps as sudo user:
    • create a systemd unit file in /etc/systemd/system/ like this for example
      nano /etc/systemd/system/my-startup-service.service
    • my-startup-service.service is a text file which should look something like this:

[Unit]
Description=start-python-script Service
After=multi-user.target
Conflicts=getty@tty1.service

[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/my_python_script.py
StandardInput=tty-force
StandardOutput=inherit
StandardError=inherit

[Install]
WantedBy=multi-user.target   
  1. Run sudo reboot (that's the easiest among several ways to start the service). After the first reboot, the service is enabled and will start your python script automatically each time you boot up your RPi. Consult systemctl man pages to learn how to disable and re-enable the service if needed.

Further comments:

  • The paths to your python executable and your python script in the ExecStart= directive might differ from those in the example.
  • My example was inspired by this blog post.
  • I haven't done any detailed tests regarding the StandardInput= directive. You can find more about tty-force and related options here.
0

Yes, rc.local runs things a little differently than the terminal command line. (Don't ask me how, I don't know. But I get file not found errors and such.)

Try pasting the full path to the python script into /home/pi/.profile.

Then use sudo raspi-config to boot to CLI instead of desktop.

0

I use the .bashrc file in my home directory.

cd ~

vim .bashrc

Go to the bottom of the file and add the command that would normally launch your Python script.

Now whenever you launch a shell it will also execute the Python script. I use .bashrc to get neofetch to run automatically in my console when launch it.

  • That doesn't answer the question of how to start that shell. It also means that every shell will start the program. – RalfFriedl Sep 16 at 18:10
0

If you want to boot into CLI mode there is a Boot link in raspi-config. I run most of my Pi this way, but require login.

If you want to run headless you should select the Console Autologin

Putting commands in rc.local is obsolete SysV procedure, and may or may not work under systemd depending on the script. In any event rc.local runs as root and has no environment or Console input so this is not what you want.
See https://www.raspberrypi.org/documentation/linux/usage/rc-local.md

It is difficult to imaging a script that accepts commands, but has no output! Your best option would be a cron which runs as user pi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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