Sunday, December 6, 2009

Thursday, November 26, 2009

Schedule an one time job in linux - the "at" command & "sudo" time out option

Wanted to download and build from source the new Chromium OS, when I came across a huge list of packages as pre-requisites for Chromium OS. The download over the internet being free after 2:00 AM in the night, it had to be scheduled around that time.

There is an "autodownload.sh" script which will download & install the files (detailed at the end of this post). This script had to be scheduled for execution at 2:10 AM.

To schedule the job
benoy@palazhi:~$ sudo at -f autodownload.sh -v 2:10 AM
[sudo] password for benoy: [enter the password]
Fri Nov 27 02:10:00 2009

warning: commands will be executed using /bin/sh
job 15 at Fri Nov 27 02:10:00 2009

[NOTE]
1. The "at" command is preceeded by the "sudo" command since the script "autodownload.sh" requires root privileges to execute and install the packages.
2. The file name (name of the script file which is to be executed at a scheduled time) is given with "-f" option.
3. The time at which the script has to be executed is given with "-v" option.
4. The output shows that a job, with ID 15, has been scheduled for execution at 02:10:00 hours on Nov 27, 2009.
5. For scheduling periodic jobs, you can create a "crontab" using a "crontab" command with "-e" option.
[/NOTE]

To see the scheduled jobs
benoy@palazhi:~$ sudo atq
15 Fri Nov 27 02:10:00 2009 a root

[NOTE]
1. The "atq" command lists the scheduled jobs.
2. It is preceeded by the "sudo" here as the earlier job was added as job to be executed by the root.
[/NOTE]

To delete the scheduled job
benoy@palazhi:~$ sudo atrm 15
benoy@palazhi:~$ sudo atq
benoy@palazhi:~$

[NOTE]
1. The "atrm" command cancels the job with the ID mentioned in the command line (15 in this case).
2. The "atrm" command is preceeded by the "sudo" command as the earlier job was added as job to be executed by the root.
[/NOTE]

Now lets see the autodownload.sh script

benoy@palazhi:~$ cat autodownload.sh
#!/bin/sh
rm -f temp
echo "Deleted temp.log..." >> temp.log
echo "Start downloading..." >> temp.log

while read package
do
sudo apt-get install -y --install-recommends $package >> temp.log 2>>temp.log
done < packages.txt

echo "Halting the system....." >> temp.log
halt -p

