(originally published on 07.05.2014, reviewed/rewritten on 24.04.2025, tested on Oracle Solaris 11.4 SRU 80)
 

Sometimes a rather small option for a command is really useful. One possible problem with svcadm is the asynchronous behaviour of the command. You execute the command and it will return immediately. There is no feedback at the return of the command, if the service was enabled or disabled successfully. I often see people and ad hoc scripts doing something akin to the next example.

I will use the svc:/network/http:apache24. I slightly modified the script /lib/svc/method/http-apache24 used by SMF by inserting a 10-second sleep. Normally, apache24 is up and running so fast that I can’t use it as an example.

STATE          STIME               FMRI
disabled       2025-04-24T03:54:38 svc:/network/http:apache24
root@testbed:~# svcadm enable svc:/network/http:apache24
root@testbed:~# svcs svc:/network/http:apache24
STATE          STIME               FMRI
offline*       2025-04-24T03:54:48 svc:/network/http:apache24
root@testbed:~# svcs svc:/network/http:apache24
STATE          STIME               FMRI
offline*       2025-04-24T03:54:48 svc:/network/http:apache24
(...)
root@testbed:~# svcs svc:/network/http:apache24
STATE          STIME               FMRI
offline*       2025-04-24T03:54:48 svc:/network/http:apache24
(...)
STATE          STIME               FMRI
offline*       2025-04-24T03:54:48 svc:/network/http:apache24
root@testbed:~# svcs svc:/network/http:apache24
STATE          STIME               FMRI
online         2025-04-24T03:54:58 svc:/network/http:apache24

The service is going from disabled to offline to indicate that the service has been started, but the startup hasn’t been completed. It’s a transitionary state as indicated by the * after the state.

When you use the -s, the svcadm enable command just returns when the state transition has completed. So, for example, when the service got from offline to online. It will return as well when it has determined, that it can’t do the desired state transition and needs admin intervention. As I artificially delayed the execution of the method script, for example, an enable should need slightly over 10 seconds until it returns.

root@testbed:~# ptime svcadm enable -s svc:/network/http:apache24

real       10.291075738
user        0.006505993
sys         0.008724291

In the background, the service is still going through the offline* state.

root@testbed:~#  svcs svc:/network/http:apache24
STATE          STIME               FMRI
offline*       2025-04-24T04:28:36 svc:/network/http:apache24

When used in scripts, the -T option is really useful. You can define a timeout after which the command returns no matter if the state transition has been successful. If the command runs into the timeout, you will see an error code of 5 compared to an 0 when the state transition was successful within the timeout.

root@testbed:~# svcadm enable -s -T 5 svc:/network/http:apache24
root@testbed:~# echo $?
5
root@testbed:~# svcadm enable -s -T 15 svc:/network/http:apache24
root@testbed:~# echo $?
0

You have to keep in mind that this timeout is just for the svcadm subcommand. The startup or shutdown process of the service itself goes on until other timeouts hit or the transition was successful. So when you know that your service should take 30 seconds tops to start, you can let this svcadm command timeout at 30 seconds and react accordingly to the error code.

Written by

Joerg Moellenkamp

Grey-haired, sometimes grey-bearded Windows dismissing Unix guy.