Do it once by hand, do it twice with cron inotifywait will exit after that if the file is not there. Note the timeout in seconds (28,800 = 8 hours). #sudo /usr/sbin/lshw -json > $HOME/lshw.json So if a random file shows up on $HOME, it won't break the wait cycle, but if the file you're looking for shows up there and is fully written, it will exit the loop: #/usr/bin/touch $HOME/randomfilenobodycares.txt Test "$(/usr/bin/inotifywait -timeout 28800 -quiet -syslog -event close_write "$(/usr/bin/dirname "$LSHW_FILE")" -format '%w%f')" = "$LSHW_FILE" & break What happens if the file never arrives? You could use the timeout tool or more complex logic to handle this scenario.Īnother alternative is to use the Inotify API with inotify-tools and do a better, version 2 of the script using inotifywait: #!/bin/bash.If the system sleeps too little, it wastes CPU cycles.If the file arrives one second after the process starts sleeping, you wait 29 seconds. There are a few problems with this approach: + /usr/bin/jq '.|.capabilities' /home/josevnz/lshw.json usr/bin/jq ".|.capabilities" "$LSHW_FILE"|| exit 100Ī magic process generates the file for while you are waiting: # sudo /usr/sbin/lshw -json > $HOME/lshw.jsonĪnd you wait until the file arrives. # Enable the debug just to show what is going on. # Wait for a file to arrive and once is there process it I wrote this script ( version 1) to handle this situation: #!/bin/bash Once it does, you want to start processing it. Here's another type of problem: You are waiting for a file named $HOME/lshw.json to arrive. WorkingWithDateAndTime.shĭownloaded: /home/josevnz/covid19-vaccinations-town-age-grp.csv Wait for a file using inotify tools If the conditions are met, the output will be something like this. # COVID-19 Vaccinations by Town and Age Group Report_file="$HOME/covid19-vaccinations-town-age-grp.csv" # No updates during the weekend, so don't bother (not an error) Test "$hour" -gt 18 & return 0|| return 1 Test "$day_of_week" -ge 1 -a "$day_of_week" -le 5 & return 0 || return 1 # Simple script that shows how to work with dates and timesĭay_of_week=$(/usr/bin/date +%u)|| exit 100 You can get the current day of the week and hour of the day and perform a few comparisons with a simple script: #!/bin/bash To see the full list, just type: # /usr/bin/date -helpīack to your script. GNU /usr/bin/date supports special format flags with the sign +. It is after 6PM (there are no updates earlier in the day).It is during the week (there are no updates to the data made over the weekend).Say you want a script to download the COVID-19 Vaccinations by Town and Age Group dataset from the state of Connecticut when the following conditions exist: You can find the code for this article in my GitHub repository. Apache Airflow is an excellent tool for this type of situation. Run many tasks on different machines-some with complex relationships.Run your program at a specific time based on conditions by using atq.Wait for a file without knowing for how long with inotify tools.Format dates and use them as conditions to make your program wait before moving to the next stage.How well do you know Linux? Take a quiz and get a badgeīy the end of this article, you should be able to do the following:.Linux system administration skills assessment.A guide to installing applications on Linux.Download RHEL 9 at no charge through the Red Hat Developer program.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |