This guide works for Arch Linux but it should apply to all distributions that use Pacman.
When a new list of mirrors is available, it is placed in the /etc/pacman.d/mirrorlist.pacnew file. In this file all of the server URLs are commented by default.
The mirrorlist.pacnew file is not considered by Pacman. Only the mirrorlist is. The thing to do is to uncomment the URLs in the new file and to run the rankmirrors script that will sort out the fastest n mirrors.
For doing this I created a simple script. You can place this under /root/rank_new_pacman_mirrors.sh.
#!/bin/bash # Let's make a copy of the pacnew file, because we do not want to loose it. cp /etc/pacman.d/mirrorlist.pacnew /etc/pacman.d/mirrorlist.backup; # Remove the comments from the server URLs. sed -i 's/^#Server/Server/' /etc/pacman.d/mirrorlist.backup;1 # Use rankmirrors to calculate the 10 fastest mirrors. You can set as many as you like. rankmirrors -n 10 /etc/pacman.d/mirrorlist.backup > /etc/pacman.d/mirrorlist;
It is very simple and inline comments should be enough to explain it. Don't forget to make it executable.
sudo chmod +x /root/rank_new_pacman_mirrors.sh
OK, so now, every time the mirrorlist.pacnew file is updated, you have to run this script. Manually.
What if this process would be automatic? We can achieve it using incron, which is a inotify event processor.
First of all, install incron. For Arch Linux users: it is in the official repositories so you can simple install it with
sudo pacman -S incron
Now we have to add an entry in the table, telling incron which file we want to monitor and which script we want to execute when the file is, in this case, edited.
For doing this, execute the following commands.
su export EDITOR=/usr/bin/nano incrontab -e
The export is used to set a temporary global EDITOR variable. It's a good practice to set it one for all.
OK, now we have to enter the information for the monitor.
/etc/pacman.d/mirrorlist.pacnew IN_CLOSE_WRITE /root/rank_new_pacman_mirrors.sh
I personally prefer to introduce a lock in the script to avoid it to run more than once simultaneously, causing undesiderable behaviours with your Pacman. Let's modify /root/rank_new_pacman_mirrors.sh.
#!/bin/bash # Thi is the lock file. lockFile="/tmp/calculate_best_mirrors.lock"; # We execute the following code only if the lock file doesn't exist. if [ ! -f "$lockFile" ]; then # Create the lock file. touch "$lockFile"; cp /etc/pacman.d/mirrorlist.pacnew /etc/pacman.d/mirrorlist.backup; sed -i 's/^#Server/Server/' /etc/pacman.d/mirrorlist.backup; rankmirrors -n 10 /etc/pacman.d/mirrorlist.backup > /etc/pacman.d/mirrorlist; # Remove the lock file once the process is completed. rm "$lockFile"; fi;
OK, now you just have to start the incron daemon and to enable it to make it start at boot.
sudo systemctl start incrond sudo systemctl enable incrond
To test it, we can simple touch the last mirrorlist.pacnew file and check if the script is executed by incron.
sudo touch /etc/pacman.d/mirrorlist.pacnew sudo ps ax | grep rank_new_pacman_mirrors
This would show you one process running and when it will finish then you will have and updated and ranked mirrorlist file for Pacman.