'How to detect the 'service stop' command in Linux

I'm writing a C application to drive a LED strip on a Linux machine: the application listens on a TCP socket port to receive commands.
If the connection is terminated, the application waits another connection.

I launch this application as service by a susyemd configuration file, and it is running silently well.

The only problem is that when the machine restarts or shuts off (or simply I need to restart the service) the service drops any connection and delays the power down, waiting the socket timeout to goes on. I guess it is waiting for a new connection ..

Is there a way detect the 'service stop' command, just to avoid to fall in the 'wait for a connection' command and simply terminate the application?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source