I had memcached running under CentOS 7 as a UNIX socket for about a year. Then, something like an out of memory was causing the memcached daemon to fail. This failure / restart caused the socket directory, /var/run/memcached , and the socket file /var/run/memcached/memcached.pid to be deleted.
Note, this info should apply to CentOS 7, Fedora 7, and any OS variety of Red Hat 7 should be roughly the same.
It turns out the /var/run directory, which is a symlink to /run, is a temporary file system and not persistent across reboots or failures in daemons.
The way systemd works is the directory at /etc/systemd/system/multi-user.target.wants has symlinks pointing to the appropriate xyz.services files.
Much like the old /etc/init.d paradigm, the new systemd services files will exist by default in /lib/systemd/system/, like /lib/systemd/system/memcached.service.
If you want to modify the default memcached.service file then copy the file over to the /etc/systemd/system/ directory and edit the new file. Do NOT directly edit the /lib/systemd/system/memcached.service or your changes will eventually be overwritten by an upgrade.
cp /usr/lib/systemd/system/memcached.service /etc/systemd/system/memcached.service nano /etc/systemd/system/memcached.service
In order to have the /var/run/memcached directory and .pid file created on every start or restart you'll need to explicitly create the directory and set permissions. Note, the .pid file will be created by the services itself, based on settings in /etc/sysconfig/memached
This assumes the memcached daemon / service will run under the user and group called memcached.
You'll add the following lines under the [Service] section.
User=memcached Group=memcached # Run ExecStartPre with root-permissions PermissionsStartOnly=true ExecStartPre=-/usr/bin/mkdir /var/run/memcached ExecStartPre=/usr/bin/chown -R memcached:memcached /var/run/memcached/
The complete /etc/systemd/system/memcached.service file will then look something like the following:
Description=Memcached Before=httpd.service After=network.target [Service] Type=simple EnvironmentFile=-/etc/sysconfig/memcached User=memcached Group=memcached # Run ExecStartPre with root-permissions PermissionsStartOnly=true ExecStartPre=-/usr/bin/mkdir /var/run/memcached ExecStartPre=/usr/bin/chown -R memcached:memcached /var/run/memcached/ ExecStart=/usr/bin/memcached -u $USER -p $PORT -m $CACHESIZE -c $MAXCONN $OPTIONS [Install] WantedBy=multi-user.target
Now, it's necessary to disable and re-enable the service so systemd points its symlink to your new /etc/systemd/system/memcached.service file
Do that as follows
systemctl disable memcached.service systemctl enable memcached.service
The command should respond with
Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /etc/systemd/system/memcached.service.
Running memcached as a socket, instead of on a TCP port, is another topic, but I'll provide some info on that too.
The socket setting is specified in the /etc/sysconfig/memcached file, which happens because the EnvironmentFile in the memcached.service above tells it to pull in extra options.
To the end of /etc/sysconfig/memcached, add something like:
Note, you may need to add users to the memcached Linux group for the 0770 permissions to work. I feel like I've had trouble getting 0770 to work, so while less secure, you may eventually need to loosened that value to 0777 if your web server isn't able to read and write to Memcached.
OPTIONS="-s /var/run/memcached/memcached.sock -a 0770"