Monday, April 20, 2009

Initial process of Linux

Suppose you set up a sever or a administration machine, where there are many monitoring programs. If the machine reboots, then you have to restart all the programs manually. Maybe sometime you forget some processes. In that case, it's very danger to the system. So you should set up some initialization of Linux system. When the machine reboot, some processes will start automatically. This is what I have done.

emacs -nw /etc/rc.d/rc.local

You will see.

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
/usr/sbin/ntpdate ntp1.cs.wisc.edu
# define by Haifeng Li. For the CHTC machine's jobs scanning
/home/haifeng/chtcKillZombieJobs.py >> /home/haifeng/chtcKillZombieJobs.log &
# For the removing the old directory.
/home/hpeng/chtcclean.py >> /home/hpeng/chtcclean.log &

-----------------------------------------------------------------------------------------------------
Linux Process Control
Linux start scripts

Friday, April 17, 2009

Static library and dynamic library in Linux

A library is a package of code which can be used by other program. First, libraries are compiled. You don't have to compile them when you are using them. Second, because the complied libraries are in machine language, it can prevent other people to know the source code. This is important for business use. But it's not good for open source.
-----------------------------------------------------------------------------------------------------
What is library

Actually, library is just a collection of header(C++) files or sub functions(maybe Fortran).

There are two types library.
  1. Static library(archive library). In linux, statics lib has a .a extension. When you compile a program using static library, the program will copy all the funcitonally to the program. So your executable file is very big. One advantage of static library is the program does not have to find the lib when it's running. This is very convenient when you compile the code in one machine and run the program in another machine and you don't want to install the lib in that machine.
  2. Dynamic library(shared library). Dynamic library has a .so extension in Linux. Dynamic lib will be linked when the program is running. So you don't have to copy all the libs to your executable file. That is a advantage. Because the library you are using may be very big(GB).

------------------------------------------------------------------------------------------------------
Generate library

  1. To create a static library,

    ar rcs my_library.a file1.o file2.o
    This command adds the objects files file1.o and file2.o to the static library.
  2. Create a Shared library. First , create the object files using the gcc -fPIC of -fpic flag. The -fPIC and -fpic option enable 'position independent code' generation. Here is an example.

    gcc -fPIC -g -c -Wall a.c
    gcc -fPIC -g -c -Wall b.c
    gcc -shared -Wl,-soname,libmystuff.so.1 -o libmystuff.so.1.0.1 a.o b.o -lc
    -g means debugging information. -Wall will generate warnings. -c is compilation.
--------------------------------------------------------------------------------------------------------
Use Library

If you want to use the shared library, you have to tell the computer where the library is. Actually you have to how the Linux system to find lib.

  • Default shared lib directory. The default dir is /usr/lib, which means the system will first search the libs in /usr/lib. Then you have to run ldconfig.
    ldconfig -n directory_with_shared_libraries
  • You can use LD_LIBRARY_PATH to tell the system to find the shared libraries before the usual places.
    LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH  my_program

  • ldd can display the dependence of a shared library.

    ldd /usr/bin/mesg

    It will display

    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0xb7eaf000)
    /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0xb7feb000)


---------------------------------------------------------------------------------------------------------
My Makefile

This is a Makefile I am using.

