We have a shell script that -- for various reasons -- wraps a vendor's application. We have system administrators and application owners who have mixed levels of familiarity with systemd. As a result, in situations where the application has failed (systemctl indicates as much), some end users (including “root” system administrators) might start an application “directly” with the wrapper script instead of using systemctl restart
. This can cause issues during reboots, because systemd does not call the proper shutdown script -- because as far as it's concerned, the application was already stopped.
To help guide the transition to systemd, I want to update the wrapper script to determine whether it is being called by systemd or by an end-user; if it's being called outside systemd, I want to print a message to the caller, telling them to use systemctl.
How can I determine, within a shell script, whether it is being called by systemd or not?
You may assume:
- a bash shell for the wrapper script
- the wrapper script successfully starts and stops the application
- the systemd service works as expected
An example of the systemd service could be:
[Unit]
Description=Vendor's Application
After=network-online.target
[Service]
ExecStart=/path/to/wrapper start
ExecStop=/path/to/wrapper stop
Type=forking
[Install]
WantedBy=multi-user.target
I am not interested in Detecting the init system, since I already know it's systemd.
/dev/tty
), then the init system did not start you. Or you acquired a controlling terminal by opening a pty or something, but that's usually a Bad Idea. Or else the init system is not "reasonable" - I've never heard of an init system that gives daemons controlling terminals, but I suppose it could exist? – Kevin Dec 05 '20 at 20:00ssh somehost somecommand
, for example, won't have a TTY with ssh using a default configuration. – Charles Duffy Dec 05 '20 at 23:28ssh somehost somecommand
without-tt
orForceTTY true
, demonstrates the point. – Charles Duffy Dec 06 '20 at 20:10