Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a symlink has changed

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?
  • What should I use for $old_dir, where should I put the symlink?
like image 760
BCS Avatar asked Dec 05 '25 15:12

BCS


1 Answers

As for a good approach, I'm not sure. Seems plenty workable but weird.

I'd put old_dir in /var/local/projectname

EDIT: Without seeing enough to truly know if leading you down the wrong path, I somehow think the correct way to do this is to use spooling directories.

The way this works is:

  1. New work file gets created in /var/spool/appname/tmp.
  2. File gets moved to /var/spool/appname (same filesystem required)
  3. File gets picked up from /var/spool/appname, processed, and deleted (or moved if that is more appropriate)

Step 2 is the atomic handoff. Step 3 might have to be armored so processing the same file twice is harmless.

Then again, if you have something here like keeping a full history and the symlink refers to current you're doing it right already.

like image 58
Joshua Avatar answered Dec 07 '25 07:12

Joshua



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!