# Anyone have a good method for notifying someone if segments stop building?

**URL:** <https://forum.mautic.org/t/anyone-have-a-good-method-for-notifying-someone-if-segments-stop-building/25086>\
**Category:** Product Support\
**Created:** [August 17, 2022, 11:58pm UTC](https://forum.mautic.org/t/anyone-have-a-good-method-for-notifying-someone-if-segments-stop-building/25086 "2022-08-17T23:58:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steverobinson](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/steverobinson/32/174_2.png) [@steverobinson](https://forum.mautic.org/u/steverobinson)\
**Post date:** [August 17, 2022, 11:58pm UTC](https://forum.mautic.org/t/anyone-have-a-good-method-for-notifying-someone-if-segments-stop-building/25086/1 "2022-08-17T23:58:25Z")

</div>

**Your software**  
My Mautic version is: 4.X  
My PHP version is: 7.4  
My Database type and version is: N/A

We have been experiencing [issues with segment filters losing values](https://forum.mautic.org/t/segment-filters-losing-values/25002) after minor point upgrades. This stops the cron job in its tracks and only some of our segments get updated.

What are you doing to proactively notify someone when this (or other Mautic errors) happen? Is there some magic you can add to a cron command to notify an admin when it errors?

I love that out of date segments get a red exclamation mark now, but I’d rather not have to wait until a member of our team logs in and happens to navigate to the segment tab to know that some of our segments aren’t updating.

---

<div class="post-metadata">

**Author:** ![ekke](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/ekke/32/296_2.png) [@ekke](https://forum.mautic.org/u/ekke)\
**Post date:** [August 18, 2022, 7:33am UTC](https://forum.mautic.org/t/anyone-have-a-good-method-for-notifying-someone-if-segments-stop-building/25086/2 "2022-08-18T07:33:01Z")

</div>

Hi Steve,  
basically we take all cron job output, filter all “known good”, e.g.

```auto
<console command> |egrep -v "^$|contact\(s\)|Triggering scheduled events|Triggering events|total event|<further known good>" 

```

and send the rest to some monitored email address.  
Add in crontab, above your actual jobs:

```auto
MAILTO=<desired email address>

```

If you have logfile monitoring you may as well send output to syslog through

```auto
`|tr '\n' ' ' |logger -t mautic_cron`

```

Trouble is that our commands do not have nice structured return codes, properly use STDERR or other things (that I know of)…

Greets  
Ekke

---

<div class="post-metadata">

**Author:** ![silavapi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/silavapi/32/7424_2.png) [@silavapi](https://forum.mautic.org/u/silavapi)\
**Post date:** [August 18, 2022, 11:02am UTC](https://forum.mautic.org/t/anyone-have-a-good-method-for-notifying-someone-if-segments-stop-building/25086/3 "2022-08-18T11:02:09Z")

</div>

I guess we could notify admins like we do if [Webhooks start to fail](https://github.com/mautic/mautic/pull/8782)?

---

<div class="post-metadata">

**Author:** ![sfahrenkrog1](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/sfahrenkrog1/32/4583_2.png) [@sfahrenkrog1](https://forum.mautic.org/u/sfahrenkrog1)\
**Post date:** [August 19, 2022, 11:28am UTC](https://forum.mautic.org/t/anyone-have-a-good-method-for-notifying-someone-if-segments-stop-building/25086/4 "2022-08-19T11:28:01Z")

</div>

Hey Steve

You could do it with a bash script like this:

```auto
#!/bin/bash
set -xu

MAUTIC_DOCROOT=$(realpath $(pwd)/../public_html/)
PHP_BIN="/usr/bin/php"
MAUTIC_CLI="$PHP_BIN $MAUTIC_DOCROOT/bin/console"

# change to mautic docroot
cd $MAUTIC_DOCROOT

# get db credentials
HOST=$(grep db_host app/config/local.php |cut -d"'" -f4)
DATABASE=$(grep db_name app/config/local.php |cut -d"'" -f4)
USER=$(grep db_user app/config/local.php |cut -d"'" -f4)
PASSWORD=$(grep db_password app/config/local.php |cut -d"'" -f4)

function trap_error {

   exit_code=$?
    if [$exit_code -ne 0]; then
        echo "Error during segment command ... "
        
        # HERE YOU CAN CALL A COMMAND TO NOTIFY YOU 
        # dshofdhf
    fi

}
trap trap_error EXIT

while read -a row
do   

   # call the segment rebuild per id and timeout after 5,5 hours 
   /usr/bin/timeout --preserve-status 20000 $MAUTIC_CLI mautic:segments:update --batch-limit=200 --list-id=${row} -q -n

done < <(echo 'select id from lead_lists where is_published=1' | mysql --host=${HOST} --user=${USER} --password=${PASSWORD} --batch -s --database=$DATABASE)

```

(the script expects to be in a dir scripts and goes one level up and change into public\_html. replace MAUTIC\_DOCROOT with your mautic document root)

Script explanation:

- We change into the mautic document root and get the db credentials from the mautic config

- We then use the db credentials to get every published segment and rebuild it

- We use the timeout command to prevent an endless running command. We preserve the status of the command to get the exit code.

( See: [https://linuxize.com/post/timeout-command-in-linux/](https://linuxize.com/post/timeout-command-in-linux/) )

- In case of problems the function trap\_error get called. You could put any command here to send a notitfication to you.

Other Benefits of this script:  
The other segments are still rebuild even if one of the segment rebuilds fails!

Regarding the notfication in the bash script:

We regulary use this service to monitor cron jobs (you could also use the open source version to self host it)

> **[Healthchecks.io Documentation - Healthchecks.io](https://healthchecks.io/docs/)**
>
> Heartbeat monitoring for cron jobs, scheduled tasks, periodic processes. Guides, API reference, usage examples, self-hosting instructions.

Please see how to use them with bash scripts:

> **[Monitor Shell Scripts with Healthchecks.io](https://healthchecks.io/docs/bash/)**
>
> You can easily add Healthchecks.io monitoring to any shell script. Edit the script to send a heartbeat signal when it completes. Code examples using curl.

With [https://healthchecks.io/](https://healthchecks.io/) you would simply send the notification abount the problem like this

# Notify [Healthchecks.io](http://Healthchecks.io) about the error

curl -m 10 --retry 5 [https://hc-ping.com/your-uuid-here/fail](https://hc-ping.com/your-uuid-here/fail)

and configure in [https://healthchecks.io/](https://healthchecks.io/) the notification method (we use signal and email)

Greetings  
Sebastian
