question on if cron can watch certon folders and be able to run a set of commands if theirs new stuff added
I am working on some automation stuff and I was wondering somethings. I am wondering does cron have a way when you make a crontab or make a cron job can cron be made to watch certon folders, and be able to run a set of commands if new stuff gets added to these folders? Here is why I am asking this question I am a blind user of linode, and I run my own radio station through a program called centovacast, and I have a set of playlists that are scheduled like the main automation playlist, and two of my friends who also have scheduled playlists one of my friends addes stuff some of the time to her stuff like when she wants stuff added she addes it to the folder, and she will have me or another friend of mine update the playlist through the command line, sence I can't use the drag and drop thing through the website. I have already created a shell script that will reindex centovacasts media library, remove all of the folders, from the playlist, and re add the folders. back in I want to be able to have a cron job that will be able to moniter and watch the folders that contain the files for the playlist and if their is new files that are added to run the commands in that script I have created like once an hour or so. to have it check the folder for changes I am not worryed if any files are removed, the playlist does not need to be updated when files are deleted just when added. Any help or suggestions on how I can do this with a little example on how this would work would be great. I already asked centovacast on this but they really didn't answer the question as far their software goes so am having to make my own way of doing it.
Yes, you should be able to do something like this with cron.
Cron is just a service that runs a command automatically (based on the timetable you specify). So, you'd need to either write a script that does what you want or create a command that does what you want.
I found an example script you could base this off of on the following page: How to Detect Changes in a Directory with Bash
DIR_TO_CHECK='/dir/to/check' OLD_STAT_FILE='/home/johndoe/old_stat.txt' if [ -e $OLD_STAT_FILE ] then OLD_STAT=`cat $OLD_STAT_FILE` else OLD_STAT="nothing" fi NEW_STAT=`stat -t $DIR_TO_CHECK` if [ "$OLD_STAT" != "$NEW_STAT" ] then echo 'Directory has changed. Do something!' # do whatever you want to do with the directory. # update the OLD_STAT_FILE echo $NEW_STAT > $OLD_STAT_FILE fi
This script uses the 'STAT' command to check for changes to a specific directory. You would need to change the value of
DIR_TO_CHECK to the directory that you want to check. And, you'd need to change the value of
OLD_STAT_FILE to a file that would store the directory status (in a different directory than you are checking).
Once you have the script squared away, you would want to add a line to your crontab to run the script every hour:
0 * * * * /opt/dir_checker.sh
This cron entry would run the script located at
/opt/dir_checker.sh every hour on the hour.
ionotify in Linux, and its cron equivalent
incron is an "inotify cron" system. It works like the regular cron but is driven by filesystem events instead of time events. This package provides two programs, a daemon called "incrond" (analogous to crond) and a table manipulator "incrontab" (like "crontab").
Chris, I think Ionotify and incron I think that would work better that way I can have it check folders, and be able to run my script I made for updateing the playlists. do you think I would need both ionotify and incron, for this to work? or does it install incron with ionotify.