[NOTE]
1. The while loop reads from the file "packages.txt", line by line, to a user defined variable "package".
2. The "sudo apt-get install..." line has "$package" to get the name of the package (which is read from the file packages.txt).
3. The "apt-get" command is preceeded by a "sudo" command as it involves package installation which requires root privilege.
4. The "-y" option to "apt-get" provides an automatic "yes" to the "apt-get" prompts.
5. The "--install-recommends" options ensures that recommended packages are also installed.
6. The errors are as well appended to "temp.log" file (note "2>>temp.log" at the end of the "sudo apt-get..." command.
7. The "halt -p" command shuts down the system after completing the download & installations.
[/NOTE]

We now have another issue at hand. The sudo command will time out in 5 or 15 minutes, so at 2:10 AM, when the "sudo apt-get..." command will be executed, the system will require the root password again (which will defy the very purpose of scheduling this download). The way around is to give a high or an indefinite value for time out.

To change the sudo timeout
benoy@palazhi:~$ sudo visudo

The system opens the "/etc/sudoers" file; add the following line:
Defaults:benoy timestamp_timeout=-1

[NOTE]
1. Change "benoy" to your user name.
2. A negative value for timestamp_timeout means indefinite timeout, i.e till you logout or exit from the terminal.
3. A positive value will ensure that the sudo session remains active for that many minutes.
4. Do a "sudo -K" to forcefully end a sudo indefinite session.
[/NOTE]

Now, have a file "packages.txt"; which will list the packages that are to be downloaded. Let us keep it very simple; each line of the file will have the name of one package that is to be dowloaded.

benoy@palazhi:~$ cat packages.txt
subversion
pkg-config
python
perl
g++
g++-multilib
bison
flex
gperf
libnss3-dev
libgtk2.0-dev
libnspr4-0d
libasound2-dev
libnspr4-dev
msttcorefonts
libgconf2-dev
libcairo2-dev libdbus-1-dev

Saturday, November 21, 2009

godbc (Go & ODBC)

Got the basic structure up; made my first submission today.

http://github.com/BenoyRNair/godbc

Sunday, November 15, 2009

More on VIM syntax highlighting

To force a syntax scheme of one language on files in other languages or those with no file extension.

In vim editor, key in:
:setf [language _name]

For example, to force a perl syntax on a *.go file; in the vim editor, key in:
:setf perl

To have this behavior by default, modify the filetype.vim file.

benoy@palazhi:~$ find / -name 'filetype.vim' 2>/dev/null
/usr/share/vim/vim71/filetype.vim

benoy@palazhi:~$ sudo vim /usr/share/vim/vim71/filetype.vim
[sudo] password for benoy:

After typing the password, include the following line in the file:

" Go project file
au BufNewFile,BufRead *.go setf perl

This will ensure that all *.go files are shown with perl syntax color highlighting by default in vim.

[NOTE]
Ensure that there is no space between the strings "BufNewFile", "," & "BufRead" in the above line.
[/NOTE]

Of course, it is better to have the Go syntax itself on while opening *.go files, instead of the perl syntax scheme. For that do the following:

1. Download go.vim (http://code.google.com/p/go/source/browse/misc/vim/go.vim?r=release)

2. Save it to the ftplugin directory
benoy@palazhi:~$ find / -name 'ftplugin' 2>/dev/null
/usr/share/vim/vim71/ftplugin

3. Add the following lines to .vimrc file

" Syntax highlighting for *.go
autocmd BufNewFile,BufRead *.go source /usr/share/vim/vim71/ftplugin/go.vim

[NOTE]
Ensure that there is no space between the strings "BufNewFile", "," & "BufRead" in the above line.

To locate the vimrc file:
benoy@palazhi:~$ find / -name 'vimrc' 2>/dev/null
/usr/share/vim/vimrc /etc/vim/vimrc
[/NOTE]

Now, *.go files will be shown in the Go syntax scheme by default in the vim editor.

Saturday, November 14, 2009

Register drivers & DSNs with unixODBC

This section explains how to configure drivers & data sources for unixODBC.

Ensure unixODBC Driver Manager is installed properly

benoy@palazhi:~$ odbcinst -j
unixODBC 2.2.14
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /usr/local/etc/odbc.ini
SQLULEN Size.......: 4
SQLLEN Size........: 4
SQLSETPOSIROW Size.: 2

To query for drivers:
benoy@palazhi:~$ odbcinst -q -d
odbcinst: SQLGetPrivateProfileString failed with .

To query for datasources:
benoy@palazhi:~$ odbcinst -q -s
odbcinst: SQLGetPrivateProfileString failed with .

To get rid of these errors, ensure the following:
benoy@palazhi:~$ env | grep 'ODBC'
LD_LIBRARY_PATH=/usr/local/unixODBC/lib:/usr/local/lib:
ODBCSYSINI=/usr/local/etc
ODBCINI=/usr/local/etc/odbc.ini

[NOTE]
If not yet defined, define and export the above env variables
[/NOTE]

Sample odbcinst.ini (for drivers)
benoy@palazhi:~$ cat /usr/local/etc/odbcinst.ini
[MySQL]
Description = MySQL driver for Linux
Driver = /usr/local/lib/libmyodbc5.so
Setup = /usr/local/lib/ libmyodbc3S-5.1.6.so
FileUsage = 1

Once this is done, drivers can be listed
benoy@palazhi:~$ odbcinst -q -d
[MySQL]

Sample odbc.ini (for data sources)
benoy@palazhi:~$ cat $ODBCINI
#
# odbc.ini configuration for MyODBC and MyODBC 3.51 Drivers
#

[ODBC Data Sources]
dsn1mysql = MyODBC 5.1.6 DSN

[dsn1mysql]
Driver = /usr/local/lib/libmyodbc5.so
Description = Connector/ODBC 5.1.6 Driver DSN
SERVER = localhost
PORT = 3306
USER = root
Password = [enter the password]
Database = mysql
OPTION = 3
SOCKET = /var/run/mysqld/mysqld.sock

[Default]
Driver = /usr/local/lib/libmyodbc5.so
Description = Connector/ODBC 5.1.6 Driver DSN
SERVER = localhost
PORT = 3306
USER = root
Password = [enter the password]
Database = mysql
OPTION = 3
SOCKET = /var/run/mysqld/mysqld.sock

[NOTE]
Values for PORT, SOCKET can be found in /etc/mysql/my.cnf if mysql is installed properly.

benoy@palazhi:~$ cat /etc/mysql/my.cnf | grep 'port\|socket'
# One can use all long options that the program supports.
# It has been reported that passwords should be enclosed with ticks/quotes
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.
port = 3306
socket = /var/run/mysqld/mysqld.sock
socket = /var/run/mysqld/mysqld.sock
socket = /var/run/mysqld/mysqld.sock
port = 3306
# Using BerkeleyDB is now discouraged as its support will cease in 5.1.12.

Database is the name of the database created in the db; if mysql has been installed properly, you can use the database "mysql" to test the connection.
[/NOTE]

Once this is done, data sources can be listed.
benoy@palazhi:~$ odbcinst -q -s
[dsn1mysql]
[Default]

If the drivers, data sources & the db have been installed properly, it will be possible to connect to the database using isql.

benoy@palazhi:~$ isql dsn1mysql
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> quit;
benoy@palazhi:~$

[NOTE]
dsn1mysql is specified in odbc.ini.
User will connect to the database "mysql" in localhost @ 3306 as the root (as can be seen from odbc.ini)
[/NOTE]

VIM syntax highlighting not working

Take vim
Type :syntax on
Seeing the message ": Not an editor command: syntax on"?

It is very likely that you are having only vim-tiny installed in your machine.

Install vim-runtime , vim-full.
sudo apt-get install vim-runtime vim-full

Type ":syntax on" in vim after the installation, it should work.

For syntax highlighting to be turned on by default, uncomment the 'syntax on' line in /etc/vim/vimrc

OS details:
benoy@palazhi:~$ cat /proc/version
Linux version 2.6.27-7-generic (buildd@rothera) (gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu10) ) #1 SMP Fri Oct 24 06:42:44 UTC 2008

Followers