Hi, maybe someone could help me optimizing this little script.
It works so far, but when running, reboot does not work. If kill inotifywait reboot from shell works. I think some optimization is required here.
This script starts at the end of the boot process, from an external device and starts a binary, logging the output, then inotify watches the config file, if changed it kills the binary and starts it again logging its output.
#!/bin/sh
wait 3
cmd="/usr/bin/binary -c /var/media/external/config.txt /var/media/external/config2.txt"
$cmd >> /var/media/external/binaries.log &
while inotifywait -e create,modify "/var/media/external/config2.txt"
do
echo "A file or directory was modified."
killall -9 binary
wait 3
$cmd >> /var/media/external/binaries.log &
done
At first glance I noticed the use of
wait 3
. I think you mean
sleep 3
1 Like
Yes.
And not only the binary should run in the background.
Unless the script is started in the background, it needs to bg itself:
(
while inotifywait -e create,modify "/var/media/external/config2.txt"
do
echo "A file or directory was modified."
killall -9 binary
sleep 3
$cmd >> /var/media/external/binaries.log &
done
) </dev/null >/dev/null 2>&1 &
Here is a quick bash mockup, using md5sum
without inotify tools.
Where myapp.sh
is being executed.
Kill is regular not dash 9, if you really need to use that signal, change it..
It will not check if myapp.sh
was started by hand outside script tho.
Of course, additional error handling could be implemented..
#!/usr/bin/bash
CONFDIR="/workdir/rst"
MYAPP="$CONFDIR/myapp.sh"
CSUM="$CONFDIR/config2.md5"
CONFFILE="$CONFDIR/config2.txt"
# Check interval in seconds
CHECKINT=5
cleanapp () {
kill $CURPID
}
startapp () {
if [ -z $CURPID ]
then
$MYAPP & CURPID=$!
md5sum $CONFFILE > $CSUM
else
md5sum -c $CSUM
if [ $? -gt 0 ]; then
kill $CURPID
$MYAPP & CURPID=$!
md5sum $CONFFILE > $CSUM
fi
fi
}
trap "cleanapp" 0
while true ; do
startapp
sleep $CHECKINT
# Check if MYAPP is on process list, if not clear CURPID so it can be started again
ps -p $CURPID || CURPID=""
done
Hope that helps
Regards
Peasant.