Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
Cron script to restart memcached not working

I have a script in cron to check memcached and restart it if it's not working. For some reason it's not functioning. Script, with permissions:

-rwxr-xr-x  1 root     root       151 Aug 28 22:43 check_memcached.sh

Crontab entry:

*/5 * * * *  /home/mysite/www/check_memcached.sh  1> /dev/null 2> /dev/null

Script contents:

#!/bin/sh

ps -eaf | grep 11211 | grep memcached
if [ $? -ne 0 ]; then
        service memcached restart
else
        echo "eq 0 - memcache running - do nothing"
fi

It works fine if I run it from the command line but last night memcached crashed and it was not restarted from cron. I can see cron is running it every 5 minutes.

What am I doing wrong?

Do I need to use the following instead of service memcached restart?

/etc/init.d/memcached restart

I have another script that checks to make sure my lighttpd instance is running and it works fine. It works a little differently to verify it's running but is using the init.d call to restart things.

Edit - Resolution: Using /etc/init.d/memcached restart solved this problem.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

What usually causes crontab problems is command paths. In the command line, the paths to commands are already there, but in cron they're often not. If this is your issue, you can solve it by adding the following line into the top of your crontab:

 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

This will give cron explicit paths to look through to find the commands your script runs.

Also, your shebang in your script is wrong. It needs to be:

#!/bin/bash
over 4 years ago · Santiago Trujillo Relatório

0

I suspect the problem is with the grep 11211 - it's not clear the meaning of the number, and that grep may not be matching the desired process.

I think you need to log the actions of this script - then you see what's actually happening.

#!/bin/bash

exec >> /tmp/cronjob.log 2>&1
set -xv

cat2 () { tee -a /dev/stderr; }

ps -ef | cat2 | grep 11211 | grep memcached
if [ $? -ne 0 ]; then
        service memcached restart
else
        echo "eq 0 - memcache running - do nothing"
fi

exit 0

The set -xv output is captured to a log file in /tmp. The cat2 will copy the stdin to the log file, so you can see what grep is acting upon.

over 4 years ago · Santiago Trujillo Relatório

0

Save below code as check_memcached.sh

#!/bin/bash

MEMCACHED_STATUS=`systemctl is-active memcached.service`
if [[ ${MEMCACHED_STATUS} == 'active' ]]; then
    echo " Service running.... so exiting "  
    exit 1
else
    service memcached restart
fi

And you can schedule it as cron.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda