Keep jobs active with screen command

Latest posts by Cyrille Modiano (see all)

screen command

What is the screen command ?

When you have long running jobs, you need to keep them active when you disconnect from server.
The screen command allows you to detach your terminal from you session.
By doing this there is no link between your session and your terminal and you can disconnect from server while your terminal and your jobs keep running.
Even better, once you reconnect to the server you can reattach the terminal to your new session and find your terminal exactly like you left it.

How does it work ?

Connect to your server with you username and password and issue the screen command:

[cmodiano@veelaora001 ~]$ screen

you can also name your terminal with:

[cmodiano@veelaora001 ~]$ screen -S name

Now you can run your long operation, for example a long sql script:

[cmodiano@veelaora001 ~]$ sqlplus test/test @mylongrunningscript.sql

once your script is running, press “Ctrl-a” and then “Ctrl-d” to detach your terminal.
Your terminal is now detached. You can start more than one screen at a time if you want to run multiple tasks, just detach your current screen and issue the screen command again.
To see your screen processes, just type the following command:

[cmodiano@veelaora001 ~]$ screen -ls
There are screens on:
21931.pts-1.veelaora001 (Detached)
10145.pts-1.veelaora001 (Detached)
2 Sockets in /var/run/screen/S-cmodiano.

Here I have 2 different terminal currently detached.
To reattach a screen when it is detached you can issue:

[cmodiano@veelaora001 ~]$ screen -r screen_pid

Where screen_pid is the process id of the screen when you have multiple screens (if you have only one screen the screen_pid is not needed).
Instead of the pid you can also use the name of your screen if you named it.
This pid can be found by using the screen -ls command :
21931.pts-1.veelaora001 (Detached)

Log everything you do

Using the screen command you can log everything happens on your terminal in a file, to do that, once you have attached the right screen to your session, just use “Ctrl-a” and then “H“.
A file named screenlog.0 will be created in your home directory.

Share your terminal

Screen alows you to share your screen with someone else so he can see what you do and even type command in your terminal.
To do this the other person just have to login to the server with the same account and type:

[cmodiano@veelaora001 ~]$ screen -x screen_name

This is really an awesome feature.

You can find the complete manual of screen here

Thank you for reading

6 thoughts on “Keep jobs active with screen command

  1. Cyrille, thank you very much for sharing your knowledge. Please know that you have been a blessing to me.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.