I have a long running python script that I would like to be called via a udev
rule. The script should run unattended, in the background if you like. The udev RUN
is not suitable for long running commands which is getting in my way - my script gets killed after a wile.So I cannot call my script directly via udev.
I tried to disowning it by calling it in udev RUN via a shell script:
#!/bin/bash
/path/to/pythonscript.py & disown
This still got killed.
Now I was thinking to turn my script into a daemon, e.g. using pythons daemon
module. Now "all" I need to do is put a short command in my udev RUN
statement that will send some sort of trigger to my daemon, and things should be fine. What is less clear to me is the best way to implement this communication. I was thinking of adding maybe a jsonrpc service to my daemon, only listening on the loopback device. Is there a simpler or better way to achive what I want?
EDIT Thanks to the below comments I came up with the following solution. Main "daemon":
import signal, time
import auto_copy
SIGNAL_RECEIVED = False
def run_daemon():
signal.signal(signal.SIGUSR1, signal_handler)
while True:
time.sleep(1)
global SIGNAL_RECEIVED
if SIGNAL_RECEIVED:
auto_copy.auto_copy()
SIGNAL_RECEIVED = False
def signal_handler(dump1, dump2):
global SIGNAL_RECEIVED
SIGNAL_RECEIVED = True
if __name__ == "__main__":
auto_copy.setup_logging()
run_daemon()
This gets started via systemd, the unit file beeing
[Unitt]
Description=Start auto_copy.py script
[Service]
ExecStart=/home/isaac/bin/auto_copy_daemon.py
[Install]
WantedBy=multi-user.target
Then there is a udev rule:
SUBSYSTEM=="block", KERNEL=="sr0", ACTION=="change", RUN+="/home/isaac/bin/send_siguser1.sh"
and finally the script that sends the signal:
#!/bin/bash
kill -SIGUSR1 $(ps -elf |grep auto_copy_daemon.py |awk '/python/ {print $4}')
If anyone is interested, the project is on github.