Keeping a process running with perl (or bash if that would be better)?
|
Author | Content |
---|---|
techiem2 Apr 02, 2008 10:51 AM EDT |
Ok, I'm trying to work on the final touches on our Digital Sign setup, and I've run into a little problem.
Mplayer in slave mode works well, but has some issues when you break a playlist loop.
Basically, if you start it in loop mode, then load a different playlist/file, it will play it then play the normal playlist once and exit.
To solve this I have a script that restarts it.
This works well overall, but I've found that if you say send a command to play something else more than once, it breaks out of the script loop.
Obviously this is bad, as it means the script has to be restarted manually.
I tried making a script that auto-restarts the mplayer slave script when it detects that mplayer is no longer running, but it seems to have the same issue (it seems that it's exiting as soon as it starts the mplayer slave script even though it's not supposed to - I just did a test by starting slave script and then the test script, and the test script exited after 2 iterations).
I won't post that whole script unless someone asks, but it's basic layout is: #!/usr/bin/perl sleep 6; checkmplayer(); sub checkmplayer { check if mplayer is running if mplayer is running { don't do anything } else { system ("mpslave &"); } } sleep 6; checkmplayer(); Any help would be greatly appreciated! Thanks as always! |
techiem2 Apr 02, 2008 12:54 PM EDT |
Well, I think I found a solution (it's testing now). Instead of doing the sub/call sub thing, I did a while loop. while (count |
Sander_Marechal Apr 02, 2008 12:58 PM EDT |
Just take a look at the code you posted. It's quite obvious that doesn't work (unless the forum ate some of your code). First it sleeps for 6 second, then it checks for mplayer. Then it sleeps 6 seconds again, then checks again and then quits. What you need is an infinite loop: #!/usr/bin/perl sub checkmplayer { check if mplayer is running if mplayer is running { don't do anything } else { system ("mpslave &"); } } while (1) { checkmplayer(); sleep 6; } |
techiem2 Apr 02, 2008 1:20 PM EDT |
well, it ate my spacing.
it also ate most of my second post...
I see what you mean though in your example.
I assumed that since I called the sub after the sub exited that it would always reprocess that line when the sub exited.
Apparently not.. Thanks. My infinite while loop is testing. Since the forum ate it, basically I set it to do stuff while count |
Sander_Marechal Apr 02, 2008 1:45 PM EDT |
Quoting:I assumed that since I called the sub after the sub exited that it would always reprocess that line when the sub exited. No. When a function (or sub) exits, it jumps to where it was called from and continues from there. So in the script you posted, the sub call in line 3 would make it go to line 4 (the sub itself). When the sub exits at line 16 it jumps back to line 4. It then skips line 5-16 because it's just the definition of a sub. It only defines it and doesn't executes it. At line 18 it jumps to the sub at line 4 again. When exiting at line 16 it jumps back to line 18. There's nothing after line 18 so the script exits. This is really, really basic behaviour of functions. It's programming 101 and applied to all languages, even Lisp. May I recommend you try follow some beginner programming tutorials? The language you learn isn't very important. Basic things like functions, control flows, loops, switches and arrays apply to all languages. It will make your work with these kinds of scripts a whole lot easier. PS: It's very likely that your first restart script has a similar bug in it. If you post it (or an abbreviated version of it) we can probably see quickly what the problem is. In that case you wouldn't need the second script at all. |
tuxchick Apr 02, 2008 3:05 PM EDT |
http://pastebin.com/ is a good place to post scripts and coding snippets- then you don't have to hassle with the weirdo formatting problems that plague forums. |
techiem2 Apr 02, 2008 4:49 PM EDT |
@sander
Ah.
Well, it's been years since I took programming and did any kind of function/sub studying, so I'm a bit rusty.
That makes sense now.
I believe I did the same or similar in the main script, so I'll have to check that and fix it. @TC Thanks. I'll have to remember to use that next time. :) |
Sander_Marechal Apr 02, 2008 8:23 PM EDT |
There's even a nice commanline tool to use pastebin. Just pipe some text on it's STDIN and it'll upload to pastebin and give you back an URL. It's called pastebinit. It's in the debian repositories. I don't know about other distros. $ pastebinit < code.pl |
techiem2 Apr 03, 2008 10:16 AM EDT |
Thanks! I fixed the main script to loop properly so I don't need the checking script now. I found the archive for the latest pastebinit ( http://www.stgraber.org/download/projects/pastebin/ ) and posted the fixed mplayer slave launching script. http://pastebin.com/f6574f9c6 |
You cannot post until you login.