使程序重新加载文件更改已打开的文件 [英] make a program reload an already opened file on file change

查看:184
本文介绍了使程序重新加载文件更改已打开的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要玩瑞士法郎文件然后在文件更改为重新加载新的内容播放器。可悲的是,我选择的程序没有做显示器更改的文件。我想出了一个有点复杂的解决方案:

I need to play a .swf file then when the file is changed to reload the player with new content. Sadly, my program of choice does not do monitor files for changes. I came up with a bit convoluted solution:

sudo apt-get install incron gtk-gnash
echo "my_username" | sudo tee -a /etc/incron.allow # allow my_username to run incron
incrontab -e

以下内容添加到incrontab:

add the following to incrontab:

/home/my_username/path/myfile.swf IN_MODIFY /home/my_username/path/run.sh

run.sh 包含:(也搭配chmod 700 run.sh

#!/bin/sh
killall gtk-gnash
gtk-gnash /home/my_username/path/myfile.swf

正如你可以看到这是远离优雅。我还能怎么去呢?

As you can see this is far from elegant. How else could I go about this?

这是在Ubuntu 12.04如果你很重要。这是我自己的问题对askubuntu重复

This is on Ubuntu 12.04 if that matters for you. This is a duplicate of my own question on askubuntu

编辑:澄清,我选择使用GTK的咬牙切齿,一个独立的球员,这不是必需的,Web浏览器会做太多,但似乎没有必要

clarification, I chose to use gtk-gnash, a standalone player, this is not required, a web browser would do too but seems not necessary

推荐答案

什么你所描述的是一个的文件变更监视器

What you're describing is the job of a File Alteration Monitor.

首先,现有的incrontab解决方案是一个很好的起点,因为你是从外壳推出的东西。事实incrontab是 inotify的(7)基于解决方案是一个巨大的优势。这意味着,你是不是轮询,从而节省CPU。

First off, your existing "incrontab" solution is a good starting point, given that you're launching things from shell. The fact that incrontab is an inotify(7)-based solution is a huge plus. It means that you aren't polling, which saves CPU.

你问的外壳和系统级解决方案,而我不是一个Flash程序员,所以我会坚持你的问题,但我认为一个更好的解决方案是创建一个Flash包装,也许使用一个AS3 Loader类在现有的SWF吸吮和一些由incrontab推出接收通知。 Flash编程是远远超出范围的这个答案,虽然它可能会提供更好的解决方案。

You're asking for shell and system-level solutions, and I'm not a Flash programmer, so I'll stick to your question, though I think a better solution would be to create a Flash "wrapper" that perhaps uses an AS3 Loader class to suck in your existing SWF and receive notifications from something launched by incrontab. Flash programming is way out of scope for this answer, though it might provide a more elegant solution.

所以...

您当前的方法由启动脚本,首先杀死任何现有的 GTK的咬牙切齿程序然后运行一个新的。它得到从头开始时incrontab看到一个更改文件重新启动。这是一个可行的解决办法,如果你相信你的Flash应用程序永不死机并退出,如果你总是有完美的时机。它的一个问题是,你有一个进程的死亡与下一开始之间的未知延迟。你的 killall 发送一个信号,该信号 GTK的咬牙切齿可能响应立即或之后暂停。有了短暂的停顿,你可能会发现自己启动SWF之前旧的完全消失了。具有较长的停顿,你可能会短暂地显示您的桌面。

Your current method consists of a launch script that first kills any existing gtk-gnash process then runs a new one. It gets relaunched from scratch when incrontab sees a change to the file. That's a viable solution if you trust that your flash application will never crash and quit, and if you always have perfect timing. One problem with it is that you've got an unknown delay between the death of one process and the start of the next. Your killall sends a signal, which gtk-gnash may respond to immediately, or after a pause. With a short pause, you might find yourself launching the SWF before the old one is fully gone. With a longer pause, you may briefly show your desktop.

有一个更好的解决方案可能是简单地在一个循环中启动SWF:

A better solution might be simply to launch the SWF within a loop:

#!/bin/sh

while true; do
    date '+[%Y-%m-%d %T] myfile.swf relaunched' >> /var/log/swf.log
    gtk-gnash /home/my_username/path/myfile.swf
done

然后让incrontab仅仅杀死现有流程:

Then have incrontab merely kill the existing process:

/home/my_username/path/myfile.swf IN_MODIFY killall gtk-gnash

通过从发射分离killall,您确保GTK的咬牙切齿的新实例不会启动,直到旧的实际上已经退出,并控制返回到shell脚本包装它。

By separating the killall from the launch, you make sure that the new instance of gtk-gnash does not start until the old one has actually quit and returned control to the shell script wrapping it.

当然,而是采用incrontab,你可以另外安装 inotify工具包,并留下一个循环运行:

Of course, instead of using incrontab, you could alternatively install the inotify-tools package and leave a loop running:

#!/bin/sh
while inotifywait -e modify /home/my_username/path/myfile.swf; do
    killall gtk-gnash
done

同样的系统调用,同样的效果,不同的前端。

Same syscalls, same effect, different front-end.

如果你想成为尤伯杯小心你杀什么过程中,你也可以GTK的咬牙切齿的PID存储在一个临时文件。下面是关于Flash播放器的包装另一个看法:

If you want to be über-careful about what process you're killing, you can also store the pid of gtk-gnash in a temporary file. Here's another take on the flash player wrapper:

#!/bin/sh

while true; do
    date '+[%Y-%m-%d %T] myfile.swf relaunched' >> /var/log/swf.log
    gtk-gnash /home/my_username/path/myfile.swf &
    echo $! > /var/run/gtk-gnash.pid
    wait
done

而incrontab行:

And the incrontab line:

/home/my_username/path/myfile.swf IN_MODIFY xargs kill < /var/run/gtk-gnash.pid

您可以采用减少的另一个战略的可见的杀/重新启动的效果,是它与很少或没有的内容运行时采取myfile.swf的截图,然后用其作为播放器上的桌面墙纸。 (。或者相当于我不知道你是如何设置),我做了我在几年前成立了一个数字标牌解决方案类似的东西 - 不时我们需要终止或重启一个独立的球员,所以我们刚才提出的,显示在第一页的帧。当我们打死了FlashPlayer的过程中,系统似乎在它的盒子,没有内容重置......然后,一秒钟后,将内容显示出来。 (是的,这是一个黑客。)

Another strategy you might employ to reduce the visible effect of the kill/restart, is to take a screenshot of myfile.swf while it is running with minimal or no content, then use that as the desktop wallpaper on the player. (Or equivalent. I don't know how you're set up.) I did something similar for a digital signage solution I set up a few years ago -- from time to time we needed to kill and restart a standalone player, so we made just the "frame" of the first page that shows up. When we killed the flashplayer process, the system "seemed" to reset with no content in its boxes ... and then, a second later, content would show up. (Yes, it's a hack.)

另外一个技巧:我总是发现Adobe的独立的Flash播放器(投影机)更可靠,比GTK的咬牙切齿更兼容。你可以找到Adobe公司在 Adob​​e支持中心下载页面,独立播放器,从他们的标准的Flash Player下载页面有所不同。

One other tip: I've always found Adobe's stand-alone Flash player ("Projector") to be more reliable and more compatible than gtk-gnash. You can find Adobe's stand-alone player at the Adobe Support Centre Download Page, which is different from their standard Flash Player download page.

这篇关于使程序重新加载文件更改已打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