-2

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.

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

Milliways
  • 59,890
  • 31
  • 101
  • 209