[Reopened] Stack Inotifywait events in an array (skip first few posts)

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

[Reopened] Stack Inotifywait events in an array (skip first few posts)

Post by RemonK »

*** The original problem is solved! But need some help with array scripting! ***
*** See this post: viewtopic.php?p=2049632#p2049632 ***


I have some conky windows with all my ipcamera's. The camera's store pictures into my pure-ftp server. This ftp server keeps a log on PUT commands. I made a script that watches this log file on modification and if so it will read out the last line of that log and shows an overlay window on the specific camera that stored the pictures (ip adress) and plays a sound. Since the last update a day ago suddenly inotifywait does not react on modification of the log file anymore and i can't find out what the problem could be!? Does anyone know if there has been some changes to inotifywait ??

script

Code: Select all

#!/bin/bash
#
# Enable debugging on part of script = set -xe' 
# Disable debugging on part of script= set +xe
#
# Err number = $? 
# Script filename = $0
# Script PID = $$
#
#------------------------------------------------------------------------
#  This script watches /var/log/pure-ftpd/transfer.log and
#  presents an overlay window on ipcam when an event happens
#------------------------------------------------------------------------
#
#set -xe
#
# '--window-id=OVERLAY' MUST BE TE FIRST ARG !!!
#
#
# Use nothing = standard output, -D for debugging, -DD more debugging, -q for no output
args0=""
#
args1="--window-id=OVERLAY -i 15 -a top_right"
args2="--c /home/admin/.conky/MyPanels/ipcam_snapshots/ipcam_overlay_"
log="/var/log/pure-ftpd/transfer.log"
ftpansw="0"
ipcam="0.0.0.0"
lastlogline=""
 
# Show starting variables
echo -e "\n---------------------------------------------------------------------------------------------------"
echo    "                Watching Pure-FTP log on events, then start overlay on IP Camera's"
echo -e "---------------------------------------------------------------------------------------------------"
echo " log         : $log"
echo " lastlogline : $lastlogline"
echo " args        : $args"
echo " ftpansw     : $ftpansw"
echo " ipcam       : $ipcam"
echo -e "---------------------------------------------------------------------------------------------------\n\nStart watching $log ...\n"


# Function to show ERROR overlays
overlays_error () {
  ipcam="ERROR"
  ftpansw="Error in FTP log ! "
  export FTPANSW=$ftpansw
      echo -e "\nconky: message from script: ipcam_overlay.sh \033[31m"
      echo -e "       Logbestand $log is leeg of laatste regel is corrupt! Controleer het bestand."
      echo -e "       Programma blijft doordraaien. Let op melding op uw camerabeeld!\033[00m\n"
      allargs1=$args1" -x -670 -y -600 "$args2$ipcam".conky &" # Oprit
      allargs2=$args1" -x -670 -y -405 "$args2$ipcam".conky &" # Washok
      allargs3=$args1" -x -670 -y -213 "$args2$ipcam".conky &" # Aanbouw
      allargs4=$args1" -x -670 -y -22 "$args2$ipcam".conky &"  # Shelby
      echo "conky: -----| Overlays_error Args |-----"
      echo "allargs : "$allargs1
      echo "allargs : "$allargs2
      echo "allargs : "$allargs3
      echo "allargs : "$allargs4
      echo "-----------------------------------------------"
      conky $allargs1; conky $allargs2; conky $allargs3; conky $allargs4  # Start overlays
      conkykill=$(sudo pkill -fe "conky --window-id=OVERLAY")             # Kill overlays
      echo "pkill:" $conkykill
}


# Function to show IPCam overlays
overlays_ipcam () {
    ftpansw=$(echo "$lastlogline" | gawk -F " " '{print $8}')
    export FTPANSW=$ftpansw
    play -q -v 0.2 /home/admin/.conky/MyPanels/ipcam_snapshots/motion.wav pitch 350 speed 1.8
      echo -e "ftpansw : $ftpansw\nipcam   : $ipcam"
      allargs=$args0" "$args1" "$args2$ipcam".conky"
      echo "allargs : "$allargs
      conky $allargs
      conkykill=$(sudo pkill -fe "conky --window-id=OVERLAY")
      echo "pkill: "$conkykill
}


