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?
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 .
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.
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.
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:
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
I think you also miss setting test_enable
to a default value.
: "${test_enable:="NO"}"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With