Search code examples
linuxraspberry-piraspbian

Restarting crashed drivers on Raspberry Pi


I am currently working on a NFC system using the ACR122U reader and not using the constructor drivers which lead to some occasionnal crashes of those drivers. The problem here is that when it crashes, the whole process isn't crashed, my program keep running but the drivers don't. (No need to say that it makes my code useless) I am aware of the ways to restart a crashed program but not crashed drivers. I thought of using a watchdog to hard reset the raspberry but needless to say that a reboot isn't the best choice because of the time it takes. ( I am using the very first Raspberry). So, is there a way to reboot only the driver and more important, detect when it fails ?


Solution

  • I found a solution to my own problem after many hours of research and trials. The solution is actually very simple : just a background running script (my program in my case), and a check using grep, every two seconds :

    #!/usr/bin/env bash
    
    command="/your/path/to/your_script"
    log="prog.log"
    
    match="error libnfc"
    
    matchnosp="$(echo -e "${match}" | tr -d '[:space:]')"
    
    $command > "$log" 2>&1 &
    
    pid=$!
    
    while sleep 2
    do
        if fgrep --quiet "$matchnosp" "$log"
        then
             echo "SOME MESSAGE"
             kill $(pidof your_script)
             $command > "$log" 2>&1 &
             sleep 5
             truncate -s 0 $log      
             echo "SOME OTHER MESSAGE..."
       fi
    done
    

    This restart the program when some message matching "error libnfc" is found in the log file.