Shell Running a separate script after an rc script is run

I have not been able to find an answer to this question here, in the man page, or via google so here goes. Is there a way to specify a script to be run after an rc script is run from /usr/local/etc/rc.d/? I have a service that I need called after I restart a Tomcat 9 instance. I'm able to handle running it on boot and whenever we do a full service restart on the system as those are run through separate scripts. The one scenario I cannot account for is those times when we only restart the Tomcat instance. I thought about modifying /usr/local/etc/rc.d/tomcat9 but that would get clobbered every package update. Changing procedures is an option but our admins have 15+ years of muscle memory associated with just using the base tomcat rc script and I'm trying to avoid the mistake of restarting Tomcat without also calling the external service. Does anyone know if there's an option to specify something in rc.conf like tomcat9_poststart="/run/this/script.sh" that would accomplish this?
 
I don't know of anything like a "postrestart" thing.

I fear you'll just have to modify the /usr/local/etc/rc.d/tomcat... script, and then remember to refix it after every upgrade. There are various ways to automate that so it doesn't get completely wiped out after each upgrade. One way would be to build the tomcat package from source, install it from your port, and modify your source. I think this solution is more heavyweight and painful than the original problem.

Another crazy idea is this: Create a new service (call it "mytomcat"), write the rc files for it from scratch, and in there start/stop/restart things correctly, with suitable pre-/post-processing. Then train your muscle memory to always say "mytomcat". It's a tradeoff.

I'm having the same problem: The apcupsd daemon doesn't work correctly for my UPS, nor does Nut (firmware issue with my equipment, I think). I fear I'll have to modify /etc/rc.shutdown and /usr/local/etc/rc.d/apcupsd, and then remember to fix them every time I do an upgrade. Unfortunately, neither have an "escape hatch" like rc.shutdown.local. Don't know yet what to do; the last 3 weeks I've been insanely busy with other stuff, and for now I'm controlling my UPS by walking down there and pushing buttons and pulling plugs.
 
Code:
#    ${rc_arg}_postcmd n    If set, run just after performing the
#                ${rc_arg}_cmd method, if that method
#                returned a zero exit code.
I think this is what you're looking for, have a look at /etc/rc.subr
 
in the script that runs first add a line to run another script at the end of that script. I'd give the absolute path to the script.
 
In /etc/make.conf
Code:
.if ${.CURDIR:M*/www/tomcat9}
post-patch-script:
        sed '/match/a\
              restart_postcmd="tomcat_postrestart"
        and so on ... # Make sure that the edit will work even if the file changes
.endif

EDIT: Using post-install-script is more accurate.
 
I encourage you to read rc.d(8)() to understand how each rc script works. More specifically, check out the part(s) about the REQUIRE and BEFORE keywords, which allow you to choose the order of execution relative to the rest of the scripts. Then, you can use /sbin/rcorder /etc/rc.d/* /usr/local/etc/rc.d/* 2>>/dev/null after to confirm the desired execution order.
 
Here is an other way, perhaps more appropriate to set up this. There are not a lot of ports who use *-script target, it should be safe to create /usr/ports/www/tomcat9/scripts/post-install
Code:
# tail +3644 /usr/ports/Mk/bsd.port.mk | head -15
.    if exists(${SCRIPTDIR})
.      for stage in pre post
.        for name in pkg check-sanity fetch extract patch configure build stage install package

.          if !target(${stage}-${name}-script)
.            if exists(${SCRIPTDIR}/${stage}-${name})
${stage}-${name}-script:
    @ cd ${.CURDIR} && ${SETENV} ${SCRIPTS_ENV} ${SH} \
            ${SCRIPTDIR}/${.TARGET:S/-script$//}
.            endif
.          endif

.        endfor
.      endfor
.    endif
 
Here’s another idea that may be useful to someone: consider extracting the Tomcat service to a jail, and adding your new startup script with AFTER: tomcat. Then you just restart the jail.

I wanted to do a similar thing, but before nginx. I suspect argument_precmd will do the job - thanks W.hâ/t.
 
Using /etc/make.conf and ${_USES_install}. This will run just after install-rc-script (860).
Bash:
.if ${.CURDIRM*/www/tomcat9}
_USES_install+=   861:tomcat9-rcedit
tomcat9-rcedit:
           cmd ... ${STAGEDIR}${PREFIX:S,^/usr$,,}/etc/rc.d/rcfile
.endif
 
Back
Top