Archive

Archive for the ‘Linux’ Category

Turn off your laptop and leave the server working

If you usually connect to servers via SSH, you have probably had to wait to finish a time consuming task before you could close the console and therefore, your computer. However, there is at least one way for executing the needed commands on the server and going home. The screen command will help you with that.

The first thing you have to do is logging into the SSH server. That’s easy, you know how to do it:

ssh user@mydomanin.com

Once you are in, install screen if you don’t have it yet. As easy as this for an Ubuntu server:

sudo apt-get install screen

Now that you have everything you need, execute screen:

screen

This will open another session in the same terminal.

Perform any task you need now. For example, upload a large file to a remote FTP server:

sftp user@myftpserver.com
sftp>put a_big_file.tar.gz
Uploading a_big_file.tar.gz to somewhere in your FTP server very slowly
a_big_file.tar.gz 1% 5KB 1.4KB/s 00:05 ETA

That’s going to take long and you have to leave now, so it’s time to detach the session. Press on your keyboard:

Ctrl + a

and then, to definitely detach the session, press:

d

The server will keep on uploading the file, but now you can close the SSH connection and turn off your computer.

Tomorrow, when you arrive at the office, you might want to know whether the task was finished correctly. Connect to the server and run:

screen -r

This will resume any previous screen sessions, or will show the screens to be resumed if there are more than one.

Reset nondetachable USB devices on your laptop

2012/01/17 1 comment

I’ve had problems with Ubuntu and my laptop’s integrated webcam for quite some time. Because of these problems, a couple of developers started working on some alternative drivers, but the project seems to be abandoned right now (7 months without commits).

With no appropriate drivers, the device displays odd colours and randomly hangs up (specially when using Flash Player) leaving the power led on. This is very unpleasant because it gives you the impression that somebody might be spying on you.

So I googled a bit searching for a way to reset the devices that I can’t detach, and found this great post by Alan Stern in which he gives us a piece of code to do just that.

cd
mkdir usbreset
cd usbreset

Copy the code into usbreset.c:

/* usbreset -- send a USB port reset to a USB device */

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>

#include <linux/usbdevice_fs.h>

int main(int argc, char **argv)
{
 const char *filename;
 int fd;
 int rc;

 if (argc != 2) {
 fprintf(stderr, "Usage: usbreset device-filename\n");
 return 1;
 }
 filename = argv[1];

 fd = open(filename, O_WRONLY);
 if (fd < 0) {
 perror("Error opening output file");
 return 1;
 }

 printf("Resetting USB device %s\n", filename);
 rc = ioctl(fd, USBDEVFS_RESET, 0);
 if (rc < 0) {
 perror("Error in ioctl");
 return 1;
 }
 printf("Reset successful\n");

 close(fd);
 return 0;
}

Then build it:

cc  usbreset.c -o usbreset
chmod +x usbreset

Now we have to know which is the bus and device our webcam is attached to:

lsusb
Bus 002 Device 005: ID xxxx:xxxx (...) Webcam

Last, call usbreset with the path of the device as a parameter:

sudo ./usbreset /dev/bus/usb/002/005

And that’s it. Bus and Device might change after the reset but ID won’t. So, we note down that ID and make a little sh script to avoid the lsusb step (put it in the same place as the usbreset binary). Let’s call it usbreset.sh (for originality’s sake):

#!/bin/sh
ID='xxxx:xxxx'
MATCHES=$(lsusb | sed -n 's/Bus \([0-9]*\) Device \([0-9]*\): ID '$ID'.*/\/dev\/bus\/usb\/\1\/\2/p')
if [ -z ${MATCHES} ]; then
 echo "No match found"
else
 sudo ./usbreset $MATCHES
fi

And now we can reset our webcam by simply calling ./usbreset.sh.

USBReset source code:
http://marc.info/?l=linux-usb&m=121459435621262&w=2

Build latest ffmpeg from source

2012/01/11 1 comment

