N00b
N00b

Reputation: 148

Why /etc/rc.local starts same Python script twice?

I start my script with rc.local like this:

sudo python3 /home/pi/myscript.py &   # &-mark because of while loop

In terminal I write

ps aux | grep "/home/pi"

It shows me:

python3 /home/pi/myscript.py         # <-- What is this?
sudo python3 /home/pi/myscript.py    # <-- rc.local 

If I remove that start line from rc.local file there are no scripts running then. Also user is root for both of them. Is it really running my script twice at the same time?

Upvotes: 0

Views: 1356

Answers (2)

Anya Shenanigans
Anya Shenanigans

Reputation: 94654

In this case; the line:

python3 /home/pi/myscript.py         # <-- What is this?

is the python command, as run by the sudo command and the line:

sudo python3 /home/pi/myscript.py    # <-- rc.local

is the sudo command, as invoked by rc.local.

Using ps -fe it also displays the parent pid of the processes, and from that it is easy to see that the python command is a child of the sudo command (using a sudo bash example):

$ ps -fe | grep bash
  UID   PID  PPID   C STIME   TTY           TIME CMD
    0 15095   481   0 10:18am ttys000    0:00.06 sudo bash
    0 15096 15095   0 10:18am ttys000    0:00.01 bash

so the parent of bash is pid 15095, which is the pid of the sudo command that invoked bash.

Because rc.local script is already run as root, the sudo is not needed, so to avoid the situation of seeing apparently multiple copies, you can omit the sudo in the script.

Upvotes: 1

N00b
N00b

Reputation: 148

Ok found out that rc.local does not need sudo because it is already running as root. So sudo is causing that.

sudo python3 /home/pi/myscript.py &   # &-mark because of while loop 

Has to be changed to that:

python3 /home/pi/myscript.py &   # &-mark because of while loop

Upvotes: 0

Related Questions