0

I added a line at the end of .bashrc to start a python program as I start up raspberry.

However the program is not automatically executed and it runs only after openning terminal.

So I would like to make the program to be run without having to open terminal.

Is there anybody here with a suggestion?

My configuration is not raspberry connected to computer and running by serial

But Raspberry connected directly to keyboard, mouse and monitor.

0

2 Answers 2

4

You shouldn't put scripts into .bashrc or .profile. This files are executed when you login in console every time, not when system startup. It could hang your login session, and make raspberry pi unable to log in to. And of course can be executet multiple times, one for each console session.

To run script at startup, when the script is not intended to run all the time, I suggest to use corntab:

# edit crontab config
$ crontab -e

# add line in crontab config file
@reboot /path/to/the/script arguments
# save

To run script that must start with startup and run all the time raspberry pi is running, you should create service. This way is much more complicated, but gives you control what and when is running, especially when you haven't direct access to raspberry pi.

5
  • Actually they are not run when you login, unless one references the other; see INVOCATION in man bash. .profile is executed for interactive login shells, .bashrc is executed for interactive non-login shells, and neither is executed for non-interactive shells.
    – goldilocks
    Commented May 10, 2018 at 14:01
  • In my case I am willing to create a program to run all the times at start up so I might need to create a service,
    – pb77
    Commented May 15, 2018 at 0:57
  • It can be done in many ways. From writing daemon on your own in python, by using special programs that make your script running all the time raspberrypi.stackexchange.com/questions/5371/…, to the combination of tmux and some service configuration, which is my prefered because of simplicity raspberrypi.stackexchange.com/questions/79926/… Commented May 15, 2018 at 6:42
  • Cron worked for a simple .py file but for my main purpose. Made a new question here: raspberrypi.stackexchange.com/questions/83968/… could you check please when you have time?
    – pb77
    Commented May 16, 2018 at 7:44
  • what is corntab? Is it a kind of cereal? Commented Jan 25, 2020 at 16:39
1

.bashrc is NOT intended to run scripts.

It is run each time a non-login interactive shell is started and is used to configure the shell.
~/.bashrc: executed by bash(1) for non-login shells.

There are may ways of running scripts, depending on what you are trying to do. See https://raspberrypi.stackexchange.com/a/47537/8697 for an example.

1
  • Actually it is run when a non-login interactive shell is run, and it is not run for non-interactive shells, which is why it doesn't run at boot (unless referenced from profile); all the shells are either non-interactive, or logins.
    – goldilocks
    Commented May 10, 2018 at 13:58

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