It feels like I have a morale dilemma / future proofing / portability issue and I just wanted to get this thing working.
Why? You should not incorporate the service aspects to your application at all, just keep them separate and maintain them separately.
Generally, the application should not control the CAN interfaces at all. (Does your browser control your network interfaces? Mine does not. If they try, I switch to a browser that doesn't.)
If you want to, like discussed before, you should have the application execute external scripts, say
/usr/share/yourapp/can-up and
/usr/share/yourapp/can-down that can be adapted to different environments and init systems. Your application simply runs them, and uses the exit status (zero if success, 1..127 if error) to determine the result, perhaps collecting standard error to display to the user if it fails.
Similarly for the other OS/distribution-specific things. Do not integrate the exact commands into your code. Execute external scripts instead, so that those can be adapted to the circumstances without having to recompile your application. Put them under
/usr/share/yourapp/ with
yourapp a global variable or constant.
When using Bash or POSIX shells, scripts that simply execute a fixed command should be written as
#!/bin/sh
export LANG=C LC_ALL=C
exec /path/to/command args...
where the
export command ensures the expected locale (
C being the default, non-localized locale), and
exec replaces the shell process with
/path/to/command. Without
exec, the shell process will stay resident in memory until the command exits. In the
exec statement,
"$@" (including the double quotes) expands to all command line parameters.
Do not use
system() or
subprocess.run() to execute these scripts, because they block the parent process until the child process exits. In Python, use
subprocess.Popen() instead. That way the parent and child processes run concurrently. Use
subprocess.DEVNULL for standard input, output, and/or error when you aren't interested in them, so that the kernel handles those streams, saving resources.