Method: SdNotify.notify

Defined in:
lib/sd_notify.rb

.notify(state, unset_env = false) ⇒ Fixnum?

Notify systemd with the provided state, via the notification socket, if any.

Generally this method will be used indirectly through the other methods of the library.

Parameters:

  • state (String)
  • unset_env (Boolean) (defaults to: false)

Returns:

  • (Fixnum, nil)

    the number of bytes written to the notification socket or nil if there was no socket to report to (eg. the program wasn’t started by systemd)

Raises:

  • (NotifyError)

    if there was an error communicating with the systemd socket

See Also:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/sd_notify.rb', line 106

def self.notify(state, unset_env=false)
  sock = ENV["NOTIFY_SOCKET"]

  return nil if !sock

  ENV.delete("NOTIFY_SOCKET") if unset_env

  begin
    Addrinfo.unix(sock, :DGRAM).connect do |s|
      s.close_on_exec = true
      s.write(state)
    end
  rescue StandardError => e
    raise NotifyError, "#{e.class}: #{e.message}", e.backtrace
  end
end