Sing me a lullaby lappy!

It is often the case you want to listen to some music before you drift away into deep slumber. Well, in order to do that, if you’re using your lap or phone, you’ll have to manually stop the music after a while. And that will be either at the time a nice sleep is at its verge or waking up in the middle of your sleep. But that’s not how a lullaby works, is it? Nope. A lullaby gets us to sleep, continues a little while after we are asleep and then stops. We are going to see how we’re going to do that with our computer.

Its really simple. As usual, I’m on my Ubuntu 10.04 while doing this and I’m assuming we are using totem movie player in the rest of this post.

We have already started the movie player and it is playing a list of our favourite musics. You have given the ‘repeat’ option and you’re ready for bed.

Since everything running on your computer is a process, totem too is a process and it will have its own unique process id. We need to get that first. We use the ps command for that which gives a ‘snapshot of the current processes’. We give an option -e to it in order to select and display all the processes and from that list, we grab (grep) totem by the collar. So this is how it will look:

haris@asylum:~$ ps -e | grep totem
 7315 ?        00:00:30 totem
haris@asylum:~$

That number to the left is totem’s process id. Now let’s say you want your music to last for 45 minutes assuming you will sleep within half an hour or so.

Run date.

haris@asylum:~$ date
Sun Oct  2 06:43:21 IST 2011
haris@asylum:~$

It is 6:43 now. Let’s say I want the music played till 7:30 and then totem should stop. There is a cool command called at. Here is a demonstration:

haris@asylum:~$ at 0730
warning: commands will be executed using /bin/sh
at> kill 7315
at> <EOT>
job 8 at Sun Oct  2 07:30:00 2011
haris@asylum:~$

That’s it. Now I can lie down to sleep and at 7:30 sharp, totem will be killed. Note that <EOT> is not typed into the terminal. You must press ctrl-d for that.

This is most useful when you have a torrent running and you don’t want to have your laptop suspended or shutdown for the night.

Otherwise you can use the shutdown command itself to have your laptop shutdown or halted after a specified amount of time. Its the trick we use while giving my 3 year old niece food. We use the shutdown command to make the computer shutdown automatically after 20 or 30 minutes and when it does, we tell her, “Oh, its a power failure. We’ll watch cartoons tomorrow, okay?” and gets her away from the monitor.

The shutdown command is pretty simple. If you want your system to be shutdown in say, 30 minutes, you do:

haris@asylum:~$ sudo shutdown -P +30
[sudo] password for haris:

Broadcast message from haris@asylum
    (/dev/pts/2) at 6:55 …

The system is going down for power off in 30 minutes!

You can hit ctrl-c to quit the order. The -P option is to power off. There are several other options. Refer the man page of shutdown for them.

Check it out.

Advertisements

Having fun with gcc.

I actually stumbled upon this idea quite a awhile ago along with my friend Sunil. I don’t exactly remember what we were trying to accomplish back then but here is what I got from the little hacking time we had.

This is about how you can mimic commands. You use the commands cd, ls, cp, etc, right? What if you want something of your own like that? For example your name as a command? Ha! Let’s do that itself.

I did this on my Ubuntu 10.04 distro and I have the gstreamer plugins for mp3  files downloaded and installed.

Let’s assume that you have a song called rockthee.mp3 in your /home/<yourname>/music/ directory.

Now what we are going to do is this. We are going to manipulate a few things such that when you type in your name and hit enter, totem movie player will open up and the song rockthee.mp3 will start playing. And we are going to use the system() command to accomplish this.

For this, go to any directory you wish and open up a text editor. Type in the following code there:

#include<stdio.h>
int main()
{
system("totem ~/music/rockthee.mp3 &");
return 1;
}

Now save it as filename.c and quit the editor.

Going back to the terminal, compile the code as follows:

gcc filename.c -o <your name>

(Replace “<your name>” with your name)

In my case, it would be

gcc filename.c -o haris

Now if you do an ls, you will see that your name is there. Typing ./haris will open up totem and play rockthee.mp3. But that’s not how we want it to be, is it? I mean this is just like executing a C program from the folder it is written in! We shouldn’t use that ‘./’. So what do we do?

Ah ha! We have two options. Either (1) add the path of the current folder (got by executing the command ‘pwd’) to the PATH variable or (2) simply copy the file having your name into any one of the locations given by $PATH.

We will explain both here. But first, try executing ‘$PATH’ in your shell (terminal) and see what comes. Those are the directories which will be searched for when you type in a command at the prompt and hit enter. If you try

ls /bin

