Basic SMF commands

Modified: 28 Apr 2022 01:26 UTC

SMF consists of three command line utilities:

Examining service status with svcs

The svcs command displays information about the state of your services. This typically means whether or not they are running and any problems encountered when attempting to start them. In general, services will fall into three different states:

Running the svcs command with the-a argument displays a list of all online and offline services. By default svcs prints out:

Each service is identified by a Fault Management Resource Identifier (FMRI). For example, the FMRI svc:/network/http:cswapache2

The above FMRI breaks down in the following way:

String Description
svc: The service type
/network/http The service name
:cswapache2 The service instance

For example, this output shows that Apache was disabled on May 31st:

# svcs svc:/network/http:cswapache2
  STATE    STIME  FMRI
  disabled May_31 svc:/network/http:cswapache2

An easy way of uncovering the FMRI for a service is by using grep in combination with svcs -a. For example, using grep in the following exposes the FMRI of the MySQL service, which is cswmysql5:

# svcs -a | grep -i mysql
  enabled  May_31  svc:/network/cswmysql5:default

You can abbreviate an FMRI by specifying the instance name or the trailing portion of the service name. For example, valid abbreviations for svc:/network/http:cswapache2 are: cswapache2, :cswapache2, http, http:cswapache2, network/http

Starting and stopping services using svcadm

The svcadm command is used to enable, disable, restart, or refresh services. For example, this command enables the MySQL service.

# svcadm enable cswmysql5

Using svcs, you can verify that the service is enabled:

# svcs cswmysql5
  STATE   STIME     FMRI
  online  15:20:39  svc:/network/cswmysql5:default

You use disable to stop services. For example, this stops the MySQL service:

# svcadm disable cswmysql5

You use restart to refresh a service. For example, after making a configuration change, you can refresh an enabled service like this:

# svcadm restart cswmysql5

Some daemons do not respond to the restart command. If that is the case, you will need to disable and re-enable the service.

Counducting service maintenance using svcadm clear

If a service enters maintenance mode or becomes disabled, you will need to perform some maintenance before SMF can restart the service. This means resolving any conflicts that prevent the service from running then using svcadm clear to clear the service state. When using svcadm clear, you need to specify the service FMRI.

This command does nothing to fix a service, it just signals that the service is ready to resume. Before you can clear a service state, you need to ensure that you resolve the conflict that caused the service to go into maintenance mode. For example, if you configure Postgres to use an IP port that is already in use by another service, the Postgres service will terminate abnormally and go into maintenance mode. In this case, you would need to resolve the IP conflict by modifying the Postgres configuration to use another IP or terminating the conflicting service. You would then clear the state after resolving the conflict.

Once the state is cleared, the service can resume. Each service has an assigned service restarter agent that is responsible for carrying out actions against it. The default service restarter is svc.startd.

For example, the following will clear the state of the Apache service and SMF will automatically restart the service once cleared:

# svcadm clear svc:/network/http:cswapache2

Verifying a service is in maintenance mode

SMF will place a service in maintenance mode when the service encounters something that causes it to crash. This usually indicates an error with the service but can also occur if your SmartMachine is running out of resources (RAM or disk space).

To verify if a service is in maintenance mode run this command:

# svcs -a

This will show all running and disabled services. If a service is in maintenance mode, you will see something similar to this:

maintenence 18:50:25 svc:/network/webmin:webmin 

Review the log to root-cause why the service was in maintenance mode.

# more $(svcs -L service_name)

Take the service out of maintenance mode:

# svcadm clear service_name

Verifying a service is disabled

To verify if a service is disabled:

# svcs -a

If the service is disabled, you will see something similar to this:

disabled 18:51:10 svc:/network/webmin:webmin

Enabling a service

Enable the service:

# svcadm enable service_name

Verify the service is enabled:

# svcs service_name

If successful, you should see something similar to this:

online 18:50:25 svc:/network/webmin:webmin

The term "online" is the service state and indicates that the service is running.

Examining service contracts using svcs -p

SMF maintains a contract with every running service it manages. The contract keeps track of what processes are running for any given service. Using the -p option, you can determine all the processes that belong to a service. In the following example, the MySQL daemon is process number 29004.

# svcs -p network/cswmysql5
  STATE      STIME       FMRI
  online     16:55:27    svc:/network/cswmysql5:default
             16:55:27    28938 mysqld_safe
             16:55:27    29004 mysqld

The following example demonstrates how SMF restarts a service when it stops unexpectedly:

# kill -9 29004
# svcs -p network/cswmysql5
  STATE   STIME     FMRI
  online* 17:00:01  svc:/network/cswmysql5:default
          16:55:27  28938 mysqld_safe
          17:00:01  29228 mysqld
# mysql -u mysql
...
mysql> \q
Bye

Even though the MySQL daemon was unexpectedly terminated, it was automatically restarted by SMF. Notice that the STIME shows that the MySQL service is back online. The inclusion of an asterisk with the "online" state indicates that the service is currently in transition. However, the MySQL service is already back online by the time the next command is run.

SMF does not restart the service in brain-dead mode like a legacy inittab; You can configure a threshold for service restarts. For example, numerous restarts of a service in a 60 second time frame might indicate a severe issue in your environment. You can configure a restart threshold in SMF for that service. At that point, SMF will put the service in "maintenance" mode, and the service will remain in that state until you clear it with svcadm clear.

Configuring services using svccfg

The svccfg command allows you to import, export, and modify service configurations. You specify entities to manipulate by using the -s option with an FMRI. The following example will set an environment variable for the specified FMRI with the value you specify.

# svccfg -s FMRI setenv ENV_VARIABLE value

You can invoke svccfg directly with individual subcommands or by specifying a script file. If you make any changes to a service using this command, you need to restart the service for the changes to take effect.

Enabling SMF Access

If you want to enable users without root access to manage SMF, you can modify a user profile as follows:

  1. Open /etc/user_attr for edit.

  2. Add this line replacing "myuser" with the login you want to enable:
    myuser::::profiles=Service Management

After this change, the specified user is able to manage SMF (import, stop, start) without access to root. This minimizes the need for unnecessarily sharing root access among users.