sudo inotifywait --quiet --monitor --event modify "$log" | while read; do
  echo -e "********* Event is happening in "$log

lastlogline=$(sudo tail -n1 "$log")
lastlogIPLen=$(sudo tail -n1 "$log" | awk -F " " '{print $1}')

if [[ -n "$lastlogline" ]]; then            #--- logline NOT EMPTY = GOOD
   if [[ ${#lastlogIPLen} -ge 7 ]]; then    #--- length IP correct = GOOD
      ipcam=$(echo "$lastlogline" | gawk -F " " '{print $1}')
      if [[ "$ipcam" =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then  #--- IP is OK!
        overlays_ipcam
      else                                  #--- else IP seems NOT OK! = BAD
        overlays_error
        exit
      fi
   else                                     #--- else length IP not correct = BAD
     overlays_error
     exit
   fi
else                                        #--- else lastlogline EMPTY = BAD
  overlays_error
fi
echo -e "conky: Still watching ...\n"

#exit
#break
#kill -n 15 $$

done
unset
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 4 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: Inotifywait suddenly does nothing on event modify

Post by 1000 »

You don't understand how it works.
Read options again from

Code: Select all

man inotifywait
And test the options one at a time in your terminal how they work.
Then you will see, that for example with --monitor the application never stops running.
If this application continues to run, the others can not be started.
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: Inotifywait suddenly does nothing on event modify

Post by RemonK »

Funny, cause it worked flawless for almost 7 months! Or i have dreamed it and that guy at superuser.com doesn't know what he's talking about then? But seriously, how can it work for about seven months and then after an update quit working..?
And if it did not work at forehand, why would i build a whole script around it?
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Inotifywait suddenly does nothing on event modify

Post by xenopeek »

This should work fine but does it need sudo? How are you prompted for the password on that?
sudo inotifywait --quiet --monitor --event modify "$log" | while read; do
You might instead make /var/log/pure-ftpd/transfer.log world readable or readable for your user.
Image
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: Inotifywait suddenly does nothing on event modify

Post by RemonK »

I have inotifywait in sudoers, so no password needed.
Pure-ftp makes the logs for root only..
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: Inotifywait suddenly does nothing on event modify

Post by 1000 »

I was wrong, above command inotifywait working for me.
But I still say to check in the terminal.

I was doing it wrong. Excuse me.
1. First, I just took a piece of code.

Code: Select all

 inotifywait --quiet --monitor --event modify file
After reading "man inotifywait" I thought the command was wrong.
Most of the commands which I used before required completion and exit. Maybe I have never used with " | while ".
| while read; do echo "Edited" ; done
But your command is correct and it working.

2. My second try.
Without " --monitor" option

Code: Select all

inotifywait --quiet  --event modify file | while read; do echo "Edited" ; done 
or just

Code: Select all

inotifywait --quiet  --event modify file |  echo "Edited" 
The command is closing but I didn't seen " Edited " in output.
Your command is used, so it is correct. https://superuser.com/questions/181517/ ... 32#1045932
I also I use inotifywait, but other way, without option monitor.

Code: Select all

while inotifywait  -q -e modify /var/log/iptables.log &>/dev/null ; do echo "Edited" ; done
I decided to check with my log. And my and your command working.
It didn't work when I edited the file manually.

Code: Select all

$ inxi -F
System:    Kernel: 5.11.0-22-lowlatency x86_64 bits: 64 Desktop: MATE 1.24.0 Distro: Linux Mint 20.1 Ulyssa 
I have system updated except one web browser.

Edited
When worked, I checked also this way.
First window with terminal

Code: Select all

inotifywait --quiet --monitor --event modify file | while read ; do echo "Edited" ; done
And second window with terminal

Code: Select all

echo "example" >> file
and worked.

( " dpkg -l inotify-tools " --> " inotify-tools 3.14-8 " )
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: Inotifywait suddenly does nothing on event modify

Post by RemonK »

Well.. shoot me! It works again, but the reason why???
Out of frustration i randomly changed how the arguments where placed after inotifywait, and it began to work again.
So i changed it back to how it was and now that also works?!?! A big WaTeFa!?! :roll:
And no, no typo's before or any other strange shit. I commented out the original line, started playing with a new line.. ten when it worked i commented the new one and commented-out the old one and it worked again. No clue what happened here!? :lol:

Why?? Why does Linux this to me??? :cry:
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: Inotifywait suddenly does nothing on event modify

Post by RemonK »

1000 wrote: Mon Jul 19, 2021 7:44 am It didn't work when I edited the file manually.
If you use an editor that makes a backup first then it does not work.
Disable the backup option and try again, then it works!
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by 1000 »

My editor doesn't make a copy, but if it makes a copy and I click save and I close the editor, then inotifywait should notify me?

I tested the same on a different system with a different editor.
There it notified only once and it freezes. ( I mean I need restart command with inotifywait, then echo "example" >> file working.
But there I use very very old system and older unstable inotifywait.
Here ( on new system Linux Mint ) I can't see any notification.
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by 1000 »

When I edited file with text editor I had event " DELETE_SELF "

Code: Select all

$ inotifywait -m --format "%e %f" file
Setting up watches.
Watches established.
....
OPEN 
CLOSE_WRITE,CLOSE 
ATTRIB 
DELETE_SELF 
https://stackoverflow.com/questions/275 ... ing-a-file
Inotify cannot know that the file that was deleted and the file that was created in its place are logically related.
I tried rename file, move file and
DELETE_SELF event is when file is deleted.
mmphosis
Level 1
Level 1
Posts: 25
Joined: Sat Apr 11, 2020 11:22 pm

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by mmphosis »

Thanks for posting this thread. I now have a script that uses inotifywait to automate a workflow!
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by RemonK »

You're welcome! :wink:
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by 1000 »

Something else.
I am trying to end my own script which watching the log.
The log is archived once a day.
and for me it is a big problem when a script freezes when a file has been deleted or moved.

And I noticed that the command above ends its work.
Whereas, my and your command only watches modifications, so it cannot exit.
So I read man inotifywait and I played around in the terminal.
In this example you can see " modify,delete_self,move_self "

Code: Select all

$ while inotifywait  -e modify,delete_self,move_self --format "%e" file ; do echo Next_Line ; done ; echo "Exit with status $?"
Setting up watches.
Watches established.
MODIFY
Next_Line
Setting up watches.
Watches established.
MOVE_SELF
Next_Line
Setting up watches.
Couldn't watch file: No such file or directory
Exit with status 0
In the second terminal I used the commands
before " while inotifywait ... " in first terminal

Code: Select all

$ echo file >> file
And after " while inotifywait ... " again in the second terminal

Code: Select all

$ echo file >> file
$ mv file file.old 
As a result of the above, you may notice that the loop has ended and the last command has been executed
echo "Exit with status $?"
Instead of this, you can add info or error that the file does not exist or has been moved
and add another loop if you need.
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by RemonK »

My script does not have to end, so exiting the script has no priority.
But about the deletion or rotation of the log file there you have a point. I don't know when the log rotates (not every day, maybe once a week or longer?) if then the script still works? As i just read your post i watched my camera's and when the cat walked by it did not notify me! So it stopped.. when i checked the log it was pretty fresh with only 20 lines in it. So the log recently rotated! My system is running 24/7, but i do often a reboot when i'm messing around with it. Maybe that's why i did not notice that it stopped now and then.. cause after a reboot it sees the new log and works as normal.

Thanks, i'm going to upgrade the script so it notice the log rotation!

[edit] btw, what's the best way to restart the script? Just execute it again from within the script and then exit?
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by 1000 »

You probably have two ways to choose from

1. Find and edit file in /etc/logrotate.d/ for your log

You need add new script or command to restart your script.

Example how add new script with "postrotate" option in logrotate
https://unix.stackexchange.com/question ... tated-file


2. Or edit your script

I only remind you.
From man inotifywait
move_self
A watched file or directory was moved. After this event, the file or directory is no longer being watched.

delete_self
A watched file or directory was deleted. After this event the file or directory is no longer being watched. Note that this event can occur even if it is
not explicitly being listened for.
From you
RemonK wrote:My script does not have to end, so exiting the script has no priority.
You need to close inotifywait, but you don't need to close the script.
Because,
when file is removed, then inotifywait can not watch, but still working.
Even if you create a new file for him, he can not see it. He is still pending changes to the old file.
Because the new file and the old file with the same name are two different files for inotifywait and I guess also for hard drive.

From internet
Which is faster: Copying or Moving files? Why?
...
Generally, Moving files will be faster because when moving, it will just change the links, not the Actual Position on the physical device.
( So you can suspect:
- A new file is created in a different location on the disk
- Inotifywait observes the location of the old file )

This is reason why I used move_self and delete_self for exit from inotifywait when file was removed.


Don't believe me. Just check with any example. :mrgreen:


My examples for terminal:

Code: Select all

MY_TIME=$(date "+%m/%d %H:%M:%S")  ; \
while : ; do \
if [[ ! -f file ]] ; then echo "$MY_TIME  Message to log: File not exist." | tee -a my.log ; sleep 5s ; continue ; \
else echo "$MY_TIME  Message to log: Watching started" | tee -a my.log ; \
fi ; \
EVENT_1=$(while EVENT_2=$(inotifywait  -e modify,delete_self,move_self --format "%e" file)  ; do \
echo "$MY_TIME  Message to log ; $EVENT_2" | tee -a my.log; \
done) ; \
echo "$MY_TIME  Message to log ; $EVENT_1" | tee -a my.log ; sleep 5s ; \
done

Code: Select all

MY_TIME=$(date "+%m/%d %H:%M:%S")  ; \
while : ; do \
  echo "$MY_TIME  Message 1 to log ; Starting watching." | tee -a my.log ; \
  while EVENT_2=$(inotifywait  -e modify,delete_self,move_self,delete_self --format "%e" file)  ; do \
    echo "$MY_TIME  Message 2 to log ; $EVENT_2" | tee -a my.log; \
  done ; \
  echo "$MY_TIME  Message 3 to log ; File not exist" | tee -a my.log ; sleep 5s ; \
done
while : --> infinite loop
sleep 5s --> I used it because I can't constantly watch with inotifywait , if a new file not exist but will created. ( edited )
The example checks every five seconds to see if the file already exists. So the example is not perfect.
I can probably only watch the changes in a given folder with inotifywait. But then it is best if the log file will in a separate folder.
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by RemonK »

I have changed the script and made of everything a function. But now i have to catch what event was triggered (modify or delete_self) and i can't seem to find out a nice way to catch that.
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
1000
Level 6
Level 6
Posts: 1039
Joined: Wed Jul 29, 2020 2:14 am

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by 1000 »

You have it in the examples above.
Where

Code: Select all

while EVENT_2=$(inotifywait  -e modify,delete_self,move_self,delete_self --format "%e" file)  ; do \
" while " loop performs / executes " inotifywait " and the result of this command is saved to " EVENT_2 " variable.
then inside loop " while " you have

Code: Select all

echo "$MY_TIME  Message 2 to log ; $EVENT_2" | tee -a my.log; \
which this will display
- time " $MY_TIME " variable .( date "+%m/%d %H:%M:%S" )
- text " Message 2 to log ; "
- and " $EVENT_2 " variable ( result of inotifywait command )
And all this is redirected to the " tee -a my.log "command,
which will display and at the same time save the result to " my.log " file.

Generally very long commands in the terminal are very hard to edit.
I used a text editor to break commands into lines. ( for better visibility )
For the terminal the end of the previous line must be terminated " \ " ( slash ).
Inside scripts we don't need use " \ "
( edit : not / , but \ , sorry :lol: )
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: [Solved by mystery] Inotifywait suddenly does nothing on event modify

Post by RemonK »

I tried something like that but got no response when the log was modified or deleted! But i'm going to try it with your line, see if it reacts on events.

Why do you have 2 times the delete_self event added if i may ask?


[edit] Ok, i have it going now! But one thing.. my camera's all snaps 3 pictures at a motion. So when i quickly modify my log 3x times in a row it only warns me once! So the while/do routine halts when it goes to the 'warn me' routine. It does not memorize other modifications of the log!
In my previous script the watch kept going and stored every modification in memory and when the 'warn me' routine finished it went to the next until there where no modifications stored.
Now i can't tell if all 3 uploads went correct.. it only warns me of the first upload and log mod.
When the connection is a little slow it maybe warn me 3 times because the routine to warn me is quicker then the upload of the snapshot, but when connection is fast it will warn me only once.

At the moment i like my script better cause the while/do routine never stops! If an event goes off it does a routine but in the meanwhile it still watches for events. If the warn me routine is finished it does the next one of there is one!
I only want to determine what event went off.. so i can direct it to the 'warn me' routine if modify was the event, or i can direct it to the 'restart script' routine if the delete_self was the event.

[edit2] Is there a way to start a routine and don't wait for it? Just like the & after a command? That seems not working on a routine... That way i can start the 'warn me' routine and immediately restart the log_watch.
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: [Reopened] Stack Inotifywait events in an array

Post by RemonK »

It costs me all night, but i finally got it working! :D
When it starts and there is no log i get a popup. When it is running and the log suddenly disappears i get a popup. For the rest it is doing the modify routine or when log is rotated (i hope) it does a restart of the log watch. On all other errors it does a restart of the script.
Time will tell how my scripting skills are... :mrgreen:

Code: Select all

#!/bin/bash
#
######################################################################
#      This script watches /var/log/pure-ftpd/transfer.log           #
#      and present a conky overlay window on ipcams window           #
#      when an event happens.                                        #
######################################################################
#
# Err number = $? 
# Script filename = $0
# Script PID = $$
#
# Enable debugging on part of script = set -xe' 
# Disable debugging on part of script= set +xe
#set -xe
#
# '--window-id=OVERLAY' MUST BE TE FIRST ARG !!!
#
# Use nothing = standard output, -D for debugging, -DD more debugging, -q for no output
args0=""
#
args1="--window-id=OVERLAY -i 15 -a top_right"
args2="--c /home/admin/.conky/MyPanels/ipcam_snapshots/ipcam_overlay_"
log="/var/log/pure-ftpd/transfer.log"
ftpansw="0"
ipcam="0.0.0.0"
lastlogline=""
#
### Show starting variables ###
echo -e "\n---------------------------------------------------------------------------------------------------"
echo    "                Watching Pure-FTP log on events, then start overlay on IP Camera's"
echo -e "---------------------------------------------------------------------------------------------------"
echo " PID         : $$"
echo " log         : $log"
echo " lastlogline : $lastlogline"
echo " args        : $args"
echo " ftpansw     : $ftpansw"
echo " ipcam       : $ipcam"
echo -e "---------------------------------------------------------------------------------------------------\n"


### Function to show overlays with error ###
overlays_error () {
  ipcam="ERROR"
  ftpansw="Error in FTP log ! "
  export FTPANSW=$ftpansw
      echo -e "\nconky: message from script: ipcam_overlay.sh \033[31m"
      echo -e "       Logbestand $log is leeg of laatste regel is corrupt! Controleer het bestand."
      echo -e "       Programma blijft doordraaien. Let op melding op uw camerabeeld!\033[00m\n"
      allargs1=$args1" -x -670 -y -600 "$args2$ipcam".conky &" # Oprit
      allargs2=$args1" -x -670 -y -405 "$args2$ipcam".conky &" # Washok
      allargs3=$args1" -x -670 -y -213 "$args2$ipcam".conky &" # Aanbouw
      allargs4=$args1" -x -670 -y -22 "$args2$ipcam".conky &"  # Shelby
      echo "conky: -----| Overlays_error Args |-----"
      echo "allargs : "$allargs1
      echo "allargs : "$allargs2
      echo "allargs : "$allargs3
      echo "allargs : "$allargs4
      echo "-----------------------------------------------"
      conky $allargs1; conky $allargs2; conky $allargs3; conky $allargs4  # Start overlays
      conkykill=$(sudo pkill -fe "conky --window-id=OVERLAY")             # Kill overlays
      echo "pkill:" $conkykill
}


### Function to show overlays with IPCam/ftp message ###
overlays_ipcam () {
    ftpansw=$(echo "$lastlogline" | gawk -F " " '{print $8}')
    export FTPANSW=$ftpansw
    play -q -v 0.2 /home/admin/.conky/MyPanels/ipcam_snapshots/motion.wav pitch 350 speed 1.8
      echo -e "ftpansw : $ftpansw\nipcam   : $ipcam"
      allargs=$args0" "$args1" "$args2$ipcam".conky"
      echo "allargs : "$allargs
      conky $allargs
      conkykill=$(sudo pkill -fe "conky --window-id=OVERLAY")
      echo "pkill: "$conkykill
}


### Function when log is modified ###
log_modify () {
echo "*** Activated by $event event"
 lastlogline=$(sudo tail -n1 "$log")
 lastlogIPLen=$(sudo tail -n1 "$log" | awk -F " " '{print $1}')

if [[ -n "$lastlogline" ]]; then           #--- logline NOT EMPTY = GOOD
   if [[ ${#lastlogIPLen} -ge 7 ]]; then    #--- length IP correct = GOOD
      ipcam=$(echo "$lastlogline" | gawk -F " " '{print $1}')
      if [[ "$ipcam" =~ ^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then  #--- IP is OK!
        overlays_ipcam
      else                         #--- else IP seems NOT OK! = BAD
        overlays_error
        exit
      fi
   else                          #--- else length IP not correct = BAD
     overlays_error
     exit
   fi
else                           #--- else lastlogline EMPTY = BAD
  overlays_error
fi
echo -e "conky: Still watching ...\n"
}


### Start monitoring of logfile ###
log_watch () {
  while event=$(inotifywait -e modify,delete_self,move_self --format %e $log); do do_while; done

  # Should not come here! Better warn with a buzz and popup!
  errmsg="\nExit: $?\nEvent: $event\n\n Programm exit because $log\n does not exist or other error! Check your buggy script!"
do_popup
}


### Function to determine event and perform action ###
do_while () {
   if [ $event = "MODIFY" ]; then 
      echo "*** Starting $event routine, log watch is set again"
      log_modify &
      log_watch
   elif [ $event = "DELETE_SELF" ]; then 
      echo "*** Starting $event routine, log watch is set again"
      # Check if log is rotated and replaced, or it's actually gone!
       if [ -f $log ]; then
         # log is replaced, restart watch
         log_watch
       else
         # log is gone, do popup
         errmsg="\nExit: $?\nEvent: $event\n\n $log not found!\n Check if file exists!"
         do_popup
       fi
   else
      echo -e "*** Unknown event '$event' happened!\n *** Restart script.."; restart_script
   fi
 errmsg="\nExit: $?\nEvent: $event\n\n An exit without an event happening!\n This only happens when inotify suddenly\
 stopped, the monitored file, is completely gone or other weird shit! Check your buggy script!"
 do_popup
}


### Popup important error message ###
do_popup () {
 play -q -v 0.2 /home/admin/.conky/MyPanels/ipcam_snapshots/buzzer.wav pitch -1000 speed 1.5
  echo -e $errmsg | yad --text-info --margins=6 --back=#181818\
     --title="Inotifywait error" --listen\
     --center --fixed --on-top --wrap\
     --tail --always-print-result --on-top\
     --fontname="Terminus Regular 14"\
     --text-align=left\
     --width=700 --height=250\
     --button="OK" &
exit
}


### Function restart script ###
restart_script () {
 echo "Restart script!"
  SCRIPTPATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"  #" Get realpath of this script
  $SCRIPTPATH &                      # Execute new instance of this script
exit                               # Kill this script
}

log_watch
Last edited by RemonK on Thu Aug 05, 2021 8:07 am, edited 1 time in total.
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
RemonK
Level 4
Level 4
Posts: 395
Joined: Wed Feb 06, 2019 6:32 pm
Location: Land van Umme
Contact:

Re: [Reopened] Stack Inotifywait events in an array

Post by RemonK »

The first bug presented itself already... now the events does not stack up in memory, but the routine immediately is called. So when the 3 snapshot are uploaded very quickly it almost also calls the modify routine simultaneously! What results in a messy overlay.
So i have to stack the events in an array or something of that kind, but i'm not that strong in array scripting.... :(
Het leven is net een kartbaan; Sensatie, adrenaline en veel te snel voorbij!
Joeptjoep - Feestboek
Locked

Return to “Scripts & Bash”