you can see many familiar commands including ‘ls’ itself. So when we type ‘ls’ and hit enter, our system searches all the directories specified in the PATH variable and only if it finds ‘ls’ in any one of those locations will ‘ls’ be executed.

So let’s try method (2) first. It is simpler. Run the following:

sudo cp <your name> /bin/

You’re done. Now close the terminal, open up a new one and try simply typing in your name and hitting enter. Viola! There comes Totem with your song! Now to remove what you copied, do

sudo rm /bin/<your name>

Now let’s go to method (1). In order to permanently include the path of your current folder in the PATH variable, you have to edit a file called “.bashrc” in your home folder. But first execute ‘pwd’ and remember the output. Now open up .bashrc using any text editor.

gedit ~/.bashrc

And add the following two lines to the end of that file.

PATH=$PATH:<output of executing ‘pwd’ earlier>
export PATH

In my case it would be

PATH=$PATH:/home/haris/music/ (assuming I wrote and compiled filename.c in the ‘music’ folder itself)
export PATH

Now save the file and close it. Close your current shell (terminal) and open a new one. Type in your name and hit enter and see the wonder works! Also now try executing ‘$PATH’ and you will see that a new path has been added to it.

You can make it a bit more interesting if you add a few printf statements in that C program before the system command saying “This software is created by haris and its version is 0.xx” or something. You can show your non-linux user friends and mess with them.

You can try system command for many things. Here are a few examples:

system(“firefox gmail.com &”);

system(“evince <path>/filename.pdf &”);

system(“gedit <path>/filename.c &”);

Try playing with it and I’m sure you will get more and more ideas.

All the very best!

Firefox download getting stuck. ‘wget’ to the rescue!

The firefox download manager has been giving me quite a headache whenever I try to do big downloads. Even if it is a 70MB download, after 25 or so MB, it gets stuck. Then I have to pause and hit continue for the download to proceed again. It so happens that many times an error occurs and I can’t continue the download from where it got stuck and I have to download the whole thing again… and again… and again…

Ha! But I’m not to be trifled with. Not as long as ‘wget’ is there! ‘wget’ is a GNU free software package that is used for retrieving files over the web. If you have got it installed, then all you have to do is to get the download link and use wget to download it.

In order to get the download link, right click on whichever download you want in the firefox download manager. There will be an option ‘copy download link’. Click on it.

Then open a terminal and go to whichever folder you want the file downloaded to. Assuming it is in the Desktop, type the following command at the prompt:

haris@asylum:~/Desktop$ wget <paste the download link>

Now even if it gets stuck in the middle, you can resume from wherever it was interrupted by giving the option ‘-c’ to ‘wget’. That is, the command will be like

haris@asylum:~/Desktop$ wget -c <paste the download link>

And there it continues beautifully.

32 or 64 bit? The magic of pointers.

Thinking and talking about the issue of whether my OS is 32-bit or a 64-bit one, I found out there were many ways to know that. But we discuss none of them over here. Here we discuss a cool way in order to find it out.

We all know about pointers in C right? It points to a memory location. That is it holds an address whose contents maybe dereferenced using the ‘*’ operator. There I already said it! The pointer stores an address.

But what is the size of an address? And who determines it?

That is where the matter of 32 and 64 bit comes in. A 32-bit OS means that its addresses are of size 32-bits and it can address upto 2^32 memory locations. As such, a maximum of 4GB. In a previous post of mine, I have mentioned the fact of the file system FAT32 not supporting files larger than 4GB. It is because that file system uses 32-bit addressing.

But our concern is with the OS. The OS determines the addressing and as such, a 32-bit OS allocates 32 bits to a pointer whereas a 64-bit OS allocates 64 bits to a pointer.

We can exploit this allocation to find out if the OS running on our system is 32 or 64 bit. For that, we write a small C code as follows:

#include<stdio.h>

int main(){

     int *p;

     printf(“Size is : %d\n”, sizeof(p));

     return 0;

}

Compile the code using gcc as:

gcc filename.c

And run it as:

./a.out

See what number you get as your output. If it is 4, your OS is 32-bit and if it 8, your OS is 64-bit. This is because the 4 and 8 are shown in bytes.

I was hoping to get a way of knowing what my system’s architecture is. But this method can only recognize the OS and not the system.

Happy hacking!

Disk drive with UUID = # not yet ready or present. (Hibernation option missing!)

I’ve been greeted with that error message for quite a long time now on my Ubuntu loading screen and it was today that I finally fixed it.

I do not know the entire technical details of what all is going on, but I just want to share what I did so that it may help someone out there.

