2

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.

63bus
  • 31
  • 7

3 Answers3

1

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
Skyler
  • 186
  • 1
  • 6
  • Thank you. You are correct - sbin is not in the path for cron so "service" doesn't work. http://stackoverflow.com/questions/8127433/unable-to-run-a-service-command-via-cron – 63bus Feb 17 '16 at 15:25
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.

dan4thewin
  • 1,134
  • 7
  • 10
  • I tried that - it spit out the "do nothing" text in the cronjob.log file since it's running.11211 is the port memcached runs on. The startup command to run memcached is memcached -d -p 11211 -u memcached -m 512 -c 1024 -P /var/run/memcached/memcached.pid so it will match that. – 63bus Feb 16 '16 at 18:34
  • Why don't you kill it manually, and see if the cron job restarts it? – dan4thewin Feb 16 '16 at 18:48
  • I had to wait until a low time on my site but it works now using /etc/init.d/memcached restart The message I got using service was: /home/mysite/www/check_memcached.sh: line 10: service: command not found – 63bus Feb 17 '16 at 06:41
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.

BSB
  • 2,270
  • 17
  • 26