FFmpeg logoI use ffmpeg a lot in my work because I need to process lots of multimedia resources programmatically (without human intervention). The prebuilt binaries of ffmpeg usually suffice for your average encoding/decoding tasks (if due to your particular needs you lack certain propietary codecs you can always grab a more codec-rich build such as the one medibuntu offers). But sometimes you need advanced features such as filters (overlays,  scaling, padding…) and since filters are a constantly evolving feature it is interesting to know how to build ffmpeg from source.

Removing old stuff and solving dependencies

First, you need to install git (if you don’t already have it):

sudo apt-get install git

Next, uninstall any previous ffmpeg builds from your system (if you’re building ffmpeg with x264 support like I’m going to do, uninstall x264 as well):

sudo apt-get remove ffmpeg x264 libx264-dev
sudo apt-get autoremove

Now we need to install a bunch of dependencies. This list may vary depending on the ffmpeg configuration you want to use. Don’t worry too much if you forget about some codec or dependency at this point, ffmpeg will tell you if something’s missing in the configuration step.

In my case, I wanted as many codecs as I could remember available to ffmpeg so as you can see the dependency list is quite long:

sudo apt-get install build-essential git-core checkinstall yasm texi2html \
     libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev \
     libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev \
     libvorbis-dev libvpx-dev libx11-dev libxfixes-dev libxvidcore-dev \
     zlib1g-dev librtmp-dev libgsm0710-dev libgsm0710mux-dev libgsm1-dev \
     libgsmme-dev libschroedinger-dev libspeechd-dev libspeex-dev \
     libspeexdsp-dev libspeex-ocaml-dev libdc1394-22-dev

Ok, if you read the dependency list (did you, really?) you’ll have noticed that x264 isn’t among the installed packages. The reason is that I’ll also be building x264 from source because the prebuilt binaries (I’m talking about the ones in Ubuntu’s repository) seem to be too old for the ffmpeg we’re about to build.

Building x264 as a shared library

First, clone x264‘s git repository to grab the latest version of the code. Then, use the –enable-shared flag when configuring to build it as a shared library, otherways ffmpeg won’t be able to use it.

cd
git clone git://git.videolan.org/x264.git
cd x264
./configure --enable-shared
make
sudo make install

Optionally, you can use checkinstall to build a .deb package and thus make the binaries redistributable:

sudo checkinstall --pkgname=libx264 \
    --pkgversion="2:0.$(grep X264_BUILD x264.h -m1 | \
    cut -d' ' -f3).$(git rev-list HEAD | wc -l)+git$(git rev-list HEAD -n 1 | \
    head -c 7)" --backup=no --deldoc=yes \
    --fstrans=no --default

Well, now that we’ve got all the libraries we need it’s time to build our customized ffmpeg.

Building ffmpeg

Since  January 2011 ffmpeg no longer uses svn to host the code, you should keep this in mind when you read other ffmpeg tutorials (they may be outdated).

cd
git clone git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
./configure --enable-avfilter --enable-vdpau --enable-bzlib \
    --enable-libgsm --enable-libschroedinger --enable-libspeex \
    --enable-pthreads --enable-zlib --enable-libvpx \
    --disable-stripping --enable-runtime-cpudetect \
    --enable-vaapi --enable-swscale --enable-libdc1394 \
    --enable-shared --disable-static --enable-librtmp \
    --enable-gpl --enable-version3 --enable-nonfree \
    --enable-postproc --enable-libfaac --enable-libmp3lame \
    --enable-libopencore-amrnb --enable-libopencore-amrwb \
    --enable-libtheora --enable-libvorbis --enable-libx264 \
    --enable-libxvid --enable-x11grab --enable-filter=movie
make
sudo make install

Building ffmpeg takes quite some time, be patient. When everything’s done, call ffmpeg without parameters to see if it works.

In my case it didn’t, so I had to use strace to find out what was wrong.

Fixing runtime problems

sudo strace ffmpeg

Reveals the following:

access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
(...)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)

