检查符号链接是否已更改
我有一个守护进程,当它启动时,它从恰好是符号链接的目录加载其数据。定期生成新数据并更新符号链接。我想要一个 bash 脚本来检查当前的符号链接是否与旧的符号链接(守护进程启动时使用的符号链接)相同,如果不是,则重新启动守护进程。我当前的想法是:
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
抽象要求是:每次脚本运行时,它都需要检查给定路径上的符号链接现在是否指向上次以外的其他位置,如果是,则执行某些操作。 (另一种方法是检查路径上的数据是否已更改,但我认为这并没有变得更干净。) 我的问题:
- 这是一个好方法吗?
- 有人有更好的主意吗?
$old_dir
我应该使用什么,我应该将符号链接放在哪里?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于好的方法,我不确定。看起来很可行但很奇怪。
我将 old_dir 放在 /var/local/projectname 中
编辑:没有看到足够的信息来真正知道是否会导致您走上错误的道路,我以某种方式认为正确的方法是使用假脱机目录。
其工作方式是:
第 2 步是原子切换。步骤 3 可能必须经过防护,因此处理同一文件两次是无害的。
话又说回来,如果你在这里有一些东西,比如保留完整的历史记录,并且符号链接引用当前的内容,那么你已经做得对了。
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:
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.
您的脚本需要知道原始的
$old_dir
应该是什么。例如,您可以将该值存储在/var/run/app_name
中的文件中。我仍然不清楚你为什么要这样做。对不起。
编辑:
根据文件系统层次结构标准,我想说
/var/lib/appname
可能是放置符号链接的合适位置。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.