10

I'd like to know how GNU Emacs responds, by defaults, to such signals as HUP and USR1. Is this behavior documented in any manual? If so, where? If not, is there a place I could look in the source code?

Drew
  • 75,699
  • 9
  • 109
  • 225
Norman Ramsey
  • 1,113
  • 6
  • 13

1 Answers1

15

The signals which are mentioned in the manuals are: SIGHUP, SIGINT, SIGTERM, SIGTSTP, SIGUSR1, SIGUSR2


USR1 and USR2 get the most attention, as you can customize the effects of these. The following excerpts provide the details:

C-hig (emacs)Checklist:

If you cannot get Emacs to respond to ‘C-g’ (e.g., because ‘inhibit-quit’ is set), then you can try sending the signal specified by ‘debug-on-event’ (default SIGUSR2) from outside Emacs to cause it to enter the debugger.

C-hig (elisp)Error Debugging:

-- User Option: debug-on-event If you set ‘debug-on-event’ to a special event (*note Special Events::), Emacs will try to enter the debugger as soon as it receives this event, bypassing ‘special-event-map’. At present, the only supported values correspond to the signals ‘SIGUSR1’ and ‘SIGUSR2’ (this is the default). This can be helpful when ‘inhibit-quit’ is set and Emacs is not otherwise responding.

C-hig (elisp)Misc Events:

‘sigusr1’ ‘sigusr2’ These events are generated when the Emacs process receives the signals ‘SIGUSR1’ and ‘SIGUSR2’. They contain no additional data because signals do not carry additional information. They can be useful for debugging (*note Error Debugging::).

 To catch a user signal, bind the corresponding event to an
 interactive command in the ‘special-event-map’ (*note Active
 Keymaps::).  The command is called with no arguments, and the
 specific signal event is available in ‘last-input-event’.  For
 example:

      (defun sigusr-handler ()
        (interactive)
        (message "Caught signal %S" last-input-event))

      (define-key special-event-map [sigusr1] 'sigusr-handler)

 To test the signal handler, you can make Emacs send a signal to
 itself:

      (signal-process (emacs-pid) 'sigusr1)

C-hig (elisp)Event Examples:

To handle a SIGUSR1 signal, define an interactive function, and bind it to the ‘signal usr1’ event sequence:

 (defun usr1-handler ()
   (interactive)
   (message "Got USR1 signal"))
 (global-set-key [signal usr1] 'usr1-handler)

In addition, HUP, INT, and TERM are all relevant to the kill-emacs function:

C-hig (elisp)Killing Emacs:

The ‘kill-emacs’ function is normally called via the higher-level command ‘C-x C-c’ (‘save-buffers-kill-terminal’). *Note (emacs)Exiting::. It is also called automatically if Emacs receives a ‘SIGTERM’ or ‘SIGHUP’ operating system signal (e.g., when the controlling terminal is disconnected), or if it receives a ‘SIGINT’ signal while running in batch mode (*note Batch Mode::).

-- Variable: kill-emacs-hook This normal hook is run by ‘kill-emacs’, before it kills Emacs.

 Because ‘kill-emacs’ can be called in situations where user
 interaction is impossible (e.g., when the terminal is
 disconnected), functions on this hook should not attempt to
 interact with the user.  If you want to interact with the user when
 Emacs is shutting down, use ‘kill-emacs-query-functions’, described
 below.

The TSTP signal is mentioned in passing in (elisp)Suspending Emacs.

phils
  • 48,657
  • 3
  • 76
  • 115
  • For anyone else following this unfamiliar with the info system, you will need to go to the relevant entry, in parentheses, and then find the correct sub-heading. – Jason Hemann Jan 30 '23 at 22:37
  • The key sequence that I included before (almost) every node enables you to jump to a named info node directly, from anywhere (or if you're already in an info buffer then just `g` is sufficient). If you use that, there's no need to "find" anything manually (unless any of the node names I used are no longer valid; and notwithstanding that browsing the info manuals manually isn't a bad idea in general :). – phils Jan 31 '23 at 00:34
  • That said, the very best thing you can do if you're unfamiliar with using the info reader is to type `C-h i t` and work through the excellent info tutorial -- you'll learn lots of extremely useful things by doing that. – phils Jan 31 '23 at 00:36