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:
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. |
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 |
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 ./a& will give you this message: Code:
$./a& I don't know how to invoke the file in the same manner as I do in GUI. Thanks for the help. |
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" |
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. |
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.
|
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 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 P.S. When you double click a script, how does Linux execute the command? You can simply type that command in the terminal, right? |
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 |
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:
P.S. When you double click a script, how does Linux execute the command? You can simply type that command in the terminal, right? |
Quote:
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. |
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. |
Quote:
Code:
#! /usr/bin/expect -f Code:
expect_script > /dev/null 2>&1 & |
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. |
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 How does KDE execute a script, when you double click it? |
Quote:
Code:
expect_script |
I got:
Code:
bash: ./script: Permission denied When I ran it using the previous command you supplied, I forgot to tell you that, after getting that job number, when I pressed Enter I got: Quote:
|
Quote:
Code:
chmod u+x script |
No, I didn't need to, as these were its permissions:
rwxr-xr-x I just found an interesting thing-- Open any link on your desktop using a text editor. You will see an option called TERMINAL=false. That explains why I don't see any terminal and can see only the video while running the script. If you can figure out how KDE executes TERMINAL=false, the problem will be solved. I don't know how KDE executes such commands. |
I tried it again on my desktop and it worked.
nohup mplayer -really-quiet <video> & This also worked in the virtual terminal vt/1: nohup mplayer -vo fbdev2 -really-quiet <video> & |
I tried both your commands jschiwal, but neither of them work. Stupid RH9 :cry:
But still, do you know how to do the TERMINAL=false thing I mentioned in my previous post. |
kde dcop urls
Quote:
|
You would use the terminal=false entry if you are linking to a gui program and terminal=true if you are linking to a script program, i.e. with menu. You might try looking at the .desktop entry for a program like firefox. Sometimes you are running a wrapper script. If you want to run mplayer in kde, you could simply call gmplayer instead.
Also, which terminal emulator are you running. I ran my test using konsole on two different computers. And on vt/1 using the framebuffer device. Also if you want to start playing something in particular by double clicking on an icon, you can make a copy of an xmms icon and add an argument list after the command in the properties. Using dcop as the poster above mentioned is the most powerful and flexible approach. |
You're right kdcop is indeed an awesome program. I learned many things by reading that link, but I cant seem to figure out how TERMINAL=OFF is acheived by it. Also, isn't there a simpler way to invoke the script without having to view the terminal. I'm fine with using the screen command, but I want to know if there is any other way.
I use konsole too. |
Quote:
Quote:
Screen is very useful, but not really the right tool for what I think you want. The right tool is something similar to expect. I know you tried it before and had trouble, but I think it should work if you put a bit more effort into it. If you would like to work on the expect setup, you should start by seeing if you can run a simple expect script. After that, you can get it to run mplayer with almost no output. |
Ok, I'll read the man pages of expect. nmh, from where did you learn all this stuff?
|
Quote:
I have found that there are many problems that are very easy to figure out simply by poking at them, reading the man/info pages, or reading examples online. I have also found that there are problems that are *very* difficult to solve without asking for help. The real trick is knowing which you are trying to solve. Good luck with the expect stuff - and feel free to ask more questions if some of it does not make sense. |
Getting too complicated??
If i am understanding correctly, the following command gives the effect you want:
mplayer <video_file> < /dev/null > /dev/null 2>&1 & So just stick that in your script?? Earlier in the thread, you had the output redirected but the video just didn't play. Someone mentioned that mplayer wants to be connected to a terminal - and this was your problem, it wasn't connected to any input, so it was hanging. " < /dev/null" connects /dev/null as standard input too. |
Late to the party
Right, just noticed the dates on the previous postings.
|
All times are GMT -5. The time now is 07:45 PM. |