As this requires elevated privileges (i.e.
sudo), you can do this by adding it to the
root crontab. In other words, use
cron to start this at each boot. You will do this from the command line interface (a terminal):
Once you are logged in, and have the shell prompt, proceed as follows:
$ sudo crontab -e
You may be prompted to choose an editor -
nano would be a god choice as it is easiest for beginners.
nano starts, your screen will show the existing
crontab for the root user in
nano. Move the insertion point/cursor to a blank line just below the last line of text. Then, type the following:
@reboot sleep 15; /usr/sbin/service dnsmasq start >> /home/pi/dnsmasq-cron.log 2>&1
Now save (
nano) and exit (
nano editor. You will get a message that your
crontab has been created. After that, you will return to the interactive shell & command prompt where you may issue a
reboot command to test the command.
Explanation & alternative to
What does the
crontab entry do?
@reboot means the command(s) following are run once each time your RPi boots
sleep 15 means that
cron will wait 15 seconds before executing the next command. This gives your system time to get its networking services started before starting
/usr/sbin/service dnsmasq start is the command you supplied to start
dnsmasq; the full PATH to
service is used so that
cron doesn't need to find it.
>> /home/pi/dnsmasq-cron.log 2>&1 re-directs any output or error messages that you would have gotten in your interactive shell to a file in user
pi's home directory. You should examine this file after you
reboot to make sure it's working properly, and in the event it fails to start.
Finally - this
crontab you are invoking starts a service. Another way to start this service is to use
systemd; a newer unified approach which has its advantages, but may not be as simple as this
crontab entry. If you're interested, this Q&A provides a
systemd way to start the