linuxit

The linux way to do things

Wait until an jquery ajax request is completed before continuing

Sometimes, you need an ajax request to be completed to allow an application to continue as the rest of the process depends on it.  An example of this could be for instance when you are updating via ajax some database entries upon an user’s action.  Before allowing the web application to continue, you want to be sure that the row has been properly updated.

The typical way to achieve this is to add a delay using setTimeout or some obscure function (see http://stackoverflow.com/questions/1183872/put-a-delay-in-javascript) but of course, this method has a major drawback: you can never be absolutely sure that the delay will be long enough to let the operation finish.

A better way would be to simply specify that we want our ajax request to be sync instead of async (the default) by using {async: false} in our request :

$.ajax({
   type: "POST",
   async: false,
   url: "/ajax/newMemberGroup.php",
   data:  'name=' + $("#newMemberGroupInput").val() +'&selectOption=' + $("#memberGroup").html()
   }).done(function( msg ) {
   $("#memberGroup").html(msg);
});

That way, the rest of the code will only be executed once the ajax request is completed.  The only drawback of this method is that if your ajax request takes a long time, the browser can be locked for a long time (since we are waiting for our ajax request to be finished before continuing) but for a majority of cases where all you need is a quick  but crucial, operation, this method will do it!


Samba: permission denied when mounting a share from linux

Recently, I’ve been facing a weird issue with my samba server.  I tried mounting a share as I always did using :

[hudony@fedh mnt]$ sudo mount -t cifs -o username=raall //10.11.1.15/commun /mnt/commun/
Password for raall@//10.11.1.15/commun:  ********

and for some obscure reasons, I kept getting the following error message:

mount error(13): Permission denied
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

However, the issue was not affecting windows and mac workstations.

Since the samba log files were not helpful about my issue, I searched and searched again the web for a solution and I finally got the solution while looking at some forum posts.  It seems that a recent change in the linux kernel has been released involving that from now on, the default authentication for cifs mounts needs to be at least ntlmv2 security.

The solution is to simply add the security parameter to the mount command like this:

[hudony@fedh mnt]$ sudo mount -t cifs -o username=raall,sec=ntlmv2 //10.11.1.15/commun /mnt/commun/

That’s it, problem solved!


Organize your pictures according to the date they were taken

When it comes to manage my pictures library, I must admit I am very undisciplined. I usually let pass many months between the time I take a picture and the time I transfer it to my computer so it is very hard to organize them by the date they were taken. Moreover, I am sometimes in a hurry so I just transfer them in a “temporary” place telling myself that I will organize them later (sure…).

In short, for years, my pictures library has been a total mess. Lately, I wanted to see my child evolution through years and months to finally quickly realize that my neglicence has made this simple task now impossible. In the end, all I wanted was to organize them by the date they were taken, not by the event they were attached to.

I recently solved my problem by writing a short bash script that does exactly that. First the script scans a source folder looking for jpg or jpeg files. Now for each file found, the script will look at his EXIF information (if it is not possible, the creation date of the file will then be used) to determine the date it was taken and copy it in a destination folder in an organized way using a Year, Month and Date format.  Files with the same name will be automatically renamed and you can choose to copy (default) or  instead move the pictures found.

The basic usage is the following one:

[hudony@fedh ~]$ ./organizePictures.sh -h
Usage: ./organizePictures.sh [-moqh] SOURCE DESTINATION
-m Use move instead of copy
-o Overwrite duplicate files instead of renaming them
-q Quiet, do not display informations on the console
-h This help message

Continue reading “Organize your pictures according to the date they were taken” »


Quickly analyze multiple log files

If just like me, you are using logrotate to manage your logs, you are probably also using its “compress” options (if not, then you should) which gzip (by default) the log after rotating it in order to save up some space.  Recently, a customer called me complaining that he didn’t receive an email sent to him few days ago.  I then proceeded to ask him which exact day the email was sent but he wasn’t sure since the sender had emptied its “sent items” list (???).

In short, I had to find it through all my postfix log files (we keep a week long and daily log files are compressed).  The usual way would be to unzip and analyze each log file until we find what we are looking for.  Doing that for 7 files isn’t that bad but what If we had 30 files to analyse, there must be a better way…

Indeed, to quickly solve this issue, you can use a for loop and the cat command :

[hudony@smtp log]# for i in $(ls maillog*.gz);do gunzip -c $i >> /tmp/logfile; done

The result is only one log file (which could be very big so be careful) containing all the useful data.


Alert users when they are about to exceed their quota using Courier-IMAP

I recently built a Postfix/Courier-IMAP server with virtual domains that uses maildirquota to enforce quotas limitation.  Migrating from a H-Sphere setup, I couldn’t ignore the fact that despite all its flaws, it had a very useful built-in feature: it could send an email to a user when this one was about to exceed its quota.

Since the machine was now offline, meaning I couldn’t find anymore the script executing this task, I decided, after quickly looking on the internet for one, to write my own.  Here it is in Bash :

Note that this script uses mutt since I am usually sending HTML emails.  Should you be fine with text emails, you can use the mail command.

#!/bin/bash

ratio=90

for dir in /var/mail/vhosts/*
do
    for mailbox in $dir/*
    do
        rawQuotaSize=$(head -n1 $mailbox/maildirsize 2>/dev/null)
        if [ ! -z ${#rawQuotaSize} ]; then
            quotaSize=${rawQuotaSize%?}
            alertQuotaSize=$(($quotaSize * $ratio / 100))
            mailBoxSize=$(du -sb $mailbox | cut -d"/" -f1)
            if [ $mailBoxSize -gt $alertQuotaSize ]; then
                email=$(echo "$mailbox" | cut -d/ -f6)@$(echo "$mailbox" | cut -d/ -f5)
                echo "Optional email body" | mutt -s "You are now at $ratio% of your
 allowed storage capacity" -e "set content_type=text/html" 
-e "set realname = \"donotreply\"" $email
            fi
        fi
    done
done

The script is self-explanatory: it loops through each mailbox of each virtual domain and for each, get the mailbox’s quota.  If a quota is set, it then check if the current size of mailbox is greater than the ratio * the mailbox quota.  If so, an email is sent.

Download quotaAlert.sh