I have vps-hosting with centos. On this server are Apache, PHP, MySql. I installed memcached. And sometimes (once in 3-4 days) memcached stopping - I don't know why, in log file missing necessary information.
I found that solution - every 10 minutes run cron, which verify started or not memcached (through command memcached-tool), when memcached already crashed - i send e-mail and through server command line start memv=cached
But i would start memcached automatic, but i can't do this.
I tried so command
system("/etc/init.d/memcached start");
exec("service memcached start");
system("service memcached start");
but i haven't result.
You should use sudo to make php escalate to root before killing and starting memcached or you'll get permission deined.
P.S. try to check why memcached crashes instead of restarting it!!
You would probably need to use sudo. The user running apache is not allowed to run the init script itself.
system("sudo /etc/init.d/memcached start");
Will probably have enable the apache user to use sudo. this is done via visudo command. http://www.gratisoft.us/sudo/visudo.man.html
But honestly better would be to use something like monit, to automate the 'watchdog' role. Its designed for it. http://mmonit.com/monit/
I want to just point out that it is much safer to create an actual cronjob for things like this.
Please read more on the exec()
function here: http://php.net/manual/en/function.exec.php
Additionally, if safe_mode
is on, you won't be able to execute commands outside of your safe_mode_exec_dir
. These are set in php.ini
or a custom php configuration.
As another answerer has noted, it would be better to isolate the cause of the crash instead of repeatedly starting it. What kind of errors are you getting when memcached crashes? Obviously you have some way of knowing it has crashed, otherwise you wouldn't be asking the question. The most obvious answer could be that the cache has filled up and your scripts are hitting the cache too hard/often for it to page out.