Solvedklipper Auto-reconnect

I might be the only one, but I sometimes turn my printer off.
(Sorry, could not resist putting a full stop there).
As I run OP and Klipper on a laptop, I don't turn THAT part off, so it's only the printer itself. What would be nice, if Klipper host could ping MCU every once in a while and auto-reconnect when the printer is back on. Currently I have to restart service to get connection.

43 Answers

✔️Accepted Answer

@Avalonnw I'm also one of the few people that actually switch off their printer, but keep Klipper running. I solved the "manual Klippy restart" problem by adding a udev rule to the linux environment which runs klippy. I created a new udev rule /etc/udev/rules.d/98-klipper.rules with the content:

## rule to restart klipper when the printer is connected via usb
SUBSYSTEM=="usb", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", ACTION=="add", RUN+="/bin/sh -c '/bin/echo RESTART > /tmp/printer'"

And now klippy gets a RESTART command sent whenever I connect or power on my printer. This only happens when the USB device is added to the system and not when Klippy automatically disconnects due to an error situation. Therefore the risk which Kevin mentioned above should be avoided.
Note that you'd have to change the product-id and vendor-id in above rule according to your setup (can be figured out via lsusb)

Other Answers:

I couldn't get the udev rule to work, because on my Linux system (armbian) /tmp had the 't' flag - and /tmp/printer couldn't be accessed by root - it would give me the error 'failed with exit code 2'

Fixed this by changing the command that the udev rule runs, using sudo to run as the Klipper user :

## rule to restart klipper when the printer is connected via usb
SUBSYSTEM=="usb", ATTRS{idVendor}=="10c4", ATTRS{idProduct}=="ea60", ACTION=="add", RUN+="/usr/bin/sudo -u <username> /bin/sh -c '/bin/echo RESTART > /tmp/printer'"

where <username> is the user that starts Klipper

I couldn't get any of the previous rules to work; I got permission denied (even as root!!) when trying to echo to either /run/klipper/sock (equivalent to /tmp/printer) or /dev/pts/0.

I've worked around this by using udev to restart the klipper.service.

## rule to restart klipper when the printer is connected via usb
SUBSYSTEM=="usb", ATTRS{idVendor}=="1d50", ATTRS{idProduct}=="614e", ACTION=="add", RUN+="/bin/sh -c '/usr/bin/systemctl restart klipper.service'"

This works for Arch Linux and its klipper package; I'm unsure about OctoPi or Rasbian.