Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching system events in C Linux

Tags:

c++

c

linux

system

I am writing an application on Linux which requires me to catch system events like:

  1. System reboot
  2. User 'xyz' logs in
  3. 'xyz' application crashes etc.

and need to execute some functionality based on that. For e.g.:

  1. Run backup script
  2. Run recovery program etc.

Can anyone please tell me how to catch system events in C/Linux ?

P.S: I am not talking about 'file system' events here :P

like image 915
openstk Avatar asked Feb 07 '26 01:02

openstk


1 Answers

There is no concept of "system event". You need to specify which events you need to handle and implement appropriate mechanism for handling each:

  • System startup: The init process calls scripts from /etc/init.d during startup. The exact infrastructure differs slightly between distributions, but Linux Standards Base System Initialization should generally work on all.

  • User login/logout: The LSB also defines interface to the Pluggable Authentication Modules library. You can implement a shared library that will be called during login (and also other actions that require authentication and authorization). Depending on what you want to do there may already be a module that will work for you, so try looking for it first. In either case I don't think there is distribution-independent way of installing it and even on given distribution you have to consider that administrator might have made custom modification, so the installation will need manual intervention by the administrator.

  • Application crashes: You would have to instrument it.

like image 52
Jan Hudec Avatar answered Feb 08 '26 15:02

Jan Hudec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!