I am trying to write a small clipboard logger (for linux) that listens for an event (the change of the clipboard content) and writes the clipboard content to a file (on-change).
What I have come up with is a simple while loop with the clipboard module pyperclip:
import pyperclip
recent_value = ""
while True:
tmp_value = pyperclip.paste()
if tmp_value != recent_value:
recent_value = tmp_value
with open(".clipboard_history.txt", "a") as f:
f.write(recent_value + "\n")
So my first question is, can I actually run a while True loop to 'listen' or will this consume too much memory or be generally inefficient or bad practice?
And the second question is, how can I run this in the background like a shell job control (ampersand)?
Should I go for a daemon like suggested here or some kind of event loop or threading magic?
I basically want something that sits in the background and listens for an event (clipboard content changes), reacts on it (writes to a file) and waits again.
============================
edit: Thanks for the input! + new question: Would I still need the sleep method if I used threading?