0

In some post here, I read that if I wanted to run a shell script at start up, I would have to add it at the end of /etc/rc.local but before exit 0. However, when I do this it seems to have no effect. Specifically, I am using my Pi3 as a Access Point and for some reason, I always have to sudo service hostapd restart after boot to make it work. Adding this line to /etc/rc.local does not have the same effect. Where can I add this line so it really only executes after everything else has loaded?

Here are the contents of /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo service hostapd restart

exit 0
1
  • post the content of the rc.local file to show if there is something wrong there
    – Luis Diaz
    Commented Sep 19, 2017 at 14:14

2 Answers 2

3

If those articles you're using predate systemd, be aware that rc.local compatibility is provided via a systemd service. On a fresh install, you will see a "Start /etc/rc.local Compatability" boot message if you're running newer versions of raspbian.

After modifying /etc/rc.local, check the status of the service with sudo systemctl status rc-local. You may see a warning that your must run sudo systemctl daemon-reload.

Depending on what you run in /etc/rc.local, the startup order may not be correct. In this case, if you need to ensure the order of startup (e.g. hostapd only after networking is fully up), then creating a proper systemd service will be required.

Note that some of this may vary depending on whether you upgraded to stretch or did a fresh install, or on what else you've installed.

This article on U&L SE may be useful for replacing /etc/rc.local with a systemd equivalent. There is a good discussion on the RPi forums noting that it is executed, but early in the boot process, likely before networking is available.

In short, you want to create and enable a systemd service file to launch things in the proper sequence during boot.

8
  • I don't quite understand the discussions in the links you provided. I feel like there should be a simple way of running a command at startup after everything else has loaded. It shouldn't be more complicated than adding a line somewhere. What am I missing? Commented Sep 19, 2017 at 20:48
  • In short: /etc/rc.local and @reboot in crontab still exist, but their ordering can be a problem with the new systemd scheme. Work-arounds exist (e.g. packages.debian.org/stretch/systemd-cron - not installed by default), but may take some work. In your case, try doing sudo systemctl status rc-local to see if there are any errors. If you modified rc.local, you may have to re-enable it. Will edit answer to clarify.
    – bobstro
    Commented Sep 19, 2017 at 21:01
  • sorry, I fail to see why you are saying this: "After modifying /etc/rc.local, check the status of the service with sudo systemctl status rc-local. You may see a warning that your must run sudo systemctl daemon-reload." So yes, there is a warning. But how do I perform this reload and why would that fix my problem with rc.local? Commented Sep 19, 2017 at 21:27
  • Because it's a significant change from the previous behavior of /etc/rc.local that can easily be overlooked. Changes to /etc/rc.local without checking the status will silently fail otherwise, and the startup message may be missed. The OP made such a modification.
    – bobstro
    Commented Sep 19, 2017 at 21:31
  • but how does this help me to solve the problem? I need to know where I have to add my line sudo service hostapd restart so that it executes after everyting else during bootup. I am no expert in any of this, but I imagine during boot a series of scripts is executed which are stored in some files. Hence, I would like to pinpoint the file which is used last in this process. This would be the file I have to add the code to, or is this incorrect? Commented Sep 19, 2017 at 21:35
1

Check out the line that says:

sudo nano service hostapd restart

It's opening nano, it should be:

sudo service hostapd restart

Edit:

From here:

If your command runs continuously (perhaps runs an infinite loop) or is likely not to exit, you must be sure to fork the process by adding an ampersand to the end of the command, like so:

I don't think that it's your problem, but try it, maybe it will work:

sudo service hostapd restart &
4
  • 1
    sorry, it's actually correct in my file. I don't know why I added it above. Edited. Commented Sep 19, 2017 at 14:36
  • I've edited my answer, but I'm 99% sure that it's not going to work :(
    – Luis Diaz
    Commented Sep 19, 2017 at 14:45
  • 2
    Do not use sudo in /etc/rc.local, it is run with root privileges by the init system. If you need to test it otherwise, use sudo /etc/rc.local.
    – goldilocks
    Commented Sep 19, 2017 at 15:04
  • forking does not work. Also it makes no difference not to use sudo, still does not work. I also tried without sudo and fork, no luck. Commented Sep 19, 2017 at 15:11

Your Answer

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

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