Inotifywait restart script prevents reboot when started


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inotifywait restart script prevents reboot when started
# 1  
Old 08-31-2019
Inotifywait restart script prevents reboot when started

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.


Code:
#!/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

# 2  
Old 08-31-2019
At first glance I noticed the use of
Code:
wait 3

. I think you mean
Code:
sleep 3

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-31-2019
Yes.
And not only the binary should run in the background.
Unless the script is started in the background, it needs to bg itself:
Code:
(
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 &

# 4  
Old 08-31-2019
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..

Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Changing "rx_queue_number" in "ixgbe.conf". Reboot or Network Restart?

Hi all, First post here. Working on Solaris 10, on a Sun t4-4, need to change RX queue depth(ethernet, not HBA) and was wondering if i could get by with just restarting the network or if i should just bounce the whole shebang. Apologies if i missed a similar thread. if there is one, please... (2 Replies)
Discussion started by: caspnx
2 Replies

2. Shell Programming and Scripting

Sed: deleting last line prevents '$' address from working in the multi-script invocation

It looks like if matching and deleting the last line confuses 'sed' so it does not recognize '$' address. Consider: sed -e '/^3/d' -e '$ a text' supposed to delete a line starting with '3' and then append 'text' after the last line of input. But, if it is the last line of input which starts... (2 Replies)
Discussion started by: msz59
2 Replies

3. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

4. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

5. Shell Programming and Scripting

Help with my BASH script with WINE and inotifywait

I'm trying to improve a script that I have running on a Ubuntu desktop that basically runs a powerpoint presentaions with announcements to TV's in diffrent parts of our building. My current script uses WINE to execute powerpoint viewer. The scripts that I currently use relies on inotifywait's... (1 Reply)
Discussion started by: binary-ninja
1 Replies

6. Shell Programming and Scripting

Inotifywait tool in no there in Redhat 5.6

Hi, I want to monitor any new file to be created in some of the directory using inotifywait tool but this is not available in Redhat 5.6. Could you please let me know how to achieve (monitoring of file) without using inotifywait too? Because i dnt want to install this tool due to some reason.... (1 Reply)
Discussion started by: Pawan Kumar
1 Replies

7. Shell Programming and Scripting

Script using inotifywait

Is there a way to get my script to only trigger when a .ppt is created? #!/bin/bash while inotifywait -e create /ticker/powerpointshare; do sleep 30; sudo chmod -R 777 /ticker/powerpointshare/*.*; sleep 15; sudo reboot; done I'm not sure if this is even possible... (0 Replies)
Discussion started by: binary-ninja
0 Replies

8. Shell Programming and Scripting

inotifywait to wait until completion

Hi, I am using inotifywait to monitor a directory where files are being transferred into. I want inotifywait to tell me when a file has been completely transferred not just part of it. I tried "create", "close" and "close_write" but it seems that inotifywait always gets triggered even if the... (4 Replies)
Discussion started by: jejeking
4 Replies

9. AIX

xntpd starts after reboot only when HACMP services are started ?

Hello, Running AIX 6.1, AIX machine is HACMP node. Recently I set up ntp service. Started xntpd by hand - everythig is OK. Configured xntpd to start after reboot and rebooted the machine. After reboot checked xntpd: # lssrc -a|grep ntp xntpd tcpip ... (5 Replies)
Discussion started by: vilius
5 Replies

10. Shell Programming and Scripting

need some help getting started on this script

Hi. I am just getting into scripting. I came into a situation where I need to go through several hundred files on a Linux system and find a couple specific bits of information from within each file. All the files have pretty much identical content except for a view data values on the same two lines... (4 Replies)
Discussion started by: Dave247
4 Replies
Login or Register to Ask a Question