Google Answers Logo
View Question
Q: Linux shell commands to atomically repoint existing symlink ( No Answer,   2 Comments )
Subject: Linux shell commands to atomically repoint existing symlink
Category: Computers > Operating Systems
Asked by: glmcrs-ga
List Price: $10.00
Posted: 26 May 2005 18:27 PDT
Expires: 28 May 2005 11:22 PDT
Question ID: 526119
In order to update my website files without disturbing any users, I
need to atomically repoint an existing symbolic link from one folder to another. 

Let's say we have this in /www/vhosts:

lrwxrwxrwx    1 user     user           36 May 26 08:12 ->
drwxrwxr-x   10 user     user         4096 Dec 30 01:37 folder_a
drwxrwxr-x   10 user     user         4096 Dec 30 01:37 folder_b

I need the proper unix shell command(s) to ATOMICALLY repoint the symlink from folder_a to folder_b.

I have tried to do it in one step with:

ln -nsf  /www/vhosts/folder_b /www/vhosts/ 

BUT the -f flag, which forces unlink of the destination if it exists,
seems to be breaking the atomicity of the process.
I have seen several errors generated at the exact time the ln takes
place which leads me to believe that
there is a fraction of a second when the symbolic link does
not exist -- after it has been removed and before the
new symbolic link has been created.

I need an atomic solution to this problem.  I have unsuccessfully
tried a several step solution using the mv and rename
commands with no luck. 

With the mv command, I created a temporary symlink pointing to
folder_b and tried to do:

mv /www/vhosts/ /www/vhosts/

BUT this resulted in moving the temporary symlink inside the
/www/vhosts/folder_a (which is where the symlink points)
instead of overwriting the symlink.

The attempt at renaming a temporary symlink over an existing symlink
did not work and resulted in no change in the directory.

Please help!  This has to be possible and should be really simple but
I have not been able to figure it out.

Thanks in advance.
There is no answer at this time.

Subject: Re: Linux shell commands to atomically repoint existing symlink
From: bozo99-ga on 27 May 2005 15:30 PDT
Running "ln -nsf" under strace indeed shows the sequence of calls with
small gaps between
 symlink("bbb", "mysymlink")             = -1 EEXIST (File exists)
 unlink("mysymlink")                     = 0
 symlink("bbb", "mysymlink")             = 0

Does it need to be done from shell and not perl ?   Then you could use
a rename(2) system call rather than a rename(1) command.
In Perl:
    symlink("bbb", "mynewsymlink");         # new temp link not yet in use
    rename("mynewsymlink", "mysymlink");    # atomic
Subject: Re: Linux shell commands to atomically repoint existing symlink
From: glmcrs-ga on 27 May 2005 18:19 PDT
Perl could possibly be used, as a last resort scenario though.

Any idea if PHP's rename() function work in the same manner as Perl's?

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  

Google Home - Answers FAQ - Terms of Service - Privacy Policy