The rc.local file is used on many Linux operating systems to schedule operations to run after services are started on the machine.
On all supported operating systems the rc.local file will be run as the root user. This means that all commands in the file will be run as the root user.
Typically as stated above this will be run once other services on the machine are started. However this is open to the operating system implementation. You may need to check your operating system for specifics.
The Raspberry Pi default Raspbian distribution will run this once the standard system services are started.
Making our python scripts runnable from the shell
Currently we have been running our python scripts by running python <scriptname>. This works well but to run it easier we want to launch it without running stating what python to run.
First to be able to launch the script immediately from the command line is to make it executable. This can be done on Linux with the following command line:
chmod u+x <script name>
Once this is done you are able to launch the script by running ./<script name>.
However this will only work if the shell can detect what executable it should run it with. A hint can be given to the shell by using a shebang.
This is a hint in the first line of a script to tell it which executable to run. For python files you can place the following in the top line of the file.
This tells the shell to run this file with the script /usr/bin/env python. This finds the location of python by searching the environment variables.
With these two changes we will be able to launch both slave and master using the following commands.
To run these scripts from the startup rc.local script we will want to change some relative references.
Changing relative file references
Currently the scripts assume that all files are located in the current working directory. When the scripts are launched under rc.local this will not be the case.
One way to resolve this is to change the current working directory, to the location of the scripts before running. However a more versatile method is to load files relative to the scripts.
To do this I am going to be referencing the directory of the scripts using the following code.
This gets the directory that the current file is located within. Using this and os.path.join() I can reference the configuration files relative to the script directory.
This then changes our config loading line, to the following:
Here we launch the basic_master.py file on startup. We redirect all standard output and error to /dev/null to surpress any output. Finally & is added to the end of the line to launch it in the background.
Now we have our slaves running their scripts when they are booted up. This allows them to automatically join the cluster without further intervention.
The full code is available on Github, any comments or questions can be raised there as issues or posted below.