It seems ffmpeg is trying to access two files that don’t exist. I’ll create them and see if that works.

sudo touch /etc/ld.so.nohwcap
sudo touch /etc/ld.so.preload

And… that actually worked!

Now you have a fully functional customized ffmpeg build. Congratulations.

If you want to know more about the latest features and examples of ffmpeg filters, please check out the libavfilter documentation.

Don’t hide Unity launcher bar

I’ve been messing around with Ubuntu  11.10 for a couple of weeks and it’s being hard getting used to Unity. One of the things that annoys me the most is when the launcher bar appears suddenly before I can click on a link at the left side of a web page. Nevertheless, the launcher bar can be stuck to the left edge of the screen following these steps that you all might already know:

  • Start the CompizConfig Settings Manager.
  • Select  Ubuntu Unity Plugin under Desktop category.
  • On the tab Behaviour select the value Never for the Hide Launcher property.
Categories: Linux

Linux, MX Revolution and the Middle mouse button

2011/10/24 5 comments

For those who have a MX Revolution mouse and want to configure it correctly in Ubuntu, I recommend them to use a tool called revoco. It is not new and it seems a bit abandoned. Yet it’s the only tool that did exactly what I wanted.

By default, the wheel button of this mouse is set to change between the free spin mode and the click-to-click. However, I prefer it to be in auto spin mode and use the wheel button as the middle mouse button, so that I can paste selected text in Linux and close tabs.

First of all, you must set your mouse (and keyboard if you own a MX 5500 combo) as raw HID. Edit the file /lib/udev/rules.d/62-bluez-hid2hci.rules (the file name may change depending on the system, this is for Ubuntu 11.10) and replace the following text:

KERNEL==”hiddev*”, ATTRS{idVendor}==”046d”, ATTRS{idProduct}==”c70[345abce]|c71[34bc]”, \
RUN+=”hid2hci –method=logitech-hid –devpath=%p”

by

KERNEL==”hidraw*”, ATTRS{idVendor}==”046d”, ATTRS{idProduct}==”c70[345abce]|c71[34bc]”, \
RUN+=”hid2hci –method=logitech-hid –devpath=%p”

Download revoco’s last version from http://maemo.cloud-7.de/mx-revolution/. Untar it, compile it running make and move the compiled binary to /user/local/bin directory.

cd revoco-0.5
make
mv revoco /usr/local/bin

Finally, run the command to set your mouse in auto spin mode and you’ll have both, the desired scroll wheel mode and the correct middle button configuration.

revoco auto=[speed]

Set the speed you prefer to change between free spin and click-to-click mode. I think 10 is the best value.

Categories: Linux Tags: ,

Read .gz text files on the fly

Logging systems usually compress old logs into Gzip files. Uncompressing those files to check old logs can be a pain in the neck, specially if you don’t know which file contains the entries of a specific date. The zless command will help you a lot with this task. For example, to read Tomcat’s compressed logs you just have to execute:

zless catalina.out.1.gz

You will see the content of the compressed file as if you were using the less command.

Categories: Commands, Linux Tags: , , , ,

Shutdown your computer, cleaners are lurking

It’s late and you are at work. Is time to take the bus and go home, but your computer is processing a huge amount of ultra-confidential data (of course, this only can be done on Linux :P). You’ll miss the bus if you don’t run now, but you don’t want to shut you computer down and lose a lot of time repeating all the process tomorrow.

The problem is that if you don’t shut down the computer, the cleaners, that are coming at 22:00 to clean the office armed with their brooms and cloths, could access all the data in your computer (as we all know, cleaners are actually high level hackers). So, what can you do?

Don’t worry, just run the next command and your PC will shut down one minute before they come into your office:

shutdown -h 21:59

If you know how long it will take to finish the process (let’s say 60 minutes), run this command instead:

shutdown -h +60

Finally, if you are too paranoid, you can halt your computer immediately by running this one:

shutdown -h now

Hey! And don’t forget to lock the door!

Categories: Commands, Linux Tags: , ,