##################################
# root variables
ROOTCFLAGS := $(shell root-config --cflags)
ROOTLIBS := $(shell root-config --libs) -lRooFit -lHtml -lCore -lCint -ldl -lTree \
-lGpad -lGraf -lHist -lMatrix -lPostscript -lMinuit -lMinuit2 -lPhysics
# Programs
CXX = g++
F77 = f77
CXXFLAGS = -g -ggdb -Wall -fPIC -Wno-deprecated
MY_CXXFLAGS = -static
LDFLAGS = -g
SOFLAGS = -shared
# do yourself a favor, use gcc for linking, and not ld
LD = CXX
# local Includes
INCDIR = -Iinclude
# Assign or Add variables
CXXFLAGS += $(ROOTCFLAGS)
CXXFLAGS += $(INCDIR)
# Assigan or add variables
LIBS += $(ROOTLIBS)
validation: src/validation.C clean
$(CXX) $(CXXFLAGS) $(MY_CXXFLAGS) -c src/validation.C -o lib/validation.o
$(CXX) lib/validation.o -o bin/validation $(LDFLAGS) $(LIBS)
clean:
rm -rf bin/* lib/*
$(shell root-config --libs) is a fantastic bash command. When you run the executable file, this command will be evaluated and will give the all the libs related to root(which is a analysis framework of high energy physic). In my case, the result of this command is

-L/afs/cern.ch/sw/lcg/external/root/5.18.00/slc4_ia32_gcc34/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -pthread -lm -ldl -rdynamic

--------------------------------------------------------------------------------------------
Program Library HOW TO
Static and Dynamic Libraries
Analysis of static and shared library
Programming of static and shared library

Thursday, April 16, 2009

Page Size in ps2pdf

ps2pdf is a linux tool, which can convert ps(or eps) file to pdf file. If you convert a ps file to a pdf, the default page size is letter. Some time you want to get different size plots. You can use

ps2pdf -dEPSCrop jet.eps
Or you can use epstopdf

epstopdf muonDetail.eps

This command is working only for eps file, which has a bounding box. If you open a ps file, you can see the

%!PS-Adobe-2.0 EPSF-2.0
%%BoundingBox: 0 0 567 552

There is no bounding box in ps file. So you can not get the special size pdf file from a ps file.

You also can use ps2eps to convert ps file to eps. This program will calculate the boundary box for ps file and generate new eps file with a boundary box.

ps2eps electron.ps





PostScript-to-PDF converter

Tuesday, April 14, 2009

Image manipulation using Python



You can use Python Image model to manipulate the image. It's interesting because you know that many pixels compose a picture. And the pixels are in different mode. For example, RGB. This is a python script which can print a new image.

#!/usr/bin/python

import sys
import Image, ImageDraw

newpic = Image.new("RGB", (300,300), color=(255,255,255))

xsize, ysize = newpic.size

for x in xrange(xsize) :
for y in xrange(ysize) :
newpic.putpixel((x,y), (int(255*(y/ysize)), int(255*x*y/(xsize*ysize)), int(255*(x/xsize))))


newpic.show()
newpic.save("gen.jpg", "JPEG")
This is the picture we get.

Wednesday, April 8, 2009

Tips for Bash

Bash is a kind of script language. It's not too formatted. But it's the language used by Linux system. So it's the most direct operation command for Linux. Other script languages, for example, python, perl, have to use bash sometimes. In my mind, if you want to write several lines script to finish your work, BASH is the best choice. I just write down something, which is used by myself when I am working.


  1. tar.
     tar -cvzf filename.tgz filename
  2. untar.
     tar xvzf filename.tgz
  3. check the space of some directorie.
    du -h --max-depth=1
  4. process control.

    #!/bin/bash

    for each in `ps -ef | grep ” | grep -v PID | awk ‘{ print $3 }’`;
    do
    for every in `ps -ef | grep $each | grep -v cron | awk ‘{ print $2 }’`;
    do kill -9 $every;
    done;
    done
  5. sed. This command will look at the text in 2.txt and substitute the 'new2' to 'CERN'. The new text will be stored in 2.txt.

    sed -i 's/new2/CERN/g' 2.txt
    If you use
    sed  -e 's/new2/CERN/g' 2.txt
    This command will just substitute the string, print the new text in monitor and will not be stored in file.
    You also can use

    line="x1.root y1.root x2.root y2.root"
    echo $line | sed 's/y[1-9].root//g'
    x1.root x2.root

  6. find.

    find ./work/ -mindepth 2 -maxdepth 2 | xargs chmod 744
  7. awk. I don't recommend people to use this awk to get some system information, like the example. Because it's not a good programming way.

    #!/bin/bash

    ps -ef | grep gnome | grep -v grep | awk '{print $7}'
    ps -ef | grep "slot" | awk '{print $7}' | sed -e 's/-.*//g' | grep -v ":"
  8. refresh the desktop

    killall nautilus

Monday, April 6, 2009

PS to image.

ps to png

gs -sDEVICE=png256 -o muon03.png -r800 -dBATCH -dNOPAUS muon.ps
gs -sDEVICE=png256 -sOutputFile=muon03.png -r800 -dNOPAUSE muon.ps -c quit

eps to png

gs -sDEVICE=png256 -dEPSCrop -sOutputFile=output.png -r800 -dNOPAUSE input.eps -c quit

pdf to png

gs -sDEVICE=png256 -o muon03.png -r800 -dBATCH -dNOPAUS muon.pdf
gs -sDEVICE=png256 -sOutputFile=muon03.png -r800 -dNOPAUSE muon.pdf -c quit

One bash script to do that

#!/bin/bash
for i in `ls *.ps`;
do gs -sDEVICE=png256 -sOutputFile=${i//.ps/.png} -r600 -dNOPAUSE ${i} -c quit;
done

You can convert a eps to pdf like this.

gs -dEPSCrop -sDEVICE=pdfwrite -sOutputFile=out.pdf -dNOPAUSE
-dBATCH -dAutoRotatePages=/None -c .setpdfwrite -f input.eps

----------------------------------------------------------------------------------------------------------
Links
How to use Ghostscript
Details of Ghostscript output devices

Sunday, April 5, 2009

CSS centralize problem

It's very difficult to centralize the web page because IE and Firefox have different explanation of CSS code. I wrote a very simple web page these days. But I found my web page is central in Firefox and can not be centralized in IE. We don't like IE. But somebody likes it. So it's better to make it work at IE too.

At first I used one div container.

div.body{
width: 32cm;
float: center ;
margin: 0 auto ;
padding: 0px;
text-align: center;
border: 0px groove;
}

This setting is working in Firefox because the "margin : 0 auto". But IE can not know this setting. So I looked through the web to get some information. There is one method which is almost good.

div.body{
position:absolute;
top:50%;
left:50%;
margin-top:-240px;
margin-left:-320px;
width:640px;
height:480px;
background:#ECECEC;
}


Actually this is a good setting if you want to get a fixed width web page. But for my case, this is not very good because I don't want to get a fixed width but a unfixed width. I felt upset about the uncompatibility between IE and Firefox. Then I want to give up. Suddenly I got an idea when I was sitting in a meeting. My idea is this. I use two containers. One of them has 100% width using the first method. Then I use a another container in the first container, which has 88% width. When I reload my web page, it looks great.