I want to start a script when booting my FreeBSD box automatically. Unlike Linux, FreeBSD does not honor the script you put in /etc/rc.local. Usually it is recommended to use the rc service (See here for details), however I think it is too complicated to convert my script to a rc compatible version. I decide to explore more simple solutions. I found that it can be done via Cron.
Suppose I have a script in the following location. I decide to run it when booting the machine automatically:
First, I need to include this script in the Cron:
sudo nano /etc/crontab
And add the following to the end of the file:
@reboot root /script/myscript.sh
That will make the system to execute this script.
However, the system may not run your script correctly because the Cron job uses a different shell and the path information may be missing. You can fix it by modifying the SHELL and PATH variables in /etc/crontab:
In my case, I uses Bash and I like my script executed by Bash:
And my script needs to execute some commands that locate in /usr/local/bin:
That’s it! Have fun with FreeBSD.