Ankit Tripathi
Ankit Tripathi

Reputation: 384

CATCH SIGTERM IN ERLANG

Trust you are doing well.

I need to catch SIGTERM in erlang run-time so that I can save the state of ets to a dets file & when the system comes up again it to start from the point it stopped. I could write a C program to catch SIGTERM but the VM would also be catching the same signal. Now how would I ask ERLANG to IGNORE the SIGTERM so that only my C program catches the signal & pass it to erlang so that it could save the state before exit-ting.

Upvotes: 3

Views: 1005

Answers (1)

zxq9
zxq9

Reputation: 13154

Easy solution: don't send the SIGTERM to any Erlang VM processes. Send it only to the C program and have it act as a signal listener for your Erlang programs it knows how to talk to. A traditional way is to have the C program write its PID on startup and write a shell command that reads the C process' PID from a temp file somewhere. The shutdown shell command doesn't need to know anything about the Erlang VM.

There are better ways of dealing with this, though. Why are you waiting for SIGTERM? If you are running an always-on service on a *nix system you should have startup and shutdown scripts that work with the system. Those could send "clean shutdown" commands to your Erlang service any way you happen to feel like writing them (they could be or call escripts themselves, for example).

If you are trying to prevent loss of data when the OS crashes, though, this is the wrong way to go about it. There are other tradeoffs (like the overhead of always working from disk or an external data store) to be made, because flushing to disk in the middle of an OS crash is more likely to corrupt or destroy all of your data than allow you to save the most recent bits of it.

Upvotes: 1

Related Questions