Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[SOLVED] redirect output to a console (tty)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 877

PostPosted: Fri Apr 25, 2014 12:11 pm    Post subject: [SOLVED] redirect output to a console (tty) Reply with quote

Hi,

I'm trying to boot with a custom script displaying messages in tty3 which in turn launches another custom script which should display its own messages in tty2.

Somehow it's not working as expected.

My custom test scripts are:

Code:

# cat /opt/test/console3.sh
#/bin/sh
stty sane < /dev/tty3
stty sane < /dev/tty2
printf "%s" "tty3: in loop" >& /dev/tty3 < /dev/tty3
/opt/test/console2.sh >& /dev/tty2 < /dev/tty2
while [ 1 ];
do
    printf "%s" "." & >/dev/tty3 < /dev/tty3
    sleep 5
done


Code:

# cat /opt/test/console2.sh
#/bin/sh
printf "%s" "tty2: in loop"
while [ 1 ];
do
    printf "%s" "."
    sleep 5
done


From inittab I'm launching console3.sh and I'm not launching anything in tty2:

Code:

# grep "^c" /etc/inittab
c1:12345:respawn:/sbin/agetty 38400 tty1 linux
c3:3:respawn:/opt/test/console3.sh
c4:2345:respawn:/sbin/agetty 38400 tty4 linux
c5:2345:respawn:/sbin/agetty 38400 tty5 linux
c6:2345:respawn:/sbin/agetty 38400 tty6 linux
ca:12345:ctrlaltdel:/sbin/shutdown -r now


When I boot the system for the first time and I press ALT-F2 I can see the correct messages displayed by console2.sh in tty2, ie. I can read "tty2: in loop....." (and the dots keep coming up every 5 seconds).
However, if I press ALT-F3 I can see the message "tty3: in loop" but nothing afterwards, ever (no dots). It's as if launching console2.sh from console3.sh was blocking the rest of the execution.
So how can I correctly fork another script (and keep going) so it redirects its output to another tty?

As a side note, I can't seem to list the "console2.sh" process.
Code:

# ps aux | grep console
root     17987  0.0  0.0   3564  1300 ?        Ss   12:36   0:00 /bin/sh /opt/test/console3.sh
root     17994  0.0  0.0   3564   816 ?        S    12:36   0:00 /bin/sh /opt/test/console3.sh


Why doesn't "console2.sh" appear in the listing?

After killing 17987 and doing "ps axjf" I can see the following:

Code:

    1 17994 17987 17987 ?           -1 S        0   0:00 /bin/sh /opt/test/console3.sh
17994 19290 17987 17987 ?           -1 S        0   0:00  \_ sleep 5
    1 19008 19008 19008 ?           -1 Ss       0   0:00 /bin/sh /opt/test/console3.sh
19008 19011 19008 19008 ?           -1 S        0   0:00  \_ /bin/sh /opt/test/console3.sh
19011 19291 19008 19008 ?           -1 S        0   0:00      \_ sleep 5


Thanks

Vieri


Last edited by Vieri on Mon Apr 28, 2014 5:50 pm; edited 1 time in total
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Fri Apr 25, 2014 1:55 pm    Post subject: Reply with quote

I strongly suggest you to avoid this stupid >& bashism: Use the compatible >... 2>&1 instead and your problems wlil disappear:
Quote:
Code:
printf "%s" "." & >/dev/tty3 < /dev/tty3

This line means: run printf in the background, and redirect stdoout/stdin to/from /dev/tty3 for the empty command.

Another issue might be that stdout might be buffered
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 877

PostPosted: Mon Apr 28, 2014 7:47 am    Post subject: Reply with quote

Thanks for the tip.
So I changed console3.sh to:
Code:

# cat /opt/test/console3.sh
#/bin/sh
stty sane < /dev/tty3
stty sane < /dev/tty2
printf "%s" "tty3: in loop" >/dev/tty3 < /dev/tty3
/opt/test/console2.sh & >/dev/tty2 < /dev/tty2
while [ 1 ];
do
    printf "%s" "." >/dev/tty3 < /dev/tty3
    sleep 5
done


It seems to work fine except for the fact that the output of console2.sh is not in tty2 but in tty1. I'd like to call a script in the background and tell it to to redirect it's output to a specific tty, without needing to touch that script's source code (console2.sh).

Also, I'm still not seeing 'console2.sh' if I do 'ps aux'. I just see 2 console3.sh processes.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Mon Apr 28, 2014 4:03 pm    Post subject: Reply with quote

Vieri wrote:
I'd like to call a script in the background and tell it to to redirect it's output to a specific tty

Oh, and I thought your "& >" was a typo for the bashism "&>". Now I see that you really intended printf to be a background task.
In this case order matters:
Code:
echo 1 >foo &
runs "echo 1 with redirection to foo" in background. In contrast,
Code:
echo 1 & >foo
runs "echo 1" in background and then redirects the output of the empty command to foo,
(To memorize this, you should think of "&" as a separator like ";" or like a newline character.)
In other words: Putting & in your above code to the end of the line should do what you want.
Back to top
View user's profile Send private message
Vieri
l33t
l33t


Joined: 18 Dec 2005
Posts: 877

PostPosted: Mon Apr 28, 2014 5:49 pm    Post subject: Reply with quote

I must be blind or in need of a vacation... Simple enough. Thanks for the explanation!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum