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
add the following to incrontab:
/home/my_username/path/myfile.swf IN_MODIFY /home/my_username/path/run.sh
and the run.sh
contains: ( also 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?
This is on Ubuntu 12.04 if that matters for you. This is a duplicate of my own question on askubuntu
EDIT: 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.
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.
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.
So...
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. Yourkillall
sends a signal, whichgtk-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.A better solution might be simply to launch the SWF within a loop:
Then have incrontab merely kill the existing process:
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.
Of course, instead of using incrontab, you could alternatively install the
inotify-tools
package and leave a loop running:Same syscalls, same effect, different front-end.
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:
And the incrontab line:
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.)
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.