0

I need to set an environment variable and run an app when the Raspberry Pi boots up. To that end, I added the following to /home/pi/.bashrc:

export MY_ENV_VAR=foo
cd /home/pi/MyApp
./MyApp

When I reboot the Pi, the app is definitely running and the environment variable is set. However, when I log in, the application is executed again - e.g. .bashrc is run on both login and boot.

How can I ensure that my script is only executed once at boot time and not at login?

P.S. I've tried copying the script /etc/rc.local, but then it doesn't run at all.

3 Answers 3

4
  1. .bashrc runs on every interactive shell launch. - It does not run when the RPi is booted (unless you have "changed something").

  2. If your app runs after booting, and before you launch an interactive bash shell (this would include an SSH connection), then that is because it is being started somewhere else.

  3. "Somewhere else" could be cron - under a user or the root crontabs, or it could be systemd (read man systemctl, and try systemctl list-unit-files).

  4. Your statement: I've tried copying the script /etc/rc.local, but then it doesn't run at all. suggests that you are launching an interactive shell without being aware you are doing so.

Absent any other information, I'd have to guess that the solution to your problem is in two steps:

  1. Remove the script from ~/.bashrc
  2. Create a cron job under the @reboot facility

FWIW:

ADDENDUM

To address the additional questions in the comments:

  1. Your crontab addition should be like this:

    @reboot sleep 10; /home/pi/MyApp >> /home/pi/myappcronlog.txt 2>&1
    
  2. If your environment variable needs to be system-wide and persistent, you should put it in /etc/environment (i.e. open this file in your editor & add MY_ENV_VAR=foo... it may be an empty file before you edit it)

  3. If your environment variable is used only in your MyApp script, and run from the crontab, you may either follow 2. above, OR add the following line above the line in 1.:

    MY_ENV_VAR=foo
    

My recommendation is 2.

6
  • Is this the correct method of doing this? @reboot export MY_ENV_VAR=foo && cd /home/pi/MyApp && ./MyApp Aug 9, 2021 at 5:54
  • No. I understood from your question that running ~/MyApp was all that was needed. Setting an environment variable is a different thing. Do you want the environment variable to be global (system-wide), or just during execution of your program?
    – Seamus
    Aug 9, 2021 at 6:23
  • @AngryHacker: No reply, and so I've edited my answer - hope this covers it - I'm off duty for a while now :)
    – Seamus
    Aug 9, 2021 at 7:03
  • Thanks for the addendum. The app also must run from its directory. How do I finagle that? Finally, I'd like the app to run under the pi user rather than root. Is that as simple as running crontab as sudo crontab -u pi -e? Aug 9, 2021 at 15:25
  • @AngryHacker: This is getting well beyond your original question. Perhaps consider creating another question. Also, I'm not clear at all wrt your comment that I'd like the app to run under the pi user rather than root?!? Unless I missed something you have never mentioned running as root.
    – Seamus
    Aug 9, 2021 at 18:12
3

.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.

It DOES NOT run when the Raspberry Pi boots up.

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.

There is no simple answer - it depends on what the script does and needs.

1
  • Not sure if my configuration is odd in some way, but .bashrc definitely does run. I know this because the app runs a website and I can access it after doing a reboot without logging in. Aug 8, 2021 at 18:31
2

I would think .bashrc only runs at login. It should not run at boot.

Personally I would add the command to the Pi crontab if you want to run it at boot (using @reboot for the time setting).

man 5 crontab

1
  • It definitely runs on boot. The reason I know is that the app runs a website. I can hit it no problem after rebooting it. Aug 8, 2021 at 18:30

Your Answer

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

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