Wednesday, June 20, 2007

Check folder size and disk usage

du -h 'du = disk usage?' '-h = human readable'.
df -h 'df = the amount of disk space that is free on file systems'; '-h = human readable'.

Tuesday, June 19, 2007

Run program after logout (my version)

There are two ways I know so far:
1. nohup
nohup stands for 'no hang up' for me. For instance:
nohup ./script_run &
Then logout the server and power off your computer.
The script_run will keep running until it finishes or you log back in and stop it (kill the process).

2. screen
screen is rather more powerful tool. It can certainly do more (better) job than running a
program for you after you logout server. Well, that is the only function I am interested from it for now. So, how to use it? Example:
screen ------------This command will give you a new screen. Check xterm title.
Ctrl-a Ctrl-d ------Detach screen, whatever program you run in 'screen' is still running.
You can logout and even power off your computer now.
screen -x ---------Reattach screen. You can use any computer to login the server as the same user when runned screen. Terminal will show whatever program you were running before.

So, three commands: screen; Ctrl-a Ctrl-d; screen -x. Is that simple.
####################################################################
detatch screen from a screen:
ctrl-a
a
d
------------------------
ctrl-a,a is just to send ctrl-a to a screen session within a screen session.
It's all in screen's manpage. I always check manpage before asking Google.

Also, I like to turn on the caption line so I know whether I'm in screen or
not.
For example, put these two lines in your .screenrc
caption always
caption string "%{= RW} [%H] <%n>=%t%=%Y-%m-%d=%c "

Run program on server after logout(screen)

How to use it (in an easy tutorial fashion)

1. screen %% To open a screen

2. commands %% Run whatever program you wanted to run

3. Ctl+a Ctl+d %% To detach screen

4. screen -x %% To attach screen

5. screen -r pid# %% To attach to the 'pid#' screen if there are more than one screens

========== DONE ==========

GNU screen is a terminal multiplexer, i.e. it lets you run many command-line applications from one console or terminal emulator window.

It is a powerful tool and mainly used in a terminal by a single user who needs to run several programs at once but does not want to log in several times on several terminals - a time consuming business on a dial up connection. Its greatest virtue is that sessions can be detached in one terminal and re-attached in another. This means that you can start a session at the office, go home, log in remotely, and re-attach the session locally. Think of it as the TTY version of VNC. If your dial-up session dies, you can simply reconnect and reattach the screen session - you won't lose any work.

Another underrated benefit is that you can switch between terminals without resorting to the mouse.

It is also very useful as a teaching, demonstration or co-operative working tool. Say you want to share a session with a remote user or show how to use a program. You both have to log into the same computer using the same user name. One user runs the command screen and then the other user runs the command screen -x

If there are multiple screen sessions available, the program will then list the available screen sessions. For example:


   There are screens on:
2463.pts-2.atreus (Attached)
11068.lab (Attached)
2 Sockets in /tmp/screens/S-keithh.

In this case, the user can explicitly specify the desired session using an unambiguous substring of the session name. In the above example, screen -x 11068 and screen -x lab are equivalent and both users will now share the same session. Because each session can contain multiple PTYs, each user can independently switch between the them. They can also share the same PTY in which case they will each see the same display and can each type commands.

Contents

[hide]

[edit] Using Screen

Screen can be controlled while it is running by prefacing commands with the command-key character. By default, this is Control-A. For instance, to view the help screen enter Ctrl-a ? The detach command is Control-a Control-D.

Control-a, Control-c creates a new terminal screen

Control-a, " shows a list of terminal screens

Control-a, n switch to the next screen

[edit] Customizing Screen

The man page for screen details a large number of options but the following examples will illustrate many of the more useful features. By default, screen checks for a ~/.screenrc file and failing that the /etc/screenrc file. Here is my default screenrc:


defflow auto

# Scrollback buffer size in lines
defscrollback 5000

# modify the termcap/terminfo when I'm using XTerm.
terminfo xterm* LP

# Support alternate screens so that, for example, when you
# quit out of vi, the display is redrawn as it was before vi
# redrew the full screen.
altscreen on

# don't display the copyright page
startup_message off

# detach on hangup - if my dial-up session fails, screen will simply
# detach and let me re re-attach later.
autodetach on

msgwait 2 # 1 second messages

# Start a number of initial shells and give them titles which show up in the status-line
screen -t CCase 0 bash
screen -t Sun1 1 bash
screen -t Sun2 2 bash
screen -t CCase 3 bash
screen -t Rich 6 bash
screen -t Pine 7 bash
screen -t Pine 8 bash
screen -t News 9 bash
screen

# remove some stupid / dangerous key bindings
bind k
bind W
bind ^k
bind .
bind ^\
bind \\
bind ^h
bind h
#make them better
bind 'K' kill
#bind 'I' login on
#bind 'O' login off
#bind '}' history
bind 'W' windowlist

I can create an alternate configuration file as follows:


...
sessionname lab # Call this session "lab"

caption always # Leave the status line on permanently
caption string "LAB %t - %w"

msgwait 2 # 1 second messages
screen -t CP 0 bash
screen -t CAC 1 bash
screen -t FTP 2 bash
screen -t xterm 3 bash
screen
...

This can be made easier to use by defining an alias for the command:

  • screen -d -R lab -t LAB -c ~/.screenrc-lab

The -d -R lab option means detach and resume the session named lab and if no such session exists then create it.

The -t LAB option simply puts a title on the status line.

And -c ~/.screenrc-lab specifies the alternate configuration file to use.

Run program on server after logout (nohup)

nohup is a command used to run a process and have it continue to run regardless whether you log out and close the connection to a server.

When you are connected to a server and wish to log out, a hangup (SIGHUP) signal is sent to all the processes you have run. Using nohup to run a program causes this signal to be ignored.

For example, you might want to download some files with wget, and be able to log out and have these files still download for you. One could use

nohup wget --quiet http://www.example.com/some_large_file &

to have wget download these files in the background, and continue to do so even after you log out. The quiet flag is set because while wget is in the background, the job will suspend if it creates any output to the terminal.

The use of nohup is not restricted to remote logins, however, one can use it for example in X, with an xterm open, one can nohup a process and have it run even if the xterm is closed.