LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 07-21-2013, 06:12 PM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Rep: Reputation: 115Reputation: 115
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?
 
Old 07-21-2013, 06:21 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,258

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
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.
 
Old 07-21-2013, 06:34 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
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.
 
Old 07-22-2013, 06:34 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,258

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
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 )
 
Old 07-22-2013, 09:38 AM   #5
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
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.
 
Old 07-22-2013, 11:36 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,258

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
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.
 
Old 07-22-2013, 11:24 PM   #7
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
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.
 
Old 07-22-2013, 11:42 PM   #8
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,258

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
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
 
Old 07-23-2013, 12:39 AM   #9
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
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
 
Old 07-23-2013, 01:31 AM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,258

Rep: Reputation: 385Reputation: 385Reputation: 385Reputation: 385
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.
Old 07-23-2013, 12:48 PM   #11
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
It is working now. I guess telling it what logfile to use is not sufficient for it to know to actually do it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Program in TERM Full Screen. Can I run as a window?+ Ace Blackwell Linux - Software 1 11-28-2012 10:36 AM
is there a program that will project my android table screen to a larger display baronobeefdip Linux - Software 4 11-06-2012 06:34 PM
Command to run program on a specific display? thecake Linux - Newbie 1 12-16-2008 02:55 PM
Clearing the screen in C++ qcoder Programming 7 11-29-2003 11:03 AM
Clearing screen xunilpassion Programming 1 10-16-2003 03:54 PM


All times are GMT -5. The time now is 08:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration