ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Rep:
All output to /dev/null?
Hi all,
Maybe a stupid question, but i cant figure it out...
I have a little script to start X on another display, and want it to start in the background.
Here it is:
Code:
#!/bin/sh
XSRV=$1
XSCREEN=$2
# Check if we are called in with parameters:
# If not, exit the program.
if [ -z $XSRV ]
then
echo
echo " Usage: `basename $0` server_name [display_number]"
echo " Valid display numbers are 1, 2 and 3"
echo " Where the default display = 1"
echo
exit 1
fi
#Check if an optional screen is passed:
if [ -z $XSCREEN ]
then
XSCREEN=1
fi
# check if X is installed on this machine:
XSTARTER=`which X 2>/dev/null`
if [ -z $XSTARTER ]
then
echo
echo "No X-server located in $PATH";
exit 1
fi
# Check if the second arg is a screenNumber within 1 to 3...
case $XSCREEN in
[1-3]) CONT=true;;
*) echo "$XSCREEN is not a valid display number (please use 1,2 or 3)"
exit 1;;
esac
let newTerm=$XSCREEN+7
echo
echo " $XSRV will start on VT $newTerm in 3 seconds."
echo " Program will run in background"
echo " Press ctrl-c to abort...."
sleep 3
echo Now connecting....
$XSTARTER :$XSCREEN -once -keeptty -quiet -query $XSRV 2>&1 /dev/null &
echo "To enter the new window, press CTRL-ALT-F$newTerm";
As you can see on the last lines, i direct the output to dev null.
However, it keeps showing information about X in the screen what started it. Also, the -keeptty option doesn't seem to work.
I guess you understand the meaning of the script..
How can i stay in the TTY I started from, and suppress all output from X ?
(I still see myself as a newbie, so be clear in your answers please?)
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Original Poster
Rep:
I still have output:
Code:
[beta@laptop bin]# xconnect myotherserver
myotherserver will connect on VT 8 in 3 seconds.
Program will run in background
Press ctrl-c to abort....
Now connecting....
To enter the new window, press CTRL-ALT-F8
[beta@laptop bin]#
XFree86 Version 4.3.0 (Red Hat Linux release: 4.3.0-2)
Release Date: 27 February 2003
X Protocol Version 11, Revision 0, Release 6.6
Build Operating System: Linux 2.4.20-3bigmem i686 [ELF]
Build Date: 27 February 2003
Build Host: porky.devel.redhat.com
Before reporting problems, check http://www.XFree86.Org/
to make sure that you have the latest version.
Module Loader present
OS Kernel: Linux version 2.4.20-20.9 (bhcompile@stripples.devel.redhat.com) \
(gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) #1 Mon Aug 18 11:45:58 \
EDT 2003
Markers: (--) probed, (**) from config file, (==) default setting,
(++) from command line, (!!) notice, (II) informational,
(WW) warning, (EE) error, (NI) not implemented, (??) unknown.
(==) Log file: "/var/log/XFree86.1.log", Time: Sun Oct 5 19:51:04 2003
(==) Using config file: "/etc/X11/XF86Config"
[10f] 320 x 200, 70Hz
[112] 640 x 480, 60Hz, 72Hz, 75Hz, 85Hz, 100Hz
[115] 800 x 600, 60Hz, 72Hz, 75Hz, 85Hz, 100Hz
[118] 1024 x 768, 60Hz, 70Hz, 75Hz, 85Hz, 43Hz, 100Hz
[11b] 1280 x 1024, 60Hz, 75Hz, 85Hz, 43Hz
[11e] 640 x 400, 70Hz
[124] 1600 x 1200, 48Hz, 60Hz, 75Hz, 85Hz
[134] 320 x 240, 72Hz
[144] 400 x 300, 72Hz
[154] 512 x 384, 70Hz
[175] 720 x 480, 75Hz
[17a] 720 x 576, 75Hz
i think the Xserver is not allowing its output to be sent to any log file or anyplace else..so you'd have to send all the output of the script to /dev/null like this:
Xconnect <my-server> 2>/dev/null
by this you get what you want and dont get the Xserver logs that are anyway also going to the /var/log/XFree86.1.log file.
as for your keeptty option not working...i am at a loss....
sorry cant be of any more help
i dont know what the keeptty option does..it is for debugging ..so as to keep hold of the calling terminal, right? but what should i be able to see.
what kev says works,
what i understand from this is :
i) the output to be redirected is not being sent on 1(stdout) but only on 2(stderr), so if you were to do just a redirection of 2 to the desired file it would work just fine.(the way you want it to).
ii) that >"file" 2>&1 &makes sense because >"file" means redirecting the stdout to a certain file... but here i want 2 to be out on the same stream as 1, there is no output on one, so no redirection and i tell this after the command so that the redirection and the dup'ing of descriptors is not negated (which is the case in 2>&1 >"file" , the dup'ing is overriden in the last part as i tell that i want ONLY stdout to be sent to file)..
2>1 means redirect stderr to a file called 1 in the local directory
then 2>&1 means redirect all stderr (file descriptor 2) to stdout (file descriptor 1).. The & is required otherwise you would not be able to direct output to a file called 1 (not the file descriptor 1)...
So on the right of the '>' to reference a file descriptor (say stdout) it is &1 or for stderr &2 etc...
Distribution: Red Hat 7.3, Red Hat 9, Solaris8, Slackware 10, Slax on USB, AIX, FreeBSD, WinXP, AIX, Ubuntu
Posts: 418
Original Poster
Rep:
Ahhhhhh thats making sense!
Thanks alot for your help. I'm beginning to loose my newbie status with all of your help!!! :-)
But, then in the example kev gave:
Quote:
> /dev/null 2>&1 & would work much better than 2>&1 > /dev/null &
it looks to me as:
all default output to /dev/null then all stderr to stdout. Correct?
Then the second option is: send all stderr to stdout, and send all stdout to /dev/null ?
(Am I still on the right track here?) Why is the first option better then the second?
I'm not too sure, and hopefully someone will explain in detail for us.
I just spent about 15 minutes trying to illistrate how it would work, but I can't for the life of me work out the semantic difference between the two statements. I know from experience that it does work but I can figure out the mechanism behind it. I typed about 500 words tying to explain what is happening but each time I attempted they both came out with basically the same result .
Please if you know the technical mechanics involved here can you please explain in detailed lay-mans terms.
How does " >/dev/null 2>&1" achieve what we want and " 2>&1 >/dev/null" does not?
Now A contains both the error message and the normal output. The >& is called a redirection operator. x >&y tells the shell to write pipe x into pipe y. Redirection is specified from right to left on the command-line. Hence, the above command means to mix stderr into stdout and then to redirect stdout to the file A.
Finally,
ls existing_file non-existing_file 2>A 1>&2
cat A
We notice that this has the same effect, except that here we are doing the reverse: redirecting stdout into stderr and then redirecting stderr into a file A.
To see what happens if we redirect in reverse order, we can try,
ls existing_file non-existing_file 2>&1 1>A
cat A
which means to redirect stdout into a file A, and then to redirect stderr into stdout. This command will therefore not mix stderr and stdout because the redirection to A came first.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.