Shell script behaves strangely when called via an Erlang port?

Normally if you run a command in background with & in a shell script and the shell script terminates before the command, then the command gets orphaned. It might be that erlang trys to prevent orphaned processes in open_port and waits for eog to terminate. Normally if you want to run something in background during a shell script you should put in a wait at the end of the script to wait for your background processes to terminate.

But this is exactly what youd don't want to do.

Normally if you run a command in background with & in a shell script and the shell script terminates before the command, then the command gets orphaned. It might be that erlang trys to prevent orphaned processes in open_port and waits for eog to terminate. Normally if you want to run something in background during a shell script you should put in a wait at the end of the script to wait for your background processes to terminate.

But this is exactly what youd don't want to do. You might try the following in your shell script: #! /bin/sh FILENAME=$1 daemon eog $FILENAME # exit 0 not needed: daemon returns 0 if everything is ok If your operating system has a daemon command.

I checked in FreeBSD and it has one: daemon(8) This is not a command available on all Unix alike systems, however there might be a different command doing the same thing in your operating system. The daemon utility detaches itself from the controlling terminal and executes the program specified by its arguments. I'm not sure if this solves your problem, but I suspect that eog somehow stays attached to stdin/stdou as a kind of controling terminal.

Worth a try anyway. This should also solve the possible problem that job control is on erroneously which could also cause the problem. Since daemon does exit normally your shell can't try to wait for the background job on exit because there is none in the shells view.

Having said all this: why not just keep the port open in Erlang while eog runs? Start it with: #! /bin/sh FILENAME=$1 exec eog $FILENAME Calling it with exec doesn't fork it bu replaces the shell process with eog.

The exit status you'll see in Erlang will then be the status of eog when it terminates. Also you have the possibility to close the port and terminate eog from Erlang if you want to do so.

Oooh, daemon(1) wrapper, excellent idea. I thought about the exec eog option, but that wouldn't let Alexey log "Starting playing finished successfully"; his wrapper wouldn't know if the process is waiting ten or twenty seconds before it could be run vs running just fine for ten or twenty seconds. But having the option to kill eog from Erlang sounds compellingly worth it.

– sarnold Mar 22 at 21:59 But the process in the background wouldn't give this info either. If there is an error during the preparation phase, this would be detected. Otherwise either run the shell with option -x (printing commands when they are run) or an echo before starting.

Running the open_port with {line Max_line} and interpreting the messages you get to find out about progress. – Peer Stritzinger Mar 23 at 11:16 In fact, keeping the port open (both to be able to kill the program more easily and, more importantly for my usecase, to know when exactly it's finished without polling) was my original design :) Unfortunately, I wasn't able to persuade my manager it would be a good idea. – Alexey Romanov Mar 24 at 7:14 daemon seems to be the solution, but while daemon mplayer /home/aromanov/workspace/gmcontroller/working_dir/media/video/635e0b3a-99f0-49e4-8a19-15c1de32a3a2.

Avi works fine, daemon mplayer /home/aromanov/workspace/gmcontroller/working_dir/media/video/635e0b3a-99f0-49e4-8a19-15c1de32a3a2. Avi -loop 0 doesn't. Presumably daemon thinks -loop 0 is passed to it, doesn't recognize the option and so doesn't start anything.

Quoting the command doesn't help, and manpage doesn't say anything about it. Any ideas? – Alexey Romanov Mar 24 at 7:24 1 You might try using -- to separate (empty) daemons options from the options of the called command: e.g. Daemon -- mplayer foo.

Avi -loop 0. Normally daemon shouldn't interpret any options after the command part but the implementation on your OS seems to be sloppy about it. Many option parsing functions accept -- as "here be the end of my options" – Peer Stritzinger Mar 24 at 9:07.

At least the /bin/sh (actually dash(1)! ) on my Ubuntu system mentions: -m monitor Turn on job control (set automatically when interactive). When you run the script from a terminal, the shell probably recognizes that it is being run interactively and supports job control.

When you run the shell script as a port, the shell probably runs without job control.

Making the hashbang line #! /bin/bash -m didn't help. – Alexey Romanov Mar 22 at 12:14 Alexy, the -m was from dash(1), not bash(1), so it would only apply to the #!

/bin/sh (if your system is like mine, and uses dash(1) to supply /bin/sh). For bash(1) it would be adding set -m in the script. – sarnold Mar 22 at 12:20 Help for bash on my system also mention -m.

But yes, this seems to be the problem: #! /bin/sh -m gives "22/Mar/2011 15:42:02.507 Debug: Starting player... Shell output: /home/aromanov/workspace/gmcontroller/scripts. Dummy/video/video-play.Sh: 0: can't access tty; job control turned off" Using bash and set -m doesn't give either the error message, but works the same as without set -m.

– Alexey Romanov Mar 22 at 12:41 Alexey, is it fair game to re-write the script in C? It'd be so easy to fork(2) and exec() the video player, and avoid the job-control mess. Does the shell script provide enough other value that re-writing in C would be difficult?

– sarnold Mar 22 at 12:45 1 Heh, don't accept my answer just yet, someone else might know exactly how to work around this without re-writing in C. :) – sarnold Mar 22 at 12:54.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions