Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-21-2013, 06:12 PM
|
#1
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,889
Rep: 
|
how to run the screen program WITHOUT clearing the display
I have a script which runs a few programs where the output scrolls up in the normal text way. There is one program the script runs I have a reason to run it inside screen. But I would like this program's output to just work like the others, and be output at the bottom of the display and scroll up like everything else. It will eventually finish in a few seconds, in which case it will exit, and screen should exit, and the script will go on to the next program as usual.
But screen clears the display because screen wants to manage it all. Any idea how to disable this?
|
|
|
07-21-2013, 06:21 PM
|
#2
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
start detached?
from man screen ( Screen version 4.01.00devel (GNU) 2-May-06 )
Code:
-d -m Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
-D -m This also starts screen in "detached" mode, but doesn't fork a new process. The command exits if the session terminates.
No idea if that fits your context, no real context to fit.
|
|
|
07-21-2013, 06:34 PM
|
#3
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,889
Original Poster
Rep: 
|
It needs to stay attached so it's output is shown and scrolls up like other output in the script, from the other programs it runs. I just don't want it blanking out the previous output. Someone watching the display should not really seen that screen was even run, although a few lines of output at the start and end could be tolerable. The program that will be run in screen will output about 3 to 6 lines. Those lines need to be seen below the output from previous programs, and above the output of programs that are run afterwards.
I'm using the detached method now, and I cannot see the output of the program being run.
|
|
|
07-22-2013, 06:34 AM
|
#4
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
OK, I think I better understand now..
I don't know if this will be suitable, but -L will produce an output log, which you could then view with cat screenlog.0
I was going to say use tail -f, but that tail will never exit.
Dumb example
Code:
#!/bin/bash
for i in {1..9};do
echo "line $i"
done
rm screenlog.0
coproc screen -DmL df -h
sleep 1
cat screenlog.0
for i in {9..18};do
echo "line $i"
done
Probably Doesn’t fit..
I don't know what you are running in your screen session or where that screen is ( local/remote or if brand new )
|
|
|
07-22-2013, 09:38 AM
|
#5
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,889
Original Poster
Rep: 
|
The task being run in screen is an rsync command to transfer /root/.ssh files over to the target host. It uses ssh. The very first time there will be an ssh password prompt. Screen is being used to stuff in the password to ssh which reads it from /dev/tty instead of stdin. Thereafter I can use ssh keys, so this is just a one-time thing per host. I start the background thing like this:
Code:
screen -S "${SESSION}" -p 0 -dm rsync -avWz {${overlaytree},root@${TARGETHOST}:}/root/.ssh/.
Then I run these commands to stuff in the password:
Code:
sleep 5
x=$(printf '%s\r' "${PASSWORD}")
screen -S "${SESSION}" -p 0 -X stuff "${x}"
This works, but I want to see the rsync command output in the scrolled output. If I take out the "-d" then I do see the output, but with the display blanking happening.
|
|
|
07-22-2013, 11:36 AM
|
#6
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
Ok, in that case I think you can use -L, and follow screenlog.0 with tail -f in background, terminating it when screen exits ( by monitoring PID )
Code:
screen -S "${SESSION}" -p 0 -DmL rsync -avWz {${overlaytree},root@${TARGETHOST}:}/root/.ssh/. &
PID_SCREEN=$!
sleep 5
x=$(printf '%s\r' "${PASSWORD}")
screen -S "${SESSION}" -p 0 -X stuff "${x}"
tail -f screenlog.0 &
PID_TAIL=$!
while $( pgrep screen | grep -q $PID_SCREEN );do
sleep 1
done
kill -15 $PID_TAIL
wait $! 2>/dev/null # avoids "Terminated tail -f screenlog.0" 'pollution'
Couple of things to note:
here I have used -Dm instead of -dm, this is so I can get pid of last bg job ( with $! )
by default screenlog.n ( where n is screen number ) is in current working dir, to change this you have to customise the .screenrc file ( unless things have changed and you can specify on command line, which would be much nicer )
Last edited by Firerat; 07-22-2013 at 11:40 AM.
|
|
|
07-22-2013, 11:24 PM
|
#7
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,889
Original Poster
Rep: 
|
It is possible to see when a file has another process open to it. The "tail" command should add an option to have it exit when the writer closes.
I could loop around testing the file with "fuser" to see when it is no longer open to another process, and avoid the mess with PIDs.
|
|
|
07-22-2013, 11:42 PM
|
#8
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
actually, yeah
man tail ( tail (GNU coreutils) 8.13 )
Code:
--pid=PID
with -f, terminate after process ID, PID dies
so much cleaner
Code:
screen -S "${SESSION}" -p 0 -DmL rsync -avWz {${overlaytree},root@${TARGETHOST}:}/root/.ssh/. &
PID_SCREEN=$!
sleep 5
x=$(printf '%s\r' "${PASSWORD}")
screen -S "${SESSION}" -p 0 -X stuff "${x}"
tail --pid=$PID_SCREEN -f screenlog.0
|
|
|
07-23-2013, 12:39 AM
|
#9
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,889
Original Poster
Rep: 
|
I want to avoid doing it by PID because -Dm is still blanking the display. Using -dm drops it in the background so I cannot reliably get the PID. But I don't need the PID since fuser can tell me when the file is no longer open.
Here's what I have lately tried:
Code:
echo "Testing if ssh can login using a key pair"
ssh=( ssh -o 'batchmode yes' "root@${dsthost}" )
rsync=( rsync -e 'ssh -o "batchmode no"' -avWz ${cwd}/noarch/root/.ssh/. ${dsthost}:/root/.ssh )
if ! "${ssh[@]}" 'echo YES' ; then
if [[ -n "${PW}" ]] ; then
echo "Transferring authorized_keys file to avoid future password prompts"
pid=$$
unique=$( exec date "+%Y%m%d%H%M%S%N${pid}" )
cfgfile="/tmp/mods-rsync-${unique}.cfg"
logfile="/tmp/mods-rsync-${unique}.log"
echo "logfile ${logfile}" > "${cfgfile}"
ls -dl "${cfgfile}"
screen -c "${cfgfile}" -S mods-rsync -p 0 -dm "${rsync[@]}"
sleep 5
screen -S mods-rsync -p 0 -X stuff $( printf '%s\r' "${PW}" )
while fuser "${logfile}" 2>/dev/null ; do sleep 1 ; done
cat "${logfile}"
# rm -fv "${cfgfile}" "${logfile}"
"${ssh[@]}" 'ls -dl /root/.ssh/authorized_keys' || fail "Transfer of authorized_key file to ${dsthost} failed"
else
echo
echo 'Enter the root password for ${dsthost} to transfer the .ssh/authorized_keys file:'
"${rsync[@]}" || exit 1
fi
fi
But this still fails because the log file is not being written. I think I have it set up correctly in the config file, and -c is used to refer to the config file. And at least 5 should be enough time for the log file to be opened. Yet it doesn't write the log file. The rsync does run, does get the password being stuffed in, and does transfer the ssh files. Screen is just not logging it. Does screen need to stay in the foreground to log it (e.g. the client side does the logging instead of the server side)? If so, I'll try some other way to get log output. The output looks like this:
Code:
Testing if ssh can login using a key pair
Warning: Permanently added 'planck,192.168.1.22' (RSA) to the list of known hosts.
Permission denied (publickey,password,keyboard-interactive). this is the test on the if command
Transferring authorized_keys file to avoid future password prompts
-rw-r--r-- 1 root root 57 Jul 23 01:34 /tmp/mods-rsync-2013072301341102456990623720.cfg
cat: /tmp/mods-rsync-2013072301341102456990623720.log: No such file or directory the file is not there
Warning: Permanently added 'planck,192.168.1.22' (RSA) to the list of known hosts.
-rw------- 1 root root 71808 Oct 2 2012 /root/.ssh/authorized_keys it got this file anyway
|
|
|
07-23-2013, 01:31 AM
|
#10
|
Senior Member
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683
|
You need to turn logging on with -L
( the logfile in the config just overrides the default /location/name )
Code:
screen -c "${cfgfile}" -S mods-rsync -p 0 -dmL "${rsync[@]}"
Not sure why you are seeing blanking with -Dm
here is my test script
Code:
#!/bin/bash
for i in {1..10};do
echo $i
done
screen -p 0 -DmL find ~/Downloads &
tail --pid=$! -f screenlog.0
echo Done
Simple, but enough for me to see no blanking
|
|
1 members found this post helpful.
|
07-23-2013, 12:48 PM
|
#11
|
Senior Member
Registered: May 2009
Location: center of singularity
Distribution: Xubuntu, Ubuntu, Slackware, Amazon Linux, OpenBSD, LFS (on Sparc_32 and i386)
Posts: 2,889
Original Poster
Rep: 
|
It is working now. I guess telling it what logfile to use is not sufficient for it to know to actually do it.
|
|
|
All times are GMT -5. The time now is 06:59 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|