简体   繁体   English

检查符号链接是否已更改

[英]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. 我想要一个bash脚本来检查当前的符号链接是否与旧的符号链接(守护进程启动时)相同,如果没有,则重启守护进程。 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? 我应该为$old_dir使用什么,我应该把符号链接放在哪里?

As for a good approach, I'm not sure. 至于一个好的方法,我不确定。 Seems plenty workable but weird. 似乎很多可行,但很奇怪。

I'd put old_dir in /var/local/ projectname 我将old_dir放在/ 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. 在/ var / spool / appname / tmp中创建新的工作文件。
  2. File gets moved to /var/spool/appname (same filesystem required) 文件被移动到/ var / spool / appname(需要相同的文件系统)
  3. File gets picked up from /var/spool/appname, processed, and deleted (or moved if that is more appropriate) 文件从/ var / spool / appname中获取,处理和删除(如果更合适,则移动)

Step 2 is the atomic handoff. 第2步是原子切换。 Step 3 might have to be armored so processing the same file twice is harmless. 步骤3可能必须装甲,因此两次处理同一文件是无害的。

Then again, if you have something here like keeping a full history and the symlink refers to current you're doing it right already. 再说一遍,如果你在这里有一些东西,比如保留一个完整的历史记录,那么符号链接指的是当前你正在做的事情。

Your script will need to know what the original $old_dir is supposed to be. 您的脚本需要知道原始$old_dir应该是什么。 You can store the value in a file in /var/run/app_name for example. 例如,您可以将值存储在/var/run/app_name中的文件中。

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. 根据Filesystem Hierarchy Standard,我会说/var/lib/appname可能是放置符号链接的合适位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM