Personal tools

Executing commands at boot time

From PFSenseDocs

Jump to: navigation, search

There are three primary options for executing custom commands at boot time: shellcmd, earlyshellcmd, and shell script.

The shellcmd package can manage the shellcmd and earlyshellcmd tags in the GUI, so you do not need to edit the config.xml values by hand, but that works as well.

At boot time, the earlyshellcmd commands are first, shellcmd is later in the boot process, and the shell scripts are executed at the very end when packages are initialized.

shellcmd option

The hidden config.xml option <shellcmd> will run the command specified towards the end of the boot process.

To add a shellcmd to your configuration, either use the shellcmd package or edit the config by hand. To edit the config, back it up via Diagnostics > Backup/restore, and open the resulting XML file in a text editor (other than the stock Windows Notepad). Above the </system> line, add a line such as the following:

<shellcmd>mycommand -a -b -c 123</shellcmd>

Where "mycommand -a -b -c 123" is the command to run. You can add multiple <shellcmd> lines to execute multiple commands. Save your changes and restore the modified configuration.

earlyshellcmd option

The hidden config.xml option <earlyshellcmd> will run the command specified at the beginning of the boot process. Normally you should use <shellcmd> rather than this, though this may be necessary in some circumstances. Similarly to <shellcmd>, to add a <earlyshellcmd> option, either use the shellcmd package or edit it in by hand. To edit it manually, backup the configuration, open it in a text editor, and add a line such as the following above </system>:

<earlyshellcmd>mycommand -a -b -c 123</earlyshellcmd>

Where "mycommand -a -b -c 123" is the command to run. You can add multiple <earlyshellcmd> lines to execute multiple commands. Save your changes and restore the modified configuration.

Shell script option

You can also place any shell script in the /usr/local/etc/rc.d/ directory. The filename must end in .sh and it must be marked as executable (chmod +x myscript.sh). Every shell script ending in .sh in this directory will be executed at boot time.

The first two options are preferable as they are retained in the config file and hence do not require additional modifications should you have to replace the storage medium and reinstall, or restore the configuration to a different piece of hardware.