I'd like to prevent direct use of start/stop bash scripts without using "systemctl start/stop service"
Is there a bash way to insert that in scripts ?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI'd like to prevent direct use of start/stop bash scripts without using "systemctl start/stop service"
Is there a bash way to insert that in scripts ?
You can check within the script whether it has been called by SystemD and e.g. abort with an error message if not or call systemctl
.
There are several ways for determining the parent, e.g.:
Very simple but not 100% certain: Check whether the PPID is 1:
grep -q '^PPid: *1$' /proc/$$/status
systemctl status $$ >/dev/null 2>&1
(exit code is 0 for SystemD processes)Set a unique environment variable in the SystemD unit file and check for it
if [ 'yes' != "$STARTED_BY_SYSTEMD" ]; then ...