LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-28-2007, 10:59 PM   #1
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
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.

Last edited by gregorian; 02-28-2007 at 11:01 PM.
 
Old 02-28-2007, 11:20 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 03-01-2007, 12:15 AM   #3
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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.

Last edited by gregorian; 03-01-2007 at 12:19 AM.
 
Old 03-01-2007, 12:42 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 03-01-2007, 01:22 AM   #5
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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.

Last edited by gregorian; 03-01-2007 at 01:25 AM.
 
Old 03-01-2007, 01:31 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 03-01-2007, 01:58 AM   #7
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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?

Last edited by gregorian; 03-01-2007 at 02:03 AM.
 
Old 03-01-2007, 04:15 AM   #8
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
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
 
Old 03-01-2007, 04:54 AM   #9
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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?

Last edited by gregorian; 03-01-2007 at 05:03 AM.
 
Old 03-01-2007, 03:16 PM   #10
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
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.
 
Old 03-01-2007, 07:19 PM   #11
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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.
 
Old 03-01-2007, 10:06 PM   #12
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
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.
 
Old 03-01-2007, 10:07 PM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.
 
Old 03-01-2007, 10:34 PM   #14
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Original Poster
Rep: Reputation: 34
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?

Last edited by gregorian; 03-01-2007 at 10:35 PM.
 
Old 03-02-2007, 01:46 AM   #15
nmh+linuxquestions.o
Member
 
Registered: Feb 2007
Posts: 135

Rep: Reputation: 15
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?
 
  


Reply

Tags
automation, dcop, expect, kde, scripting, terminal



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting: Print output to logfile, error to logfile & screen stefanlasiewski Programming 18 05-22-2008 12:47 PM
init 3 ---> error output from wifi card is redirected to stdout koca Fedora 1 02-02-2007 08:01 AM
can output of tail be redirected? dsids Linux - Newbie 19 10-16-2006 02:19 AM
What is meant by " file > /dev/null 2>&1 </dev/null " attockonian Linux - Newbie 5 06-30-2006 10:51 PM
All output to /dev/null? Satriani Programming 13 10-17-2003 01:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration