Check if a symlink has changed
- by BCS
I have a daemon that, when it's started, loads its data from a directory that happens to be a symlink. Periodically, new data is generated and the symlink updated. I want a bash script that will check if the current symlink is the same as the old one (that the daemon started with) and if not, restart the daemon. My current thought is:
if [[ ! -e $old_dir || $(readlink "$data_dir") == $(readlink "$old_dir") ]];
then
echo restart
...
ln "$(readlink "$data_dir")" "$old_dir" -sf
else
echo no restart
fi
The abstract requirement is: each time the script runs, it needs to check if a symlink on a given path now points to a something other than it did the last time and if so do something. (The alternative would be to check if the data at the path has changed but I don't see that being any cleaner.)
My questions:
Is this a good approach?
Does anyone have a better idea?
Where should I put $old_dir?