Archive for May, 2008
Using GNU screen
Recently I’ve been the victim of a really unstable internet connection. This lack of stability has made me a great fan of the GNU screen command in unix systems.
GNU Screen is a small piece of software that lets you initiate a number of terminals inside a single terminal window. The great advantage with screen is that if you loose your internet connection, the screen terminals and operations inside these terminals still run on the server, contrary to operations running directly on the terminal window of your external server.
Screen is real simple and in most cases you only need a few important commands:
- starting a session
- listing all running sessions
- attaching to a session
- de attaching from a session
- killing a session
Creating a new screen session
Command: screen
kristian@Saturn:~$ screen
Example:
Listing all active screen sessions
Command: screen -ls
Example:
-
There are screens on:
-
11179.pts-0.Saturn (Detached)
-
11114.pts-0.Saturn (Detached)
-
2 Sockets in /var/run/screen/S-kristian.
Attaching to a session
If only one screen session is active the command below will take you directly to the session. If there are several sessions running the command below will display a list of running sessions. To choose one of them the screen -r command has to be followed by the session name.
Command: screen -r
Example of several screen sessions running:
-
There are several suitable screens on:
-
11179.pts-0.Saturn (Detached)
-
11114.pts-0.Saturn (Detached)
-
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
Example of attaching to a screen session:
kristian@Saturn:~$ screen -r 11179.pts-0.Saturn
De attaching from a session
De attaching from a screen session is real simple just remember the key combinations
Hold the CTRL key down, press A, then D. Thats it.
Killing a screen session
Killing a session is as simple as de attaching from one, just use the correct key combinations.
Keep the CTRL button down, press A, then K.
Writing exceptions in PHP
Fredrik Johan Holmström has a entry on his blog about writing exceptions in PHP. He points out that several of the large PHP framework assumes that an exception is a fatal error, and that this may be a flaw in the design.
An Exception is not necessarily a fatal error and it should be the responsibility of the code that catches the exception to determine if it is a fatal error or not.
The blog entry got me thinking, and yes I’ve done that mistake a few times myself, I’ll do it right the next time I write an exception in PHP,
URL: http://loveandtheft.org/2008/05/23/exceptions-youre-doing-it-wrong/
123-meming
Mats forwarded the 123-meme to me today, I’m not entirely sure what 123-meming really is but the instructions are:
- Pick up the book closest to you
- Open page 123
- Find the 5th sentence…
- …and publish the next three sentences
- Link to 5 other bloggers and tell who linked you
The book closest to me is: AGILE PROJECT MANAGEMENT WITH SCRUM, on page 123, sentenc nr 5 is:
During this day, the nonfunctional scaling requirements for this particular project are determined and placed in the Product Backlog. For example, if you are scaling the project to use multiple teams, the following nonfunctional requirements should be added to the Product Backlog:
- Decompose business architecture to support clean-interface multi-team development.
- Decompose system architecture to support clean-interface multi-team development.
- If necessary, define and implement a development environment to support multi-team collocated or distributed environments.
I pass this 123-meme on to Erik.
Google doctype
Yesterday Google released their internal documentation of html, css and javascript. This is a brilliant resource for us web developers.
URL: http://code.google.com/docreader/#p(doctype)s(doctype)t(Welcome)
Cheat sheets
When talking about cheat sheets, www.ilovejackdaniels.com has a list of a few good cheat sheets. I’ve actually printed some of them at work and taped them to the wall next to my computer.
Thanks to Dave Child for doing the work of putting together these sheets.
PostgreSQL cheat sheet
I often have a blank hole or two when it comes to the basic PostgreSQL commands, and have to do a look up in the documentation, Kitt Hodsden has posted a nice little cheat sheet with the most common basic postgreSQL commands.
klunde.net redesigned
Tonight I did a quick google search for a new wordpress design for klunde.net, I came over this page where I downloaded the earthtones design and added it to www.klunde.net
Adobe Kuler
Are you sick of never finding the correct match of colors when developing a web application. Adobe solves your problem with their kuler system. This site contains a lot of matching color sets, and you can with ease find a suitable color scheme for your next web application.
Micro languages
Mats posted an interesting article about micro languages,
My first introduction to java and databases
The other day I did a complete reconfiguration of java on my system. Tonight I started to play around with java and postgresql. I installed PostgreSQL 8.2 (yes I know 8.3 is out there), and downloaded the jdbc driver for PosgreSQL.
PostgreSQL
After installing PostgreSQL, you have to add a user to the authorization file pg_hba.conf located in /etc/postgresql/8.x/main on a ubuntu system, remember to restart the postgreSQL server after editing pg_hba.conf. When that is done add the same user to the postgreSQL server either by using postgreSQL’s createuser function or by logging into the postgreSQL server and doing a SQL CREATE USER, I did the latter one.
-
CREATE USER kristian WITH PASSWORD 'xxxxxxxx' CREATEDB;
This enables me to do a regular access of the database with:
-
kristian@Saturn:~$ psql -U kristian crawler
-
Welcome to psql 8.2.7, the PostgreSQL interactive terminal.</code>
-
-
Type: \copyright for distribution terms
-
\h for help with SQL commands
-
\? for help with psql commands
-
\g or terminate with semicolon to execute query
-
\q to quit
-
-
crawler=#
My table:
-
crawler=# \d document;
-
TABLE "public.document"
-
COLUMN | Type | Modifiers
-
—————+—————————–+————————
-
id | integer | NOT NULL DEFAULT NEXTVAL('document_id_seq'::regclass)
-
url | text | NOT NULL
-
document | text | NOT NULL
-
ts_downloaded | timestamp without time zone | DEFAULT now()
Data in the table:
-
crawler=# select * from document;
-
id | url | document | ts_downloaded
-
—-+———————–+———-+—————————-
-
1 | http://www.klunde.net | jalla | 2008-04-19 21:11:11.805482
Java
First thing to get the postgreSQL jdbc driver to work is to add it to the java classpath, and of course copy the actual jar file to its correct location, for instance /usr/lib/jvm/java-6-sun-1.6.0.03/jre/ext/lib/which is my java library path.
My classpath:
-
/usr/lib/jvm/java-6-sun-1.6.0.03/lib:/usr/lib/jvm/java-6-sun-1.6.0.03/jre/ext/lib/postgresql-8.3-603.jdbc4.jar:.
Now its about time to write some actual code:
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement; </code>
-
-
public class DatabaseTest
-
{
-
private Connection connection = null;
-
-
private static final String USERNAME = "XXXXX";
-
private static final String PASSWORD = "XXXXX";
-
private static final String URL = "jdbc:postgresql:";
-
private static final String DATABASE = "crawler";
-
-
public DatabaseTest()
-
{
-
try
-
{
-
connection = DriverManager.getConnection(URL + DATABASE, USERNAME, PASSWORD);
-
ResultSet rs = doQuery("SELECT * FROM document");
-
-
rs.next();
-
System.out.println(rs.getString(2));
-
}
-
catch (SQLException e)
-
{
-
System.out.println("Database Exception!");
-
System.out.println(e.toString());
-
e.printStackTrace();
-
}
-
-
}
-
-
public ResultSet doQuery(String sql)
-
{
-
ResultSet rs;
-
try
-
{
-
Statement stmt = connection.createStatement();
-
rs = stmt.executeQuery(sql);
-
return rs;
-
}
-
catch (SQLException e) {
-
e.printStackTrace();
-
return null;
-
}
-
}
-
}
Trying to execute my script:
-
kristian@Saturn:~/workspace/crawler$ javac DatabaseTest.java
-
kristian@Saturn:~/workspace/crawler$ java -Djdbc.drivers=org.postgresql.Driver DatabaseTest
-
http://www.klunde.net
It works
The installation and initial setup went pretty much by the book, but as a PHP developer I must say that I’m a little bit disappointed, JDBC cannot compare itself to the PHP PDO extension yet. But then again, maybe I just need to get to know the JDBC library better. Anyhow it was fun writing this little java app and refreshing some of my java skills.