Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using FreeBSD 9 and I want to make a script that will check if an Ethernet cable is connected, and if so, connects to it. I want to set it so that it runs whenever the machine starts up, or whenever I log in. I looked at my UNIX book (It is pretty outdated, and is for general UNIX, not specifically FreeBSD) and it says that I can use crontab, but I can only do this to make things run at certain times, not when it starts up. Can someone please tell me how I can get at this?

share|improve this question
up vote 4 down vote accepted

FreeBSD's cron understands the @reboot time directive, so you can indeed have cron execute your script at startup. Instead of the usual 5 time fields, your crontab entry might look like this:

@reboot /path/to/script

If you want also to run the check when you log in, add a call to the script in the file your shell executes upon login - this could be, depending on your shell, ~/.login, ~/.bash_login, etc.

share|improve this answer

Another solution for startup script would be /etc/rc.local.

share|improve this answer
    
Though man rc tells "Typically, the /usr/local/etc/rc.d/ mechanism is used instead of rc.local these days but if you want to use rc.local, it is still supported. In this case, it should source /etc/rc.conf and contain additional custom startup code for your system. The best way to handle rc.local, however, is to separate it out into rc.d/ style scripts and place them under /usr/local/etc/rc.d/." – zezollo Jul 20 at 15:50

On FreeBSD 11 for ARMv6 (Raspberry Pi), I couldn't get the crontab @reboot directive to work for the root user, and the "add scripts to the /etc/rc.local file" method has been superseded by a mechanism that automatically runs scripts located in /usr/local/etc/rc.d/.

In other words, to run a script on boot on FreeBSD 11 (or 10?) and likely newer, place an executable shell script with the .sh extension in this folder to have it executed on boot:

/usr/local/etc/rc.d/

You may need to create the rc.d folder if it doesn't exist.

See http://www.defcon1.org/html/rc-local.html for details.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.