# the beauty of screen+script+emerge

## apokalyptik

Screen is a useful tool for running programs in a detatched terminal.  along with other benefits a screens' processes re not killed when the screen is detached (unlike a telnet/ssh session)

Script is a tool to log everything coming into or going out of your terminal to a file for later review

If you dont know what emerge does then you're in the wrong place!

```

# su

Password: XXXXXXXX

# screen

# script

Script started, file is typescript

# emerge dcron & screen -d

# exit

logout

# exit

logout

```

{ later, when you have time }

```

# su

Password: XXXXXXXX

# screen -r

# { enter }

[1]+  Done                    emerge dcron

# exit

Script done, file is typescript

# exit

[screen is terminating]

# cat typescript

```

This block of code is our script-ed log file (very cool)

```

# emerge dcron & screen -d

[1] 30647

[24614.pts-0.www detached.]

# Calculating dependencies ...done!

>>> emerge sys-apps/dcron-2.7-r9 to /

>>> md5 ;-) dcron27.tgz

>>> Unpacking source...

>>> Unpacking dcron27.tgz

patching file Makefile

patching file database.c

>>> Source unpacked.

fgrep -h Prototype main.c subs.c database.c job.c crontab.c subs.c >protos.h

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes   -c -o main.o main.c

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes   -c -o subs.o subs.c

subs.c: In function `slog':

subs.c:82: warning: `%y' yields only last 2 digits of year

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes   -c -o database.o database.c

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes   -c -o job.o job.c

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes -o crond main.o subs.o database.o job.o 

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes   -c -o crontab.o crontab.c

gcc -march=i686 -O3 -pipe -Wall -Wstrict-prototypes -o crontab crontab.o subs.o

>>> Install dcron-2.7-r9 into /var/tmp/portage/dcron-2.7-r9/image/ category sys-apps

strip:

/var/tmp/portage/dcron-2.7-r9/image/usr/sbin/crond

/var/tmp/portage/dcron-2.7-r9/image/usr/bin/crontab

>>> Completed installing into /var/tmp/portage/dcron-2.7-r9/image/

>>> Merging sys-apps/dcron-2.7-r9 to /

--- /var/

--- /var/spool/

--- /var/spool/cron/

--- /var/spool/cron/crontabs/

--- /usr/

--- /usr/sbin/

>>> /usr/sbin/crond

--- /usr/bin/

>>> /usr/bin/crontab

--- /usr/share/

--- /usr/share/doc/

--- /usr/share/doc/dcron-2.7-r9/

>>> /usr/share/doc/dcron-2.7-r9/CHANGELOG.gz

>>> /usr/share/doc/dcron-2.7-r9/README.gz

>>> /usr/share/doc/dcron-2.7-r9/crontab.gz

--- /usr/share/man/

--- /usr/share/man/man1/

>>> /usr/share/man/man1/crontab.1.gz

--- /usr/share/man/man8/

>>> /usr/share/man/man8/crond.8.gz

--- /etc/

--- /etc/init.d/

--- /etc/init.d/dcron

-o- /etc/crontab

>>> Safely unmerging already-installed instance...

--- !mtime obj /usr/share/man/man8/crond.8.gz

--- !mtime obj /usr/share/man/man1/crontab.1.gz

--- !mtime obj /usr/share/doc/dcron-2.7-r9/crontab.gz

--- !mtime obj /usr/share/doc/dcron-2.7-r9/README.gz

--- !mtime obj /usr/share/doc/dcron-2.7-r9/CHANGELOG.gz

--- !mtime obj /usr/sbin/crond

--- !mtime obj /usr/bin/crontab

--- !empty dir /var/spool/cron/crontabs

--- !empty dir /var/spool/cron

--- !empty dir /var/spool

--- !empty dir /var

--- !empty dir /usr/share/man/man8

--- !empty dir /usr/share/man/man1

--- !empty dir /usr/share/man

--- !empty dir /usr/share/doc/dcron-2.7-r9

--- !empty dir /usr/share/doc

--- !empty dir /usr/share

--- !empty dir /usr/sbin

--- !empty dir /usr/bin

--- !empty dir /usr

--- !empty dir /etc/init.d

--- !empty dir /etc

>>> original instance of package unmerged safely.

 * To activate /etc/cron.{hourly|daily|weekly|montly} please run: 

 * crontab /etc/crontab

 * !!! That will replace root's current crontab !!!

>>> Regenerating /etc/ld.so.cache...

>>> sys-apps/dcron-2.7-r9 merged.

>>> Recording sys-apps/dcron in "world" favorites file...

 * GNU info directory index is up-to-date.

 * IMPORTANT: 1 config files in /etc need updating.

 * Type emerge --help config to learn how to update config files.

[1]+  Done                    emerge dcron

# exit

Script done on Mon Sep  9 08:37:54 2002

```

```

# rm typescript

# exit

logout

# exit

logout

```

In this way you can build parts of your system while not being attached to it, and review configuration changes, and advisories at the end of each emerge... I also recommend scripting for any emerge which will compile dependencies, so that you can look at package bulletins later (after text is done flying by at a million miles per hour)

----------

## Crash1976M

i knew screen (and some of its features) but i didn't know script. - it's really a fine tool.

but, is there anywhere a docu of screen in a real readable format, because i am not really happy with this man page things.

Tank you

Michael

----------

## UnderScore

For more help & info on screen go here http://www.math.fu-berlin.de/~guckes/screen/

----------

## Crash1976M

Thank you

I forgot Sven - i saw him last year on the linux days in Austria.

bye

Mike

----------

## corporate_gadfly

I don't think you need script, since screen already has its own logging. There's at least 2 ways on how you can enable logging.

