-1

I am using .bashrc to execute a python script on system boot:

sudo -u pi python3 /path/to/script.py

and then I add @lxterminal to file /etc/xdg/lxsession/LXDE-pi/autostart to make sure a terminal window is opened on launch.

however, when I use ps aux to check all progresses, I found that there's two script.py processes running in the system, even though I call execute the script only once in .bashrc. Having two of the same script running at the same time is causing me troubles. Any help is appreciated.

yes ive tried with the other methods but couldn't get them to work. THe curious thing is that i have 5 pis all configured the same way, but one of them only executes the script once. Apparently it is only executing the script when the terminal is opened and not when the system boots. any ideas here? rc.local method never worked for me as nothing happened on startup

2 Answers2

3

There are different methods to start a script on bootup. Using ~/.bashrc is definitely a wrong method. It is only made to configure the bash shell when it is launched. So every time the bash shell is executed your python script will also start. Your script is started with /etc/xdg/lxsession/LXDE-pi/autostart and it is also started with the bash shell. So you have it running two times. It may be running some more times, depending on how often bash is started.

You can use a cron job or create a systemd service. Please search for start python script on boot.

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • yes ive tried with the other methods but couldn't get them to work. THe curious thing is that i have 5 pis all configured the same way, but one of them only executes the script once. Apparently it is only executing the script when the terminal is opened and not when the system boots. any ideas here? rc.local method never worked for me as nothing happened on startup – Michael Chang Sep 20 '20 at 09:22
  • Please edit your question and add the additional information to it. Don't use comments for it. As soon as you have deleted your dupplicate question, I will continue answering. – Ingo Sep 20 '20 at 09:31
  • sorry i used an guest account to post that question and have no idea how to log back into that guest account to delete the question – Michael Chang Sep 20 '20 at 09:40
0

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

Milliways
  • 59,890
  • 31
  • 101
  • 209
  • i have to use .bashrc. if you check my addition in the post, one of my 5 pis works fine, when it starts up, .bashrc contents are only executed once. – Michael Chang Sep 20 '20 at 09:55
  • 1
    You DO NOT "have to use .bashrc". No matter what you may have done it will cause problems. It DOES NOT run on boot. – Milliways Sep 20 '20 at 10:00
  • does it help to know that when i do ps aux, i see the scripts are executed once as pi and once as root? – Michael Chang Sep 20 '20 at 10:01
  • does it help to know that when i do ps aux, i see the scripts are executed once as pi and once as root? – Michael Chang Sep 20 '20 at 10:01
  • @MichaelChang It doesn't help anything if you use .bashrc. – Ingo Sep 20 '20 at 10:14
  • my python script is a continuous client that sends information to a server, the current bashrc + lxterminal combination allows to see the output of the program in realtime quite nicely. I just tried systemd, it did not work as well, thus i would like to stick to the bashrc method. also I can guarantee that only one terminal will be opened on the pis. thus bashrc's weaknesses really do not apply here i think. – Michael Chang Sep 20 '20 at 10:18
  • @MichaelChang Again - please don't use comments for additional information, add it to the question. – Ingo Sep 20 '20 at 10:29