Skip to content

Instantly share code, notes, and snippets.

@fbrnc
Last active August 29, 2015 14:00
Show Gist options
  • Save fbrnc/11296418 to your computer and use it in GitHub Desktop.
Save fbrnc/11296418 to your computer and use it in GitHub Desktop.
Move given directories to shared memory and create symlinks
#!/bin/bash
for i in "$@" ; do
if [ -h $i ] ; then
echo "$i is a symlink";
SYMLINKTARGET=$(readlink -f "$i")
unlink "$i" || { echo "Error unlinking $i"; exit 1; }
if [ -d $SYMLINKTARGET ] ; then
echo "Moving $SYMLINKTARGET to $i"
mv $SYMLINKTARGET $i
else
echo "Creating new empty directory $i"
mkdir "$i" || { echo "Error creating new directory $i"; exit 1; }
fi
fi
if [ ! -d $i ] ; then echo "Invalid directory $i"; exit 1; fi
NORMALIZEDDIR=`cd "$i";pwd`
TARGETDIR=${NORMALIZEDDIR//[\/-+=.,]/_}
if [ -d /dev/shm/$TARGETDIR ] ; then
echo "Directory /dev/shm/$TARGETDIR already exists. Deleting it";
rm -rf "/dev/shm/$TARGETDIR" || { echo "Error while deleting target dir /dev/shm/$TARGETDIR"; exit 1; }
fi
echo "Moving $i to /dev/shm/$TARGETDIR"
mv $i /dev/shm/$TARGETDIR
echo "Creating symlink from $i to /dev/shm/$TARGETDIR"
ln -s /dev/shm/$TARGETDIR $i
echo "Fixing permissions"
chown -R vagrant:www-data /dev/shm/$TARGETDIR
chmod -R ug+rw /dev/shm/$TARGETDIR
done
@fbrnc
Copy link
Author

fbrnc commented Aug 20, 2014

Usage: ./shared_memory.sh /var/www/magento/htdocs/var/{cache,session}

@alankent
Copy link

Any reason you did not move all of /var to /dev/shm? (Was /var/generated too big?)
Any reason you did not move it to /tmp? (Other than potential performance benefit?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment