LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Some of the output redirected to /dev/null appears on the screen while 'scripting' (http://www.linuxquestions.org/questions/linux-newbie-8/some-of-the-output-redirected-to-dev-null-appears-on-the-screen-while-scripting-533474/)

gregorian 02-28-2007 11:59 PM

Some of the output redirected to /dev/null appears on the screen while 'scripting'
 
I wrote a script that plays a video using mplayer. Let's call it 'a' The script works perfectly, but I don't want to see the messages on the screen

I typed: ./a>/dev/null

I still got a few lines on my screen:

Quote:

mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.
Selected font is fixed-width.
I tried: ./a>log.txt.

I opened log.txt and found the usual messages I get while playing the file using mplayer from the command line. Ok, so my output is being directed. The strange thing is -- what I put in the QUOTE above was not found in log.txt

I typed: ./a

I found everything in log.txt and also what I've shown in the quotes-- on my terminal as expected.

How do you prevent any message from appearing on your terminal?

Also, how do you play the file in the background. ./a& does not work as the entire process will be sent to the background. I want to run it from the terminal in the same way as it runs when you double click on it; without displaying any messages, and allowing you to work peacefully on the terminal.

Dark_Helmet 03-01-2007 12:20 AM

There are two ways for programs to output to the screen/terminal: stdout and stderr. When you redirect a command with '>' alone, you redirect stdout only. Any messages sent to stderr (usually error messages) will still be displayed. To redirect both stdout and stderr to /dev/null, do this:
Code:

./a > /dev/null 2>&1
I don't have any idea about your second question. At least, not without seeing the contents of the script. It would be most likely that you'll need to background processes within your script to get the effect you want. Again, that's speculation without seeing our script.

gregorian 03-01-2007 01:15 AM

Yes, that worked. I understood what you said, but I have no idea of what 2>&1 actually means. Can you give me a link, or tell me which man page should I refer to, so that I can learn more about that?

You wanted the contents of my script. Here is my masterpiece:

Code:

#!/bin/bash
mplayer Immortal.flv

I can run the script by double-clicking the file. It opens a window and plays the video, and shuts down when the script is done playing it. I want to have the same effect by invoking the script from the command line.

./a& will give you this message:

Code:

$./a&

MPlayer 1.0rc1-3.2.2 (C) 2000-2006 MPlayer Team
CPU: Intel(R) Pentium(R) 4 CPU 2.80GHz (Family: 15, Model: 3, Stepping: 3)
CPUflags:  MMX: 1 MMX2: 1 3DNow: 0 3DNow2: 0 SSE: 1 SSE2: 1
Compiled for x86 CPU with extensions: MMX MMX2 SSE SSE2
98 audio & 216 video codecs
mplayer: could not connect to socket
mplayer: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

<I press Enter at this point to get back my prompt>

[2]+  Stopped                ./a

$

If I want to play the file, I'll have to type: fg

I don't know how to invoke the file in the same manner as I do in GUI.

Thanks for the help.

Dark_Helmet 03-01-2007 01:42 AM

Using "2>" is equivalent to saying "redirect stderr to . . .". You redirect stdout with "1>" or with just ">". So, this sequence: "> /dev/null 2>&1" can be translated (roughly) as

Redirect stdout (">") to /dev/null ("/dev/null"), and then redirect stderr ("2>") to the same location as stdout ("&1"). So both stdout and stderr are both sent to /dev/null.The same thing could be accomplished with: "> /dev/null 2> /dev/null"

Redirection is discussed in your shell's man page (probably man bash).

As for your second question...

As I recall, the GUI version of mplayer is invoked as "gmplayer" and not "mplayer." Perhaps you should give that a try by replacing mplayer with gmplayer in your script. If that doesn't work, then you might consider reading xterm's man page. I believe you can start a new xterm, have it start up minimized, and pass a command to execute on xterm's command line. For instance:
Code:

xterm -iconic -e "/path/to/mplayer Immortal.flv"
Try executing that command in your terminal, and if it works, then put it in your script and give it a shot.

gregorian 03-01-2007 02:22 AM

Thanks for the explanation.

Substituting gmplayer opens an additional window with the play buttons, track name etc.

The second command apparently does not do anything-- no error, no output; it just returns the prompt.

When I run the script by double-clicking it, I'm not sure... but does it open some sort of a child shell to execute the commands? If yes, all I need to do is figure out how to open a child shell.

jschiwal 03-01-2007 02:31 AM

There are two options to shut up mplayer. I don't remember what they are, something like -quiet and -reallyquiet. Look in the manpage. Mplayer expects input from the keyboard so it will stop if you send it to the background. You might be able to use "screen" or "nohup" to get it to run in the background.

gregorian 03-01-2007 02:58 AM

Thanks for the response, jschiwal.

mplayer -quiet file.flv displays everything except the last line which usually looks like this:

Code:

A:  3.4 V:  3.4 A-V:  0.019 ct: -0.005 103/103  2%  2%  0.3% 0 0
Mplayer did not recognise the reallyquiet option. You're right about the fact that it won't show keyboard interrupts on the screen.

nohup ./a directs the output to nohup.out, but does not return the prompt.

nohup ./a& sends it to the background and does not play the file; it's exactly like ./a&, except the output is sent to nohup.out and the rest of the problems are as described in my first post.

screen ./a opens a new instance which shows the messages and closes when the file is done playing displaying the message [screen is terminating] at my prompt. I cant use my prompt in the meantime.

screen ./a& has almost the same problem as described above.


Note:

Try running the script after pointing it to any of your media files in two ways:

1. Double click the script.
2. Execute the script through the command line.

You'll see the difference immediately.

Here's the script again:
Code:

#!/bin/bash
mplayer your_file



P.S. When you double click a script, how does Linux execute the command? You can simply type that command in the terminal, right?

nmh+linuxquestions.o 03-01-2007 05:15 AM

surrogate terminals
 
mplayer wants to interact with the terminal. If you don't want it to interact with the terminal you started it with, you need to provide a substitute. Screen works and I think expect could also be useful, if you want a non-interactive script.

The correct way to use screen for this would be to start screen, start mplayer in one window and then use a different one for whatever else you want to do.

start example:
$screen
Screen version 4.00.02 ...
...
[Press Space or Return to end.]
$mplayer <file>
{lots of mplayer output appears}
{ctrl-a, ctrl-c - for the default screen keybindings}
$
end example:

some additional material:
screen:
http://www.gnu.org/software/screen/screen.html
http://en.wikipedia.org/wiki/GNU_Screen
http://jmcpherson.org/screen.html

expect:
http://expect.nist.gov/
http://www.linuxjournal.com/article/3065

gregorian 03-01-2007 05:54 AM

Thanks for the reply, nmh. I understood what you said, but I want to run the script the way it runs when you double click it. From my previous post:



Try running the script after pointing it to any of your media files in two ways:

1. Double click the script.
2. Execute the script through the command line.

You'll see the difference immediately.

Here's the script again:
Quote:

#!/bin/bash
mplayer your_file

P.S. When you double click a script, how does Linux execute the command? You can simply type that command in the terminal, right?

nmh+linuxquestions.o 03-01-2007 04:16 PM

Quote:

Originally Posted by gregorian
Thanks for the reply, nmh. I understood what you said, but I want to run the script the way it runs when you double click it. From my previous post:



Try running the script after pointing it to any of your media files in two ways:

1. Double click the script.
2. Execute the script through the command line.

You'll see the difference immediately.

Here's the script again:



P.S. When you double click a script, how does Linux execute the command? You can simply type that command in the terminal, right?

I don't have a fancy desktop environment on my computer. I know you said what you want before, but maybe if you explain what you want it to do, things will make sense. I thought what you were asking, was how you could run mplayer from the command line without seeing all the output. If that is what you want, you can probably use expect to make a script that does that for you. If that is not what you want, please describe what you do want.

As for "when you double click" something - that depends on the desktop environment and other software that is not just the linux kernel. It also varies from system to system.

gregorian 03-01-2007 08:19 PM

When I double click the script, all I see is a window containing the video. It's basically the window that pops up when you run mplayer from the command line, the difference being -- there is no terminal on the screen. All you see is a window and nothing else.

I want to know how do you make *only* the window appear when you run it through a script, and by not using some mplayer option, like the way described in the above paragraph.

nmh+linuxquestions.o 03-01-2007 11:06 PM

Quote:

Originally Posted by gregorian
When I double click the script, all I see is a window containing the video. It's basically the window that pops up when you run mplayer from the command line, the difference being -- there is no terminal on the screen. All you see is a window and nothing else.

I want to know how do you make *only* the window appear when you run it through a script, and by not using some mplayer option, like the way described in the above paragraph.

In that case you want to use expect. man expect to find out all the bells and whistles, but basically you can do something along the lines of:
Code:

#! /usr/bin/expect -f
spawn mplayer <video file>
expect eof

and after putting the file in your path and making it executible, you can run it with:
Code:

expect_script > /dev/null 2>&1 &
this assumes you are using bash. But the basic idea is to run the expect script in the background and redirect output to /dev/null. Bash will let you know when a background process exits, and I don't think there is any easy way to avoid that.

jschiwal 03-01-2007 11:07 PM

FYI, the even less noisy option is -really-quiet. So you can use "nohup mplayer -really-quiet &
The video will play, and after pressing return in the console, you can use it normally.

I tried it out on a Hummer commercial. Don't ask.

gregorian 03-01-2007 11:34 PM

nmh, I tried what you said, but all I got was :

[1] 4901

and the video did not play.

jschiwal, nohup mplayer -really-quiet file.flv

gives me:

Code:

nohup: appending output to `nohup.out'

If I press Enter, I get :

Code:

[1]+  Stopped                nohup mplayer -really-quiet Immortal.flv
The video plays when I type fg, but then all the messages are sent to the terminal. I don't know why it works for you. Do you use bash?

How does KDE execute a script, when you double click it?

nmh+linuxquestions.o 03-02-2007 02:46 AM

Quote:

Originally Posted by gregorian
nmh, I tried what you said, but all I got was :

[1] 4901

and the video did not play.

Okay, can you tell me what happens when you do:
Code:

expect_script
Do you get video, and what output do you get on the terminal?


All times are GMT -5. The time now is 04:44 PM.