Michael's Daemonic Doodles

...blogging bits of BSD

service(8) - a useful little gem

Most services in FreeBSD are - or at least should be - controlled through rc scripts. So if you want to reload your pf rules, you would usually use /etc/rc.d/pf reload instead of using pfctl(8) directly. For restarting the apache web server /usr/local/etc/rc.d/apache22 restart instead of using apachectl directly. To figure out which services are enabled, you would check /etc/rc.conf.

Fortunately there is a new command in FreeBSD called service(8), which helps you to manage services in a convenient and more sophisticated way.

To be fair, new is relative in this context, since the service command first appeared in FreeBSD 7.3, which has been released in March 2010.

It's extremely straightforward to use:

List of enabled services

To get a list of enabled services use

service -e

e.g.

/etc/rc.d/hostid
/etc/rc.d/zvol
/etc/rc.d/hostid_save
/etc/rc.d/zfs
/etc/rc.d/ip6addrctl
/etc/rc.d/cleanvar
/etc/rc.d/devd
/etc/rc.d/pf
/etc/rc.d/newsyslog
/etc/rc.d/syslogd
/etc/rc.d/dmesg
/etc/rc.d/virecover
/etc/rc.d/motd
/usr/local/etc/rc.d/openntpd
/usr/local/etc/rc.d/smartd
/etc/rc.d/sshd
/usr/local/etc/rc.d/ezjail
/etc/rc.d/sendmail
/etc/rc.d/cron
/etc/rc.d/mixer
/etc/rc.d/gptboot
/etc/rc.d/bgfsck

Note that the list is not order alphabetically, but in the order of invocation on startup (it uses rcorder(8) for this purpose). If you use service with "-r" option, it will list dependency sorted list of all services (including services that are not enabled in the system).

List of available services

Use

service -l

to get a list of available services in the system. It scans the standard startup script directories /etc/rc.d and /usr/local/etc/rc.d for this purpose.

Controlling services

This is probably the most common use of service, it's pretty simple, e.g.

service pf reload
service apache22 restart
service mysql-server stop
service netif cloneup

Note

Unlike invoking rc scripts directly, the service command sets HOME and PATH like they're set on system startup, which ensures that your scripts are not using invalid information (and increases the chance, that they'll start correctly at boot time as well).

Conclusion

Service makes understanding and debugging system startup as well as controlling services easier, safer and more fun. It's a small command that saves a lot of time. I like it.

See also:

service(8) man page on www.freebsd.org