Eichhorn18
Posts: 55
Joined: Wed Jan 17, 2018 9:52 am

rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 12:10 pm

Hi,

I am running Stretch Desktop on the Pi 3B. I am trying to run a .sh script at startup to do the following commands, including running a python script:

Code: Select all

#!/bin/bash
sudo mount -a
python3 /home/.../python_script.py
The above mounts a network drive for which most of the commands to do that mount are stored in /etc/fstab. Then the python script should run, which among other things takes photos and saves them to the network mount. The python script works independently of trying to get it running through rs.local.

My issue is that this method of modifying /etc/rs.local does not work for me. I can't figure out why. I also tried a method reported online that makes an executable .sh using chmod +x and then adding that file to /etc/init.d/script.sh. This did not work either.

Any idea what I could be doing wrong?

The official Raspberry Pi documentation (https://www.raspberrypi.org/documentati ... c-local.md) on how to do this, which is what I have described above--does not work.

Thanks,

Aydan
Posts: 731
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 12:47 pm

If you want to stay with the rc.local method, then add am "&" to the end of the line, so that the script executes in the background.
The other variant is to set up a systemd service (type simple), which is the preferred variant.

Eichhorn18
Posts: 55
Joined: Wed Jan 17, 2018 9:52 am

Re: rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 1:19 pm

Aydan wrote:
Thu Nov 29, 2018 12:47 pm
If you want to stay with the rc.local method, then add am "&" to the end of the line, so that the script executes in the background.
The other variant is to set up a systemd service (type simple), which is the preferred variant.
Thanks for your reply. So I do have the & at the end of the line for running the python script on rc.local. My rc.local file looks like the following (omitting the commented lines at the start of the file)

Code: Select all

_IP=$(hostname -I)||true
if [ "$_IP" ]; then
	printf "My IP address is %s\n" "$_IP"
fi
mount -a
python3 /home/pi/Desktop/folder/RPi_basic_script.py &
exit 0
Is something wrong with the above? Should I be able to run the mount command like this or do I need sudo in the rc.local file to do this mount?

Thanks

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 1:35 pm

The first and most obvious question is: What makes you think it is not working? I.e., what are the symptoms?

The second thing is to add the following line to your /etc/rc.local file, just before the # Print the IP address line:

Code: Select all

exec > /tmp/rc-local.out 2>&1;set -x
Now, reboot and take a look at the file (/tmp/rc.local). It should then become clear what happened. You will be on the road to debugging whatever is wrong.

Third, it would be better to not use the fstab file for your mounting incantations. This is true for several reasons, notably the fact that lots of forum-posters have gotten themselves into trouble by doing so - if you make any errors in the fstab file, you can end up with an unbootable system.

Since you are doing this via a script anyway, you might as well just include any needed options/etc on the mount command line in the script. Much more clean. Also, using "mount -a" looks wrong on principle, since that command is almost never executed by the end user; rather, it is part of the system startup voodoo.

(Yes, I'm saying that the commands in /etc/rc.local are, essentially, commands executed by the "end user")
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

Eichhorn18
Posts: 55
Joined: Wed Jan 17, 2018 9:52 am

Re: rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 1:59 pm

n67 wrote:
Thu Nov 29, 2018 1:35 pm
The first and most obvious question is: What makes you think it is not working? I.e., what are the symptoms?
The python script should capture photos and save them to the network mount. So the symptom is that the photos are not appearing on the network folder. The python script works fine otherwise and as it should if run in a terminal by the end user.
Third, it would be better to not use the fstab file for your mounting incantations. This is true for several reasons, notably the fact that lots of forum-posters have gotten themselves into trouble by doing so - if you make any errors in the fstab file, you can end up with an unbootable system.
I have not heard this before. The mount script I've written in fstab has worked just fine so far.
Since you are doing this via a script anyway, you might as well just include any needed options/etc on the mount command line in the script. Much more clean. Also, using "mount -a" looks wrong on principle, since that command is almost never executed by the end user; rather, it is part of the system startup voodoo.
Do you mean I should simply move the mount commands into the shell script instead of fstab? Would the syntax to execute the mount using cifs be any different if I did this?

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 2:11 pm

1) You probably need a more significant indicator of its "not working" than that. Have you tried the "ps" command?

2) Number 2 in my previous list is the most important. Have you tried this?

3) The syntax on the mount command line is a little different, but the content is the same. Something like:

mount -o someoption -o anotheroption -o etc //myserver/myshare /mnt/whatever

Finally, now that I know that the thing you are mounting is a "network drive", it is probably the case that the underlying problem is that the network isn't "up" at the time when the mount is attempted. This would have come to light had you followed the advice in "Number 2 in my previous list".

There is an option somewhere in the systemd/raspi-config stuff to "wait for the network" (or something like that) as part of the boot process. Or, you could solve it in user code (what I usually do).
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

User avatar
thagrol
Posts: 3488
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: rc.local , auto run .sh script at boot in Stretch Desktop

Thu Nov 29, 2018 6:55 pm

n67 wrote:
Thu Nov 29, 2018 2:11 pm
3) The syntax on the mount command line is a little different, but the content is the same. Something like:

mount -o someoption -o anotheroption -o etc //myserver/myshare /mnt/whatever
This

Code: Select all

mount -o someoptions,anotheroption,etc //mysevre/myshare /mount/point
also works.
Finally, now that I know that the thing you are mounting is a "network drive", it is probably the case that the underlying problem is that the network isn't "up" at the time when the mount is attempted. This would have come to light had you followed the advice in "Number 2 in my previous list".

There is an option somewhere in the systemd/raspi-config stuff to "wait for the network" (or something like that) as part of the boot process. Or, you could solve it in user code (what I usually do).
It's in raspi-config under boot options. It might also be worth adding _netdev to the options present in /etc/fstab for the mount.

With the network share unmounted, have a look under the mount point to see if your python script has wriiten anything there. If permissions don't block it, your script will happily write to the mount point whether or not anything is mounted there. It'll do this if the mount gets removed underneath it too.

You can use os.path.ismount('/mount/point') (https://docs.python.org/2/library/os.path.html python 3: https://docs.python.org/3/library/os.path.html) to have your script check and only write if the share is mounted. Or have you script attempt the mount.
Arguing with strangers on the internet since 1993.

Return to “Troubleshooting”