• Uncategorized

About c++ : C-app—how-to-stopcleanup-if-it-is-run-as-Linux-service-closed

Question Detail

I have basically a console C++ app for Linux CentOS9. I am going to run it as a service using systemctl start/stop.

Currently the app exits if user press Enter in the console but this wont be possible in service mode as the user wont see the app console after logging in.

What can be done so I the app can detect it is being stopped/terminated so it can cleanup resources (closing files)?

Question Answer

It seems systemctl stop sends SIGTERM.

Just register callback for it

void signal_callback()
    printf("Process is going down\n");
signal(SIGTERM, signal_callback)

How systemd stop command actually works

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.