views:

80

answers:

2

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?
+1  A: 

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.

Joshua
What would you do in it's place? (I've added the abstract requirements)
BCS
It's not a batch processing system, it's a server so the last bit is on target.
BCS
A: 

Your script will need to know what the original $old_dir is supposed to be. You can store the value in a file in /var/run/app_name for example.

It's still not clear to me why you'd want to do this. Sorry.

Edit:

Based on the Filesystem Hierarchy Standard, I'd say that /var/lib/appname may be an appropriate place to put your symlink.

Dennis Williamson
I plan of having `$old_dir` hard coded.
BCS
@BCS: Then I don't understand "Where should I put `$old_dir`?"
Dennis Williamson
/var/run/appname gets recreated every boot these days.
Joshua
@Joshua: True, but the OP didn't say that persistence beyond reboot was a requirement. Besides, each clarification is simply a restatement so I'm no closer to being able to offer solid help.
Dennis Williamson
Oh, I see how that would be misread. Try; What value should I hardcode `$old_dir` to? or Where should I put the symlink that I reference via `$old_dir`?
BCS
@BCS: See my edited answer.
Dennis Williamson