Check if a symlink has changed

Posted by BCS on Stack Overflow See other posts from Stack Overflow or by BCS
Published on 2010-06-04T22:27:24Z Indexed on 2010/06/05 14:22 UTC
Read the original article Hit count: 188

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about bash

Related posts about conventions