Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a script in FreeBSD after boot/reboot

Tags:

sh

cron

freebsd

rc

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?

like image 524
Pachvarsh Avatar asked Aug 27 '18 08:08

Pachvarsh


People also ask

How do I run a script from boot?

On Windows, the simplest way of running a program at startup is to place an executable file in the Startup folder. All the programs that are in this folder will be executed automatically when the computer opens. You can open this folder more easily by pressing WINDOWS KEY + R and then copying this text shell:startup .

How do I run a script in FreeBSD?

If you really want to run Linux based written script using /bin/bash in FreeBSD without modifying them, then you can simply copy /usr/local/bin/bash to /bin/bash at FreeBSD and it will work.

What is RC D?

The rc.d/ directories contain scripts which will be automatically exe- cuted at boot time and shutdown time. The service(8) command provides a convenient interface to manage rc.d services. The sysrc(8) command provides a scripting interface to modify system con- fig files.


1 Answers

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.

like image 156
Mateusz Piotrowski Avatar answered Sep 25 '22 05:09

Mateusz Piotrowski