This error came the day when I installed Debian on a primary partition. My Hard disk right now is as follows:

I installed Debian by partitioning the third primary partition into two ( 7 and 7), thus having a total of four primary partitions. It worked fine. But thereafter whenever I booted into Ubuntu, I got the aforementioned error message. Not only that, but the hibernation option was missing too. Even though I copied the Debian partition back into the extended partition at /dev/sda10 (see figure) using Gparted, the problem still persisted.

I figured out it had something to do with my fstab file. You can get yours at /etc/fstab. In this file, along with the partitions present for /, /home and /var (see the figure) , it showed the same UUID that was in the error message with a comment saying that ‘swap was initially on /dev/sda8 at the time of installation’.

Heck it was still there. So somehow its UUID had got twisted. How that happened, I have no clue.

So here is what I did.

I have 11.04 installed side by side. So I mounted that and accessed its fstab file. There it had the swap partition defined well as follows:

UUID=31b658b3-4a87-454f-b514-6dd5d647a159 none            swap    sw              0       0

I copied that into the fstab file of my ubuntu 10.04 replacing the error UUID. Saved it and there was no more error message the next time I booted. Plus, the hibernation option came back. (NOTE: In order to edit the fstab file, you need to open it as root. So do sudo gedit fstab)

So all you have to do is to replace the UUID. To find out the UUID of a partition you can do:

sudo blkid /dev/sda#

and you will get that partition’s UUID. Gparted can also provide you with this information.

Hope that helps.

Recovering Grub after installing Windows, using Ubuntu.

It so happened that after installing windows on my system (which already had linux, ubuntu, installed on it), the grub menu that used to come at the beginning stopped appearing. Instead, it started to boot directly into windows.

In order to solve this problem, I did the following. Of course, I found a lot of answers to this problem on the net, but what I did worked for me fine. But be sure to sit patiently and correct it in case something does not work fine. Never give up!

(Note: I also do believe that the following works only when the ‘vmlinuz’ and ‘initrd.img’ files are at the root directory itself. Check this in step 5. In step 5, ‘vmlinuz’ and ‘initrd.img’ should be there in the ‘temp’ folder we created.)

1. Boot using a live cd of ubuntu.

2. Open a terminal and run the command

sudo fdisk -l

It lists the complete partition table of the hard disk. In there, identify which partition you have got your linux installed on. You can identify it using the drive size you had allocated for it and looking at the last column of the output which will be ‘extended’ for all of your linux partitions. The partition will most probably be something like /dev/sda5 or something. Remember this partition.

3. Create a temporary folder in your home directory (Note: You can make the temporary folder anywhere you want. I’m using the home folder just for the sake of explanation). I’m calling it ‘temp’ for now. So that ‘temp’ folder’s path will be /home/ubuntu/temp.

4. Mount your linux partition there. That is, assuming that you found your linux partition to be /dev/sda5, you mount that at the ‘temp’ folder by doing the following command

sudo mount /dev/sda5 /home/ubuntu/temp

5. If you want to check whether you have mounted the correct partition, go to your home folder and open temp. You will be in the ‘/’ directory. In there you will find ‘home’, in which your home folder’s name will be there. Once you’ve confirmed you have mounted the correct partition, do step 6.

6. You have to install grub by showing the system where to read the data from the hard disk at the beginning. Don’t worry, just run the following command

sudo grub-install –root-directory=/home/ubuntu/temp /dev/sda

The ‘/dev/sda’ corresponds to your hard disk name. Replace it by whatever the command ‘sudo fdisk -l’ command showed you.

7. You’re done. You may restart your system.

You might want to check out how I upgraded to 12.04 too!

PS: For editing the grub menu that appears, check out my post

https://sosaysharis.wordpress.com/2011/04/29/editing-grub-menu-in-ubuntu-10-04-and-above/

Files larger than 4GB not supported on FAT32.

I accidentally found out that the FAT32 filesystem does not support files larger than 4GB. It happened when I was trying to unrar the files on a DVD into a .iso file. When it was about 92% done, the error window popped up saying “No space for files”.

But I had to make that .iso file. I had to play that game because it was the latest version of the game I loved since I was four years old. Street Fighter, the name of the game.

I found out that this problem is not there with the NTFS filesystem however. So I jumped back to my ubuntu, launched gparted (an ultra powerful application for disk management) and shrunk my 33GB windows partition into 25GB. The 7GB thus gained, I converted into NTFS.

Jumped back into windows again and happily made the .iso file on the NTFS partition. 🙂

PS: Please share your views so as to why FAT32 doesn’t support files larger than 4GB.