0

I need to run, on Ubuntu 18.04, via cron a script, myscript.sh that I want, among other things, to display to the user a message when it is being run so that he is aware that cron is being run (perhaps there are also ways to do this that somehow don't involve any GUI-related things; if yes, do let me know).

But since cron runs in its own, minimal environment, I wasn't able to figure out how to do that.

I tried various approaches and the most promising seemed to explicitly assign DISPLAY and start a terminal with this display assigned to it and inside that terminal to run a command that displayes a message, meaning in myscript.sH I have two lines

export DISPLAY=:0 XAUTHORITY=~/.Xauthority 
gnome-terminal --display=:0.0 -- bash -c "xmessage -center -timeout 10 'ATTENTION. CRON IS RUN';exec bash"

When I run in my own terminal, without cron being involved, gnome-terminal --display=:0.0 -- bash -c "xmessage -center -timeout 10 'ATTENTION. CRON IS RUN';exec bash" this works (of course, in my terminal I wouldn't need to start another terminal and could simpy run xmessage -center -timeout 10 'ATTENTION. CRON IS RUN' ...).

But cron complains with the following message (I'm redirecting the outputs of what cron execute to a file, so that I can see what went wrong):

No protocol specified
Unable to init server: Could not connect: Connection refused

If I instead place xmessage -center -timeout 10 'ATTENTION. CRON IS RUN' in myscript.sh, cron complains

No protocol specified
Error: Can't open display: :0

How can I get either this command, or any other GUI message to be displayed? I don't really care through what system the message is displayed, as long as the user is alerted in some way that right now cron is being executed.

Edit In the end I found the right software for my purpose, called Zenity, that can display text with zenity --info --text="test".

MyCatsHat
  • 65
  • 5
  • If you solved it yourself, you should [post your own answer](/help/self-answer) below (and accept it, not my answer). Your answer will not be construed as an answer if it is part of the question. – Michael Hampton Aug 30 '20 at 16:35

2 Answers2

5

You can use the notify-send application to send a notification to the logged in user's desktop. This program is in the libnotify-bin package.

For example:

notify-send --icon install "Cron job" "It's done, boss"

You may wish to background this. In my testing, I found that it would hang if no user was logged in at the time. It could just be a bug in my version, but you should be prepared for things to go wrong.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • It doesn't seem to be working, unfortunately - even when I simply execute it in the terminal, without cron involved. I made sure to have the library installed. My setup is really simple, since I don't use any remote stuff, where no users might be logged in. It's always me who's logged in and I simply want to remind myself (or whoever is sitting, logged in as myself, in fron of the computer) of some stuff – MyCatsHat Jun 22 '20 at 09:15
  • Is there perhaps some alternative (except the `xmessage` that I quoted that suffers from the `display not found`) I can use? – MyCatsHat Jun 22 '20 at 09:15
  • Are you actually using a desktop environment? GNOME, KDE, LXQt, whatever? Is DBus running? What error did you get? – Michael Hampton Jun 22 '20 at 11:38
  • Are you actually running the cron script as the logged-in user? I.e. what is the exact line that configures the cron script, and in which file is it? It's not explicit in the docs, but I would expect notify-send to send to the user that it is executed as. – Law29 Jun 22 '20 at 17:42
  • @MichaelHampton I'm very sorry for getting back so late, I had a lot of stress and was unable to access from stackexchange forums for a while. Yes, I'm running GNOME, latest version. Yesm dbus is running. I don't get any error: Nothing happens, when I run the command. – MyCatsHat Aug 26 '20 at 15:17
  • @Law29 Yes, I am. I run `00 11 * * * "/path/to/my/script.sh"` and in the `script.sh` I have the command above with `notify-send`. – MyCatsHat Aug 26 '20 at 15:19
2

if you want to run any program with custom XDISPLAY :

Bash Stack
  • 430
  • 2
  • 6
  • It also doesn't seem to work. I tried executing `/bin/bash -c 'DISPLAY=0:0 inkscape'` (in order to replace `mycommand` with an actual command) first and then I added the line `xhost +127.0.0.1` before that - and in both cases I get an error: `xhost: unable to open display "" (inkscape:23836): Gtk-WARNING **: 12:11:01.918: cannot open display: 0:0` – MyCatsHat Jun 22 '20 at 09:13
  • What *exactly* do I need to do in order to get working? I have now idea what XAuthority does and it does not seem to be a good idea to mess with it, since it looks like I can really mess up my system. – MyCatsHat Jun 22 '20 at 09:13
  • @MyCatsHat answer updated, the **:** in front of 0.0 is necessary ,also notice the dot between the zero ( `DISPLAY=:0.0 inkscape` ) , also for other displays e.g. it might be e.g. `DISPLAY=:1.0 inkscape` – Bash Stack Jun 22 '20 at 23:34
  • @MyCatsHat .XAuthorithy is a cookie file that is generated each time a X server starts , sometimes you might need to copy it to the user trying to execute the command . if you fear it , you could use `ssh -Y anotheruser@localhost inkscape` , but that is slower – Bash Stack Jun 22 '20 at 23:44