  migrate_home
  
  if command_exists gdb; then
-@@ -163,8 +189,13 @@ LOOP=1
+@@ -163,8 +190,13 @@ LOOP=1
  while [ $(( $LOOP )) = "1" ]
  do
    LOOP=0
 -  "$LIBDIR/${bin_name}/${bin_name}.bin" $SAVED_ARGS
 +  "$LIBDIR/${bin_name}/${bin_name}.bin" $SAVED_ARGS &
-+  PID=$!
-+  echo $PID >$PIDFILE
-+  wait $PID
++  KODI_PID=$!
++
++  wait $KODI_PID
    RET=$?
-+  rm -f $PIDFILE
-+  PID=
++  KODI_PID=
++
    if [ $(( $RET == 65 )) = "1" ]
    then # User requested to restart app
      LOOP=1

If running through systemd, combine this patch with KillMode=mixed
so only the wrapper script gets SIGTERM on service stop/restart.
---
 tools/Linux/kodi.sh.in | 37 ++++++++++++++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/tools/Linux/kodi.sh.in b/tools/Linux/kodi.sh.in
index 0a00a32034..aae0badeb6 100644
--- a/tools/Linux/kodi.sh.in
+++ b/tools/Linux/kodi.sh.in
@@ -28,6 +28,10 @@ LIBDIR="@libdir@"
 CRASHLOG_DIR=${CRASHLOG_DIR:-$HOME}
 USERDATA_DIR="${HOME}/.${bin_name}"
 
+PIDFILE=${USERDATA_DIR}/kodi.bin.pid
+KODI_SEND="@bindir@/kodi-send"
+KODI_SEND_PORT=9777
+
 
 # Workaround for high CPU load with nvidia GFX
 export __GL_YIELD=USLEEP
@@ -94,8 +98,8 @@ print_crash_report()
   uname -rvs >> $FILE
   printf " Release: " >> $FILE
   if [ -f /etc/os-release ]; then

From 9c996962788abb1ba3c9fc51618c63128ba23cb7 Mon Sep 17 00:00:00 2001
From: Allen Wild <allenwild93@gmail.com>
Date: Mon, 15 May 2017 00:26:32 -0400
Subject: [PATCH] Graceful Shutdown Support
