Python script and emhttp notify via cron


Recommended Posts

I wrote a simple python script which checks the CPU temp and uses /usr/local/emhttp/webGui/scripts/notify to alert. From the command line it seems to run fine just not from the crontab. This is what I see.

 

Jun  4 12:06:01 clarabell crond[1731]: exit status 1 from user root /root/scripts/check_cpu_temp.py
Jun  4 12:06:01 clarabell sSMTP[27055]: Creating SSL connection to host
Jun  4 12:06:02 clarabell sSMTP[27055]: SSL connection using ECDHE-RSA-AES128-GCM-SHA256
Jun  4 12:06:05 clarabell sSMTP[27055]: Sent mail for [email protected] (221 2.0.0 closing connection h39-v6sm14129640edb.65 - gsmtp) uid=0 username=root outbytes=598

 

It appears /usr/local/emhttp/webGui/scripts/notify doesn't seem to run with the settings as configured through the GUI ([email protected] ??? is not the email configured), presumably due to the way it gets invoke through the cron, maybe run in

 

Thoughts?

 

Script:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
import re

MAX_THRESHOLD = 30

process = subprocess.Popen(['/usr/bin/sensors'], stdout=subprocess.PIPE)
out, err = process.communicate()

for line in out.split("\n"):
    if line and not line.isspace():
        m = re.match(r'CPU Temp\:\s+[+]?(\d+(\.\d+)*)\s?°([CcFf])',line)
        if m:
            try:
                temp = float(m.group(1))
            except ValueError:
                print "Not a float"
            break

if temp > MAX_THRESHOLD:
    command_line = ['/usr/local/emhttp/webGui/scripts/notify', '-i', 'alert', '-s', 'unRAID CPU Temperature High', '-d', 'Temperature at %s degrees' % temp]
    process = subprocess.Popen(command_line, stdout=subprocess.PIPE, shell=False)

 

 

 

Link to comment
11 minutes ago, Squid said:

IIRC any exit status from a script other than zero will trigger an email via cron to "root"

 

It's odd that the script triggers an exit status other than zero via cron but one thing for sure, the /usr/local/emhttp/webGui/scripts/notify command is not running properly through python via cron.

 

If I run from the command line I see the valid outgoing email account as configured through the GUI Notifications email setup, but through cron I see: [email protected]

 

Sent mail for [email protected] 
Link to comment
4 minutes ago, Squid said:

Closer look at the error though shows it trying to run via cron from /root/scripts. Did you mean /boot/scripts?

 

Thanks for the help Squid but the path is fine, I copy my scripts to /root on boot up via the go file.

 

file /root/scripts/check_cpu_temp.py
/root/scripts/check_cpu_temp.py: Python script, UTF-8 Unicode text executable

I think it has more to do with the way python invokes the shell to run the the notify command (script) or something. Maybe I'll try to simply rewrite the script as a bash script instead.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.