5

I have a simple script:

#!/bin/sh

PROVIDE: test
REQUIRE: LOGIN NETWORKING

. /etc/rc.subr

name="test"
load_rc_config $name

rcvar=test_enable

cd /home/deploy/projects/test
/usr/sbin/daemon -u deploy /usr/local/bin/node /home/deploy/projects/test/server.js

run_rc_command "$1"

inside /usr/local/etc/rc.d. It is executable. It is registred into /etc/rc.conf

I need it to start after boot/reboot. I managed to do it with Cron using

@reboot

but it doesn't look legit. What is the proper way to run that script automatically after boot/reboot?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Pachvarsh
  • 137
  • 1
  • 10
  • Probably a supervisor could help you better, for example: https://immortal.run/freebsd/ – nbari Aug 27 '18 at 19:52

1 Answers1

5

First of all, there's an article in the official documentation explaining how to write rc scripts: Practical rc.d scripting in BSD.

It will probably answer most of your questions.

When it comes to your script:

  1. The keywords like PROVIDE, REQUIRE, etc. have to be comments. See the rc(8) manual page and the rcorder(8) manual page for more details.

    #!/bin/sh
    #
    # PROVIDE: test
    # REQUIRE: LOGIN NETWORKING
    
  2. I think you also miss setting test_enable to a default value.

    : "${test_enable:="NO"}"
    
  3. You don't really want to just put the instructions to start your daemon in the global scope of the script. This part of your code is bad:

    cd /home/deploy/projects/test
    /usr/sbin/daemon -u deploy /usr/local/bin/node /home/deploy/projects/test/server.js
    

    You should try to define a start_cmd function (look for argument_cmd in the rc.subr(8) manual page for more information) or define the command variable.


All in all, the best idea is to look at other scripts in /etc/rc.d and /usr/local/etc/rc.d to see how people write those and what are the standards. This is how I've learnt it recently as I was developing a daemon for the Keybase filesystem (KBFS). You may look at the code here.

The manpages are also helpful. Start with rc(8) and then look at other manuals listed in the SEE ALSO section.

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
  • 1
    @Pachvarsh: You might want to take a look at the example in [How to debug rc.d scripts in freeBSD?](https://stackoverflow.com/questions/51833364/how-to-debug-rc-d-scripts-in-freebsd/51838663#51838663) – Vladimir Botka Aug 28 '18 at 12:00