0

I need to run the command "sudo service dnsmasq start" on every startup as I'm using my raspberry as a network bridge (from wifi to ethernet) I know the command works but it's hard to start my computer and open Putty and log on to the raspberry and run the command I need a way to automatically run this command st every startup

I'm a beginner so make it easy, Thanks

1
  • what's the result of systemctl status dnsmasq - in particular the Loaded: line ... is it enabled? How did you install dnsmasq? apt install dnsmasq for me has always resulted in dnsmasq starting on boot Jan 12, 2021 at 23:24

1 Answer 1

2

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.

Once 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 (CTRL-o in nano) and exit (CTRL-x in nano) the 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 cron

What does the crontab entry do?

  1. @reboot means the command(s) following are run once each time your RPi boots

  2. 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 dnsmasq

  3. /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.

  4. >> /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 dnsmasq service.

4
  • I'll try. Thanks for the quick answer Jan 13, 2021 at 3:58
  • OMG it works thank you very very much Now it's easier for me Jan 13, 2021 at 4:05
  • 1
    @DahamSirimanna: I'm glad it helped. Now, if you don't mind - please vote
    – Seamus
    Jan 13, 2021 at 5:47
  • Ok I'm new to this stack exchange Sry😅 Jan 22, 2021 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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