I have a C++ that I would like to auto start each time the Pi boots. The make file takes the .cc file, transforms it into an .o file which it then transforms into an executable file without an extension.

Raspbian Buster Lite: Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux

I am running that program with: sudo ./exec_file I need to run the program as root. The program does not have a GUI. I am running it from terminal.

-rwxr-xr-x 1 root root 1248576 Aug 24 17:09 /var/www/html/4panel/exec_file
-rwxr-xr-x 1 root root 471 Aug 24 18:00 /etc/rc.local

I would like to know what do I need to write inside rc.local to make it "auto run". I have found examples with python programs, but I haven't found any with executable files or c++ programs. I only know that I have to specify the absolute path, but I have no idea what comes before that.

Adding sudo /var/www/html/4panel/exec_file & inside rc.local before exit 0 did not work. Nor did @sudo /var/www/html/4panel/exec_file &

Thank you !


Using /etc/rc.local has limitations due to Compatibility with SysV as @Milliways already noted. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it.

Here is a very simple systemd Unit file as entry point. It may need some additional configuration but we will see. Create a new service for your program with:

rpi ~$ sudo systemctl --full --force edit exec_file.service

In the empty editor insert these statements:

Description=My exec_file service



Services are started with root rights by default so there is no need to set any permissions. Enable the service with:

rpi ~$ sudo systemctl enable exec_file.service

Reboot and check with

rpi ~$ systemctl status exec_file.service
  • But what if I made my program work both from inside rc.local and as a systemd service ? You are saying that I should keep running it as a systemd. Why ?
    – bleah1
    Sep 12 '19 at 8:25
  • @bleah1 SysV isn't available anymore. It's all done by systemd as general init system, also rc.local so it doesn't make sense to use both. Please read about Compatibility with SysV with the link I have given, in particular the last sentence: "Note that there are some areas where systemd currently provides a certain amount of compatibility where we expect this compatibility to be removed eventually.". So it never helps to use rc.local if you have problems.
    – Ingo
    Sep 12 '19 at 9:27

Your problem here is that you don't execute /var/www/html/4panel/exec_file. You execute sudo with /var/www/html/4panel/exec_file as an argument. Since you don't specify the full path of the executable you run (that is, /usr/bin/sudo), your command cannot be started since no PATHs are set up at this point.

Notably, rc.local is executed with root permissions, so you don't need to put sudo there at all. Remove it and it should work.

  • What do you mean no paths ? /var/www/html/4panel/exec_file is the whole path.
    – bleah1
    Sep 11 '19 at 11:02
  • In sudo /var/www/html/4panel/exec_file, sudo is the command, and it's indeed without path. Sep 11 '19 at 11:04
  • So it should work if I take sudo out of it ?
    – bleah1
    Sep 11 '19 at 11:07
  • Yes that's what I suggest. Sep 11 '19 at 11:08
  • 1
    It worked. /var/www/html/4panel/exec_file & is what I needed. The process did start and it did work, but there was something wrong in the code. Thank you !
    – bleah1
    Sep 12 '19 at 12:52

You should not use rc.local on systemd although simple programs will work.

If the program needs sudo you can not run automatically because you need to supply a password. You could set suid permission, depending on the program.

It is preferable to write a systemd service which runs as root.

  • Does this mean that I need to ask another question in which I will ask how to write a systemd service which runs as root ?
    – bleah1
    Sep 11 '19 at 10:27
  • 1
    You will find many examples on this site. It is not possible to answer without knowing more about the code. There are many tutorials on systemd services.
    – Milliways
    Sep 11 '19 at 10:32
  • You are right. I meant that as a joke. I will try and see if I can do it on my own before asking anything more here.
    – bleah1
    Sep 11 '19 at 10:33
  • People are quite willing to help but if you want help you need to be more specific
    – Milliways
    Sep 11 '19 at 10:35
  • I do have a question though. I can only find services that run sh scripts. I want to run a binary executable that resulted from a .cc file. Is that possible ? Or do I need to make a sh script in which I start the exec file ?
    – bleah1
    Sep 11 '19 at 11:00

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.