• About Dangerous DBA
  • Table of Contents
Dangerous DBA A blog for those DBA's who live on the edge

Tag Archives: Long Running Scripts

Bash: Screen most useful command for DB2

January 21, 2013 8:10 am / Leave a Comment / dangerousDBA

Previous to one of my work colleagues of more years experience in working with LINUX and UNIX enlighten me to this most useful command I believe most of you out there like me would fall into four camps:

  1. Remote desktop machine probably windows that you log into and it stays up with you command running that you can come back to later
  2. Sit around with your command window on your machine, waiting for it to be finished as the rest of your colleagues go home
  3. nohup a script, but once you have closed your session you don’t know when your script has finished
  4. CRON tab a script

There is a fifth way screen

Screen your new best friend

So once you have ssh’ed into your server and got to the command line you can use screen too create a server based terminal session that can be attached to and detached at you leisure. Screen works on “sessions” for the user you are logged in as and these sessions can be connected to and disconnected from as that user, for the life time of the session. A full list of options to the screen command can be found here.

Create a session with screen

You have your command line type in screen and press enter:


$ screen

You will be greeted with this message and prompted to press enter or space and you will be taken to a command line again, you are now in your screen session.

Seeing your screen sessions

You can see the screen sessions that you have available to you by typing in:


$ screen -ls

As you can see its a lot like the ls of the normal LINUX file system and you will get something back like this:


bash-3.2$ screen -ls
There are screens on:
        1747.ttys000.machineid   (Detached)
        1872.ttys000.machineid   (Attached)
2 Sockets in /var/folders/XZ/XZGRJK7vHZuH1b726e+9yE++-GI/-Tmp-/.screen.

As you can see there are two screen sessions on this machine and you can only attach once to a session. Someone else must be “in” the screen “1872.ttys000.machineid”

Attaching to a screen session

So once you have found your screen sessions you can attach to a screen by using the command:


bash-3.2$ screen -r 

To attach to the session above that can be used the command would be:


bash-3.2$ screen -r 1747.ttys000.machineid

This will now allow you to run commands and leave commands on screen as you log on and off. If you try to connect to a session that no longer exists you will get this:


philipcarrington$ screen -r 1747.ttys000.machineid
There is no screen to be resumed matching 1747.ttys000.machineid.

Disconnecting from a screen session

Here you have to be slightly careful in that if you press the wrong keys then you will exit your session and you will lose what it is doing, but depending on what it is doing it will not stop potentially. The command exit will work as normal as it you were in a terminal and return you to the “main” terminal session on the server:


bash-3.2$ exit

Return to the main session with


$ screen -r 1747.ttys000.machineid
[screen is terminating]

If you wish your session to continue so that you can reconnect and your command will continue running until it terminates you need to press ctrl-a followed by ctrl-d. When you do this you will be returned to the terminal that spawned the session with something like:


$ screen
[detached]

You can then list the available sessions and see this one and then attach again later.

Another way to disconnect a session and end it is to just use ctrl-d and you will see something similar to using exit:


$ screen -r 9679.ttys000.machineid
[screen is terminating]

Screen Conclusion

This is a light look at the command and as stated before you can find the full commands options here. As you can see this command is very useful and can potentially reduce your costs as you don’t need a remote desktop machine. I use this command quite extensivly especially when your connection to your server is not guaranteed for a long time like over a company VPN.



Posted in: bash, DB2, DB2 Administration, DB2 Maintenance, IBM, IBM DB2 LUW, screen / Tagged: bash, DB2, DB2 Administration, IBM DB2 LUW, long running db2 commands, long running queries, long running scripts, screen, V10.1, V9.7

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 757 other subscribers

Recent Posts

  • Self generating Simple SQL procedures – MySQL
  • Google Cloud Management – My Idea – My White Whale?
  • Position Tracker – The Stub – Pandas:
  • Position Tracker – The Stub
  • Position Tracker – In the beginning
  • Whats been going on in the world of the Dangerous DBA:
  • QCon London Day 1
  • Testing Amazon Redshift: Distribution keys and styles
  • Back to dangerous blogging
  • DB2 10.1 LUW Certification 611 notes 1 : Physical Design

Dangerous Topics

added functionality ADMIN_EST_INLINE_LENGTH Bootcamp colum convert data types DB2 db2 DB2 Administration DB2 Development db2advis db2licm Decompose XML EXPORT GCP Google IBM IBM DB2 LUW idug information centre infosphere IOT LOAD merry christmas and a happy new year Position Tracking python Recursive Query Recursive SQL Reorganisation Reorganise Reorganise Indexes Reorganise Tables Runstats sql statement Stored Procedures SYSPROC.ADMIN_CMD Time UDF User Defined Functions V9.7 V10.1 Varchar XML XML PATH XMLTABLE

DangerousDBA Links

  • DB2 for WebSphere Commerce
  • My Personal Blog

Disclaimer:

The posts here represent my personal views and not those of my employer. Any technical advice or instructions are based on my own personal knowledge and experience, and should only be followed by an expert after a careful analysis. Please test any actions before performing them in a critical or nonrecoverable environment. Any actions taken based on my experiences should be done with extreme caution. I am not responsible for any adverse results. DB2 is a trademark of IBM. I am not an employee or representative of IBM.

Advertising

© Copyright 2022 - Dangerous DBA
Infinity Theme by DesignCoral / WordPress