1

simple cron bash script which uses xmessage to alert the user to a specific condition. It works fine and does exactly as expected when I run it, but when I fire it off via cron, I get in syslog:

(CRON) info (No MTA installed, discarding output)

What am I doing wrong?

ETA: Here is the code:

#!/bin/bash
#
# script to capture current battery percentage and display in an alert dialog box
# displays alert only if percentage is less than 10%

export DISPLAY=":0"

percentage=$(upower -i $(upower -e | grep '/battery') | grep --color=never -E percentage|xargs|cut -d' ' -f2|sed s/%//)

if [[ $percentage -lt 99 ]] then

xmessage -buttons Ok:0 -geometry 500X500 -nearmouse "BATTERY IS AT $percentage%" -timeout 30

fi

I do not get the MTA error unless the xmessage line is executed. PS, yes, the 10% and 99% dont match, I set it to 99 for troubleshooting.

1 Answers1

1

The information message:

(CRON) info (No MTA installed, discarding output)

Is not your issue ... It simply means that your crnjob resulted in an output that you didn't provide a method for it to be redirected/handled(Notice: That output could be some useful information and maybe you should redirect it to a file for later inspection) and CRON in this case tries to email the output but doesn't succeed due to absence of MTA(Mail Transfer Agent) ... It's totally a normal behavior and installing an MTA will not make your cronjob execute better ... Please see Cron job log reads 'No MTA installed', does that prevent the CRON job from finishing?

Your issue, most likely, is that xmessage is an X-based application that requires an X server user session already running plus a display interface/server specified ... If your cronjob is guaranteed to run while an X server user session is already running and not otherwise, then you might be able to make it work if you find and specify the address of your display interface/server as an environment variable before your command in the crontab e.g. like so:

* * * * * DISPLAY=":0" your_command ...

You can see your current environment $DISPLAY variable while your user X session is running e.g. like so:

echo $DISPLAY

and even though, CRON might run your command before an X user session is even fully initiated in case of e.g. @reboot is specified or the user has logged out their session for some reason at the time CRON runs your command.

Notice also that if you add your command to root's crontab with sudo crontab -e then your X-based application might not run no matter what you do as root on Ubuntu, by default, can't start an X session.

Therefore given the above reasons, CRON is not by all means the preferred way for running X-based user applications.

Otherwise, I recommend you run your X-based script/command as a startup application ... Please see Startup script not executing for similar issue and possible workaround.

Also, please see Battery Full Indicator 22.04 for an example of a battery monitoring script that can be used in a startup application instead of a cronjob.

Raffa
  • 32,237