Is it possible for an rc.d initialisation script to determine whether it has been autobooted (such as when a jail starts) or invoked interactively (such as when service xyz start
is issued from the command line)? If it is possible, can I please get some expert advice on what I should be looking for?
The radical answer is: if the script could detect this, it should be considered a flaw in the rc.d software.
Now let me explain this: when you develop an application (or port it to some system), you will test it by starting it in your terminal. Then, if all is well, you copy your start command into an rc.d script and expect the application to come up at reboot. And, in most cases, it will NOT do so.
The cause of this is, there are dozens of things that are different at system bringup than in a terminal: there is no terminal, no job control, no stdio, and lots of other things which are usually taken for granted by the interactive user. And in consequence, the application will either not start or behave differently.
So then you will have to start searching for the problem, which is not always easy. Therefore, the rc.d system should start an application always in the same way, no matter if invoked interactively or at system start. (We do rarely boot our systems, and we do not like bad surprises after a reboot.)
This is not possible in fullness, because there may be interdependencies - i.e. the sequence in which the applications are started one after another may matter. And there are certainly some tricks by which an experienced programmer could figure out what is the case, anyway.
Caddy fails initially if started from a jail. The problem is that there is no place for log output to go. The issue can be circumvented by having the initialisation script redirect stdout
and stderr
to a file.
This is a very typical example of these issues that appear when integrating an application. Ideally these should be pointed to
/dev/null, or the application run under
daemon(8) and the output written to
syslogd(8).
However, that should not be a blanket requirement. If commands are invoked interactively, it's desirable to have output redirected to the terminal. So, unless I'm thinking about this the wrong way, the logic I'm grappling with is 'If autostarted, redirect output to a file, otherwise, redirect output to the terminal'.
I understand. But I think this should be done differently. The rc.d scripts are intended for starting applications that will continue to run - as long as the server runs or until they are stopped again. If you re-route stdio to your terminal, and later log out, nobody will stop that application, and you will be back at the starting point where the application doesnt have a stdio. Or, worse, the next person logging in on your terminal will receive the output from your application (this was very common on the old time-sharing machines with hard-wired serial terminals - it is no longer the case with current virtual terminals).
So I don't think this is a good idea. What You might do instead is create a debug mode: make another script to start the application from within your terminal, in a temporary way only for as long as you work on it. And there you can handle things differently.
Some other points from your linked site:
how important is it that Caddy V2 create a PID file,
You want that PID file so that you can clearly identify your application when you want to stop it again. If the application produces a pid file, then rc.d can use that to stop it. If it doesn't, rc.d must resort to grepping around in the
ps(1) output, and that can be erroneous.
or require LOGIN and DAEMON?
If you run the application with stdout routed to syslogd (as recommended above), then it should be started only after syslogd has started. There may be other things, depending on what system ressources the application may want to use.