The system automatically issues SIGTERM
signals to all processes as one of the steps in rebooting. As long as you correctly handle SIGTERM
, you need not do anything special after invoking the reboot
command. The normal idiom for "correctly handling SIGTERM
" is:
- Create a pipe to yourself.
- The signal handler for
SIGTERM
writes one byte (any value will do) to that pipe. - Your main
select
loop includes the read end of that pipe in the set of file descriptors of interest. If that pipe ever becomes readable, it's time to exit.
Furthermore, when a process exits, the kernel automatically closes all its open file descriptors, terminates all of its threads, and deallocates all of its memory. And if you exit cleanly, i.e. by returning from main
or calling exit
, all stdio FILE
s that are still open are automatically flushed and closed. Therefore, you probably don't have to do very much cleanup on the way out -- the most important thing is to make sure you finish generating any output files and remove any temporary files.
You may find the concept of crash-only software useful in figuring out what does and does not need cleaning up.