Archive for the ‘Technology’ Category

15
Nov

Prefix command output in a shell with timestamp

   Posted by: Kristian Rønningen Tags: , , ,

So I was writing this script that logged certain things to a file using a regular redirect. My script was running various commands that outputted a random amount of lines, all in random formats. I wanted to have a timestamp prefixing all of that, similar to what you get in logs written by syslog. I searched around a bit, and ended with this:

1
ls -l / | awk '{now=strftime("%Y-%m-%d %T %Z"); print now" "$0}'

Of course if you want a different timestamp, you just change the arguments you pass to strftime. Quite handy!

22
Oct

Tail with word highlighting

   Posted by: Kristian Rønningen Tags: , , , ,

If you’ve ever ran tail -f on a busy file and tried to find certain words or patterns as they fly by, then this little “trick” might be of some help:

Shell
1
tail -f /var/log/mail.log | egrep --color 'expr1|expr2|$'

8
Aug

URL shortening service

   Posted by: Kristian Rønningen Tags: , ,

Having this short domainname (yay!) I early made my own URL shortening service of sorts. It was based on a perl-script I found online, and had a mapping between the keys and the long URL’s in a textfile. Also, I used a subdomain of 42.no for this purpose, namely r.42.no. This wasn’t good enough of course, way too long, so this weekend I wrote my own URL shortening service, using PHP. It stores the keys and URL’s in a database, and supports logging of each lookup done to a short URL. And most importantly, it has an easy interface to add new URL’s, which is something that couldn’t be said for my old text-file solution. Feel free to use it for any of your URL shortening needs.

So without further ado, here it is: http://42.no/r/

16
Jun

Shell-script template for cronjobs

   Posted by: Kristian Rønningen Tags: , , ,

Have you ever had your server drown in processes because a cronjob you wanted to run every minute suddenly never finished, and you ended up having multiple instances of your nice script going at the same time? I have! A quick and easy solution to this problem, was this wrapper-script that I created a while back. Basically, it’s just some pid-file processing before your own code starts, to make sure only to run a single instance of the script at a time. With this you’ll avoid having your scripts pile up again.

The script: cron-shell-script-template.sh
The config: cron-shell-script-template.conf-sample

If you find this script useful, or have some improvements, feel free to drop me a comment below. Thanks!

Update:

The script had a little bug, which caused two instances of the script to be started under some circumstances. The script behind the link above has been updated to correct this.

Update, July 23.:

I’ve removed the PIDDIR-setting from the actual script, and moved it to a separate configuration-file. It’s all generic, the conf-file is expected to be named the same as the script with the last “.ending” replace with “.conf”. The point of this is to be able to e.g. keep the script itself in a repository (like subversion) and keep the config separate so you can update the script from the repository without having local changes to worry about.

14
Mar

Easy batch-conversion of flac to mp3

   Posted by: Kristian Rønningen Tags: , , , , ,

After getting a new mp3-player (the iAudio D2 from COWON) I wanted to find a quick and easy way of “tagging” and converting a number of albums that I had in flac-format (or mp3) into mp3 of a certain bitrate. My goal was to just browse through my collection, and touch a file inside a directory if I wanted to convert it. When I was done with the touching, I’d just fire up a small script that did all the work for me, while I was doing something else. Here’s what I came up with.

Read the rest of this entry »