Set up `cron` job and mailto on error, on Arch

0

I use postfix linked to an commercial smtp server for cron jobs to send their owners an email in case of error/warning. This is a one box Arch setup.

postfix works. So does cronie. But although the simplest of all cron jobs does fire, I get an email from the cron daemon at every execution, which reads:

/bin/sh: warning: command substitution: ignored null byte in input.

The cron rule, to be run every minute as a test, is:

MAILTO=MYUSERNAME@LOCALHOSTNAME

* * * * * eval "export $(grep -Ez DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";/home/USERNAME/test

and the executable ~/test is basically:

#!/bin/bash
/usr/bin/notify-send 'Hello world!' --icon=dialog-information

I understand that grep -Ez [...] introduces a null byte, as is made necessary by its output with some unusual characters.

My toy example would function properly, if it weren't for that specific warning being emailed to USERNAME at every execution. How do I get rid of that ?

Cbhihe

Posted 2018-10-12T19:24:12.470

Reputation: 218

Answers

1

After some research.... it turns out that grep -Ez [...] is not the right way of doing this in a cron job.

/proc/[pid]/environ in Linux OS has the quirk that the null byte is the record separator, so that its content when output to stdout "looks" like a single line, i.e. with no EOL character:

$ cat -v /proc/$(pgrep -u $LOGNAME gnome-session)/environ
LC_MEASUREMENT=en_IE.UTF-8^@LC_PAPER=en_IE.UTF-8^@LC_MONETARY=en_IE.UTF-8^@LANG=en_US.UTF-8^@GDM_LANG=en_US.UTF-8^@DISPLAY=:1^@USERNAME=USERNAME^@MOZ_PLUGIN_PATH=/usr/lib/mozilla/plugins^@XDG_VTNR=2^@XDG_SESSION_ID=2^@USER=USERNAME^@DESKTOP_SESSION=gnome^@PWD=/home/USERNAME^@HOME=/home/USERNAME^@XDG_SESSION_TYPE=x11^@XDG_SESSION_DESKTOP=gnome^@LC_NUMERIC=en_IE.UTF-8^@MAIL=/var/spool/mail/USERNAME^@WINDOWPATH=2^@SHELL=/bin/bash^@XDG_CURRENT_DESKTOP=GNOME^@XDG_SEAT=seat0^@SHLVL=0^@GDMSESSION=gnome^@LOGNAME=USERNAME^@DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus^@XDG_RUNTIME_DIR=/run/user/1000^@XAUTHORITY=/run/user/1000/gdm/Xauthority^@PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl^@LC_TIME=en_IE.UTF-8^@

In the above, I used cat with the -v flag to reveal nul bytes.

So the solution is to look for the pattern "DBUS_SESSION_BUS_ADDRESS" while recognizing null bytes. awk and gawk both do that although it is not portable, by changing the record separator to the hexadecimal code for the nul byte: \x00

$ awk -F 'BEGIN {RS="\x00"} /DBUS_SESSION_BUS_ADDRESS/' /proc/$(pgrep -u $LOGNAME gnome-session)/environ
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

That actually produces a clean output with no nul bytes and is well liked by cron. No warning is produced and no email is sent.

Cbhihe

Posted 2018-10-12T19:24:12.470

Reputation: 218

1As your system uses the "user bus" style in /run, rather than a random session bus address in /tmp, this whole greppage is now optional and you can safely hardcode the bus address. (Or even rely on recent libdbus already having it coded as a default when the environment variable is missing.) – user1686 – 2018-10-12T19:50:41.357

@grawity: I absolutely did not know that. Would you like to write a simple answer to that effect, so others may actually not miss your comment. Just bear in mind that I was looking for portability, across Linux systems, something my answer does not quite acheive.... Tx. – Cbhihe – 2018-10-12T19:56:39.437

1

Replace \0 with \n:

eval "export $(tr -s \\0 \\n </proc/$(pgrep -u $LOGNAME -x gnome-session)/environ|grep DBUS_SESSION_BUS_ADDRESS)";/usr/bin/notify-send 'Hello world!'

Ipor Sircer

Posted 2018-10-12T19:24:12.470

Reputation: 3 578

Good idea IporSircer but even replacing "icewm" with something like "gnome-session" produces output very close to mine but with a "D" character at the beginning of the line. I would also tend to favor my posted solution because it lacks your pipe, but that's no big deal. – Cbhihe – 2018-10-12T19:52:18.293

It was a mistake, updated. – Ipor Sircer – 2018-10-12T19:54:19.477

.... "-x".... ? – Cbhihe – 2018-10-12T20:01:43.687