Anacron will not run notifications from python/bash scripts

Question:

The basics

I am trying to add desktop notifications to some fairly simple scripts running with anacron just to let me know when they are running and when they have finished. For some reason, the scripts DO run, but the notifications never get sent. If I run the scripts manually (that is to say, using ./bash_test.sh instead of sudo anacron -fdn testing), the notifications send just fine.

The scripts

The bash script I am trying to run looks like this:

#!/bin/bash

python -c 'import notifications; notifications.clamstart()'

#some clamav scanning stuff happens in here, this bit runs fine

python -c 'import notifications; notifications.clamfinish()'

and the corresponding notifications.py file looks like:

from plyer import notification

def clamstart():
    notification.notify(
        message="Security script has started running.",
        app_name="Clam Scan Daily",
        hints={"desktop-entry":"clamtk"}
    )

def clamfinish():
    notification.notify(
        message="Security script has finished running.",
        app_name="Clam Scan Daily",
        hints={"desktop-entry":"clamtk"}
    )

Supplemental info

  • These two files are in the same directory, so as far as I’m aware the import statements should work fine (and they do, when I run it with ./bash_test.sh)

  • I have already tried using notify-send, that was what I had set up initially and it was running into the same problem, which is why I decided to try and switch to python’s plyer notify() and see if that worked.

  • ALL of these components work fine individually, they only stop working when I try to run them using anacron with sudo anacron -fdn testing

  • I believe the anacrontab is set up properly since it runs except for the notifications bit, but just in case I’ll add it here too:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22


#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly
1       10      backup-script   /home/glottophilos/backup_daily.sh
7       15      virus-scan      /home/glottophilos/clamscan_daily.sh
1       10      testing         /home/glottophilos/testscript.sh
  • I should also note that I am pretty opposed to the idea of using cron instead of anacron because this is a setup for a personal rig that is not on all the time. If there is another way of handling the scheduling though that doesn’t require anacron at all, I’m happy to explore that option!

  • This is NOT a duplicate of Using notify-send with cron They are similar, but the answer that I posted has some structural differences and works where the other does not for reasons I’m not entirely sure of.

Asked By: Jonathan Walls

||

Answers:

Solution

OK, so the solution, as per @Nick ODell’s direction in the comments on the original, appears to have been doing this in the etc/anacrontab file:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22


#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly
1       10      backup-script   sudo -u glottophilos DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/glottophilos/backup.sh>
7       15      virus-scan      sudo -u glottophilos DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/glottophilos/clamscan-daily.sh>
1       10      testing         sudo -u glottophilos DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/glottophilos/testscript.sh>

and then using this format in the bash script (avoiding python altogether):

notify-send --app-name="Clam Scan Daily" --hint=string:desktop-entry:clamtk "Security script is running."
Answered By: Jonathan Walls