Announcement

Collapse
No announcement yet.

How can I use send a message to a user from cron script?

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How can I use send a message to a user from cron script?

    Does anyone know a way to make notify-send work from a bash script that's executed by cron or anacron? I spent half the day trying to figure this out and read a lot of not-useful web pages.

    Basically, I want a anacron script to do something, then notify the user that it has occurred or if it failed. Maybe there's a better way than notify-send, but I like it's output.

    Mostly I've tried:

    su $USER -c "notify-send -i <icon> -t 0 '<title>' '<message>'"

    and also added DISPLAY=:0 in a couple places. My testing has been to run the script from the command line using "sudo". With the above entry, the script halts at it and does not proceed. Other variations result in error messages.

    Please Read Me

    #2
    Is https://unix.stackexchange.com/quest...-throws-errors of any help (last entry at the bottom (3))?
    Using Kubuntu Linux since March 23, 2007
    "It is a capital mistake to theorize before one has data." - Sherlock Holmes

    Comment


      #3
      Yeah, mail is not what I'm looking for. That would require setting up postfix, adding all the users, then reading the mail. I was hoping to get notify-send to work because it provides a nice pop-up that self-clears from the desktop but is available in the notifications window in systray. It's what I use in my BTRFS Subvolume Manager Dolphin service menus.

      The hang up is anacron necessarily runs a script as root. Thus the notify-send wants to send the notification to root rather than any user. I'm trying to get it to notify me rather than root. I've tried "DISPLAY:0" and putting my user name in the notify command but it just sits there. I've read at least 10 webpages that describe what "works" but none of them do here.

      Please Read Me

      Comment


        #4
        Okay. Did your searches come across this: https://askubuntu.com/questions/2350...n-in-user-mode
        Using Kubuntu Linux since March 23, 2007
        "It is a capital mistake to theorize before one has data." - Sherlock Holmes

        Comment


          #5
          cron (and anacron) run stuff with no association with a user, or a user's session. notify-send is about "desktop notifications", which implies a running desktop. It has to find the dbus session bus somehow; on my cosmic it uses various environment variables to find it.

          cron jobs are infamous for providing very few environment variables, not even those a shell sets up when not interactive. When I did that stuff many years ago I learned the hard way to ensure all the variables a job used were set. I'm not up to date with what cron and anacron do in KDE these days, but I would suggest running a script via cron that runs /usr/bin/env to dump out all the variables somewhere.

          By running
          Code:
          $ bash                                              # to get a sub-shell I can quit to get back to normal
          $ save_path=$PATH
          $ for x in $(env | sed 's/=.*//');do unset $x;done  # unset every environment variable!
          $ export PATH=$save_path                            # a modicum of sanity
          $ notify-send bob                                   # either hangs, or does nothing
          $ export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
          $ notify-send bob                                   # works
          $ exit
          it seems that DBUS_SESSION_BUS_ADDRESS is enough, though unsetting just that one doesn't stop notify-send. Presumably it can find the dbus through other variables. I'm surprised that setting DISPLAY doesn't help.
          Regards, John Little

          Comment


            #6
            Have you tried using "write" ?
            Originally posted by write
            Usage:
            write [options] <user> [<ttyname>]

            Send a message to another user.

            Options:
            -h, --help display this help
            -V, --version display version
            Mark Your Solved Issues [SOLVED]
            (top of thread: thread tools)

            Comment


              #7
              Originally posted by oshunluvr View Post
              Does anyone know a way to make notify-send work from a bash script that's executed by cron or anacron? I spent half the day trying to figure this out and read a lot of not-useful web pages.

              Basically, I want a anacron script to do something, then notify the user that it has occurred or if it failed. Maybe there's a better way than notify-send, but I like it's output.

              Mostly I've tried:

              su $USER -c "notify-send -i <icon> -t 0 '<title>' '<message>'"

              and also added DISPLAY=:0 in a couple places. My testing has been to run the script from the command line using "sudo". With the above entry, the script halts at it and does not proceed. Other variations result in error messages.
              A few notes:
              1. "$USER" probably does not work, as anacron is running as root.
              2. As mentioned, cron/anacron and sudo use very different environments.
              3. Hardcoding DISPLAY can work in single user environments, but is rather fragile in multiuser environments, so you probably should include some checks for the correct values in such cases.

              Have you tried actually letting anacron run your scripts (rather than running them manually)?
              I made a test script that seems to work when run from cron/anacron, but doesn't work (halts for a while, and finishes without notification) when running it from the cli with sudo...the script logs the env vars to a logfile, that may be useful for troubleshooting.
              Replace username with the actual user you want to notify.
              Code:
              #!/bin/sh
              NOTIFYUSER="[B]username[/B]"
              NOTIFYDISPLAY=":0.0"
              LOGFILE="/home/${NOTIFYUSER}/notifylog"
              TITLE="Notification Title"
              MESSAGE="Notification Message"
              ICON="dialog-information"
              
              date +%c > ${LOGFILE}
              env >> ${LOGFILE}
              su ${NOTIFYUSER} -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ${ICON} -t 0 '${TITLE}' '${MESSAGE}'" 2>> ${LOGFILE} 
              NOTIFYEXIT=$?
              echo "Exit code was: ${NOTIFYEXIT}" >> ${LOGFILE}
              Last edited by kubicle; Dec 30, 2018, 01:28 AM.

              Comment


                #8
                Thanks kubicle (and everyone) I'll try that.

                Please Read Me

                Comment


                  #9
                  I ended up using just this part of Kubicle's suggestion:

                  Code:
                  NOTIFYUSER="stuart"                      # the username to get the messages
                  NOTIFYDISPLAY=":0.0"
                  
                  su $NOTIFYUSER -c "export DISPLAY=${NOTIFYDISPLAY}; /usr/bin/notify-send -i ksnapshot -t 0 'Daily snapshots:' 'Daily snapshot operation complete.'"
                  This last line is repeated in two other places with different messages.

                  I didn't use the logging suggestions because I'm using this to log to syslog;

                  [#]exec 1> >(logger -s -t $(basename $0)) 2>&1 # Log the script activity[/#]

                  As kubicle stated, running this from the console freezes the script, but it worked when running it from cron.

                  Please Read Me

                  Comment

                  Working...
                  X