0

OS: Raspbian Lite Kernel version:4.19

I am using a script to monitor a service, so that it would start the service if it goes down. I have added it in crontab and it does a great job. The only problem is that it sends its output to /var/mail/pi : You have new mail in /var/mail/pi.

I am afraid that the file will grow too large or that at some point in time it will stop working because of this.

I have found the script online and I don't know what to modify so that it won't send mails:

# vi /var/www/html/service_monitor.sh

#!/bin/bash

serv=DisplayM

sstat=dead

systemctl status $serv | grep -i 'running\|dead' | awk '{print $3}' | sed 's/[()]//g' | while read output;

do

echo $output

if [ "$output" == "$sstat" ]; then

    sudo systemctl start $serv

    echo "$serv service is now UP !" | echo "$serv service was DOWN. Restarting now on $(hostname)"

    else

    echo "$serv service is running"

    fi

done

These can act like some sort of logs, so I wouldn't mind keeping the /var/mail/pi file, but it would be nice do keep like, the last 100 entries and delete the others.

What do you think ?

1 Answers1

-1

If you're looking to stop CRON from creating emails, it can be done a few ways:

  • redirecting your output to dev/null: 0 5 * * * /example/script >/dev/null 2>&1
  • List item set the cron mailto to blank: MAILTO=""
tik27
  • 2,698
  • 1
  • 16
  • 25
  • Better yet, write the output to a log file and configure rotation for that file. You don't want to lose the ability to figure out what happened when something goes wrong. – tripleee Oct 04 '19 at 16:24
  • @tripleee How can I do that ? That is the point of my question. – GeorgicaFaraFrica Oct 15 '19 at 07:55
  • https://stackoverflow.com/a/46883504/874188 explains in more detail how to write to a log file and https://www.digitalocean.com/community/tutorials/how-to-manage-logfiles-with-logrotate-on-ubuntu-16-04 is a random google result for how to configure `logrotate`. Maybe see also https://raspberrypi.stackexchange.com/a/104617 – tripleee Oct 16 '19 at 09:00