A bit about

Hello, everyone! All you can see below is just my bank of information. Some material I've found in the fathomless net, some I've learned myself. Don't think all of the information here is right or actual, but may be it could be of use for you :) All feedback is welcome, especially constructive ones :)

Wednesday, August 27, 2008

Adding SubVersion to the Init (Linux)

First of all, I tried this solution on Debian 4.0r1. But in fact I don't think there will a huge difference between distributives.

As I could realize from packages came with two my distributives (ASP Linux v10 and Debian 4.0r1) - SubVersion provides svnserve which can be started in the current terminal or loaded as a daemon (also there is a possibility to run in inetd mode, but I used daemon mode). But installer doesn't add svnserve to init among with other daemons. So I had to do this myself.

We have several directories:

  • /etc/init.d - storage for all scripts which are called during change of runlevel. These scripts must accept one argument that can be start, stop, reload, restart and force-reload

  • /etc/rc*.d - symbolic links to the corresponding scripts in init.d directory. All of them a called when entering corresponding runlevel.

I've added the next script called "svn" into init.d:
#!/bin/sh

#standard logging functions
. /lib/lsb/init-functions

case "$1" in
start)
log_begin_msg "Starting SubVersion...";
if ! svnserve -d -r "/repositories"; then
log_end_msg 1
exit 1
else
log_end_msg 0
fi
;;
stop)
log_begin_msg "Stopping SubVersion...";
if ! pkill svnserve; then
log_end_msg 1
exit 1
else
log_end_msg 0
fi
;;
reload|restart|force-reload)
log_begin_msg "Stopping SubVersion...";
if ! pkill svnserve; then
log_end_msg 1
else
log_end_msg 0
fi

log_begin_msg "Starting SubVersion...";
if ! svnserve -d -r "/repositories"; then
log_end_msg 1
exit 1
else
log_end_msg 0
fi
;;
*)
echo "Usage: /etc/init.d/svn {start|stop|reload|restart|force-reload}"
exit 1
;;
esac

exit 0

Then I've just created link in /etc/rc2.d directory:
# ln -s /etc/init.d/svn /etc/rc2.d/S22svn

And that's all. Subversion is automatically started on runlevel 2 and is stopped during shutdown or going down to runlevel 1.

Monday, August 18, 2008

Colored output to a vt100 (e.g. Linux console)

Lots of Linux programs use colors to paint their output (ls, for example). But there're no special commands to do the same thing yourself. Solution was found here: http://www.freeos.com/guides/lsst/misc.htm (BTW, quite good tutorial). These are control characters of Linux console. For instance, if you print \033[31m, all your next output will be red. The list of codes of all supported colors can be easily obtained by experiment or found in Google, but just to save time here it is:
\033[22;30m - black
\033[22;31m - red
\033[22;32m - green
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m - white
\033[01;0m - default scheme


Also, there're a lot of other control characters (some of them can controls leds on a keyboard).

All these knowledge was needed for me to write a script that prints Linux-style log (it was just for fun :)). Two functions - first shows a log message with required number of separating symbols (dots, for example), and the second prints colored result of operation. Here is my product:
# function does color output of the result. OK if $1 equal 1, and FAILED otherwise
function echo_result()
{
if [ $1 -eq 0 ]; then
echo -e "[\033[31mFAILED\033[0m]";
else
echo -e "[\033[32m OK \033[0m]";
fi
}

# function outputs the string given in the first parameter and adds required number of dots
function echo_log()
{
str="$1";
len=`expr length "$str"`; # obtain length of the given string and correct this value by 1 for the next loop
len=$((len+1));
for i in `seq $len 34`; do
str="${str}.";
done
echo -n "$str";
}

# Usage
RES=1;
echo_log "Log message 1";
cmd1 && \
cmd2 && \
cmd3 || RES=0;
echo_result $RES;

RES=1;
echo_log "Log message 2";
cmd1 && \
cmd2 && \
cmd3 || RES=0;
echo_result $RES;


As the result we could see:
Log message 1.....................[ OK ]
Log message 2.....................[FAILED]