1. By starting screen with:

```
screen -L
```

2. or, once inside screen you can go to the "C-a :" prompt and type in 

```
log on
```

thereby eliminating the need to use script.  :Smile: 

----------

## bdunn

That seems like a lot more trouble than something like this:

```
nohup emerge mozilla &
```

(Disconnect from SSH or whatever)

Go back later and you'll find a nohup.out file containing everything that happens (and with mozilla, that's going to be a lot!)

Is there something about screen/script other than the ability to switch to many different virtual screens with screen?

----------

## fleed

I usually just use the nohup method too. I think screen+script could be more useful in other cases, not particularly when emerging.

----------

## rks1789

I think the real power in using screen is that you can detatch from it, and re-attatch later and be exactly where you left off.

This can be usefull if you have an unstable connection, or if you just want to stop working in 1 place, and start again in another.

After you start screen (and do what you want), ^a^d to detatch.

Then when you want to reconnect do

screen -r

If you were disconnected without detatching you can do

screen -rd

Which will detatch the screen process and then attatch to it.  If you omit the -r/-d options then you will create a NEW screen process.

Rich

----------

## snakü

Screen is handy if you ssh or telnet into a machine, want to view a lot of different things, but don't want all kinds of terminal windows.  Sort of like the console's virtual terminals, but in xterm or PuTTY windows.  In addition, you can split the screen so you can see several of those windows at once, something that the above link does not discuss.

Inside screen...

Control-A Control-C

(Create new "window" and switch to it.)

Control-A S

(Create a new "region.")

Control-A TAB

(Switch to new/next "region")

Control-A "

(Pull up a list of open "windows" to select from.)

Control-A A 

mywindow

(Give current window a name.)

You can now select your original window to view in the new "region."  Of course, multiple regions can be created and regions can be resized (man screen for the resize command).

As that link mentions, if you're working on a couple of different workstations, or for some reason want others to be able to monitor your session, you can point multiple terminals to the same screen session.  Be aware that each instance can manipulate the screen session.  See the man page for permissions issues between users.

Assuming you have a screen session running in another terminal...

```

snake@snaku450#screen -list

There is a screen on:

            14656.pts-0.snaku450     (Attached)

1 Socket in /var/run/screen/S-snake.

```

(Find it.)

```

snake@snaku450#screen -x pts-0

```

(Attach in multi-display mode)

Be sure to detach (Control-A d) rather than "exit," if you want to close one terminal, but not the entire session.

See the man page for all sorts of other features.

----------

## meowsqueak

Screen has a really nice feature I use a lot:

You can start up a process (like irssi) using the -d -m flags and automatically detach, and give it a symbolic name with -S. For example, to start up irssi in a screen, do the following:

$ screen -d -m -S irc irssi

You can now refer to this detached screen as 'irc'. Now for the fun bit:

$ screen -x irc

This will attach you to the screen session called 'irc' as you would expect. Now, travel 100kms to work, ssh to your box and type:

$ screen -x irc

Voila! You've reattached to your screen session WITHOUT detaching the one you started first at home. You are essentially multiplexing the session to several TTYs. You can do this many times, even on the same box. It even makes for a decent practical joke if you set it up right  :Smile: 

----------

## MooktaKiNG

Screen is great when you want to download things, have multiple bittorrent downloads, run emerge, and then run mutt all in one ssh window  :Very Happy: 

Its a fantastic tool, i fell in love with it ever since i saw it.

I usually make a small script for each program i want to run in screen. eg: 

This is the script i use for running bittorrent:

```
#!/bin/bash

## Just a small script for me to run bittorrent ##

cd ~/Downloads && \

screen btdownloadcurses.py --max_upload_rate 10 --responsefile $*

screen -list

```

That runs bitorrent in a new screen window and then i just do: CTRL+A CTRL+D and then detatch from it. Later i do:

```
screen ls
```

Which will list all the windows i have running.

gives something like this:

 *Quote:*   

> There are screens on:
> 
>         22779.pts-1.mooktaking  (Detached)
> 
> 2 Sockets in /var/run/screen/S-mma.
> ...

 

And then reatatch using:

```
screen -R 22779.pts-1.mooktaking
```

You can also use names to create new window and use that name to reattact to it again:

```
screen -R download
```

reattach:

```
screen -R download
```

Its a fantastic tool.

----------

## turtlendog

Thanks for the good tips!

Here's one I havent' seen in this thread.

Edit your /etc/syslog.conf to include:

```
*.*          /dev/tty12
```

Then you can see all the latest syslog messages by hitting alt+F12.

I picked this up from a Mandrake install where I specified "Paranoid" for the system security level.

----------

## Terminal Insanity

I'm trying to atach multiple terminals to a single screen session, but the terminals are differnt  sizes... any way to have screen resize them properly for both terminals?

----------

## meowsqueak

If you're using the -x flag to multiplex the screen sessions, then I think screen will only allow one 'size' at a time, since it's the same program in each instance. I find that the program inside the screen only responds to xterm resizes when it's the last and final instance open. If you have any others detached or active then it doesn't update.

----------

## MooktaKiNG

Is it possible to split the screen verticaly in screen?

Thanks

----------

## meowsqueak

 *MooktaKiNG wrote:*   

> Is it possible to split the screen verticaly in screen?

 

Anything is possible, especially with the source code, however screen does not do this.

----------

## dhurt

Great tips in this thread, thanks.    :Very Happy: 

----------

## nadezhkin

Great tips !!!

Thanks a lot   :Very Happy: 

But what about ssh-X forwarding and screen

it seems to be a problem

mb this help 

[url]http://www.deadman.org/sshscreen.html[/url]

----------

