0

I wanted my cron job to report to me on desktop when it executes, through notify-send command on Ubuntu. I've read through the common problems that stated a shell script didn't have access to a display, which is solved by adding this before calling notify-send:

export DISPLAY=:0.0

So i am okay in that regard.

The place where i am right now, is that my script works and notifies me on desktop if i invoke it from terminal manually, but not from the crontab.

The situation is as follows:

  1. The script that executes is a PHP file. The PHP command to invoke the shell command, is:
<?php
`export DISPLAY=:0.0 && command -v notify-send && notify-send "Hello world"`; 

(backticks in PHP mean execute in shell)

  1. In both cases i am running it as root
  2. When testing from terminal, i run:
sudo -u root /usr/bin/php -q /var/www/html/cron.php &> /dev/null

This works, and i get a desktop notification

  1. To edit my crontab for the root user, i use:
sudo -u root crontab -e
  1. In my crontab file, my line is this:
* * * * * /usr/bin/php -q /var/www/html/cron.php &> /dev/null

This one does not produce a desktop notification, even though the script 100% executes (i have the successful result in log files).

What goes wrong here, and why wouldn't i get the desktop notification?

Digital Ninja
  • 3,415
  • 5
  • 26
  • 51
  • I'd recommend to place your script in a `.sh` file and add the `export PATH=$PATH:...` in the beginning of the file. Hopefully then it should be able to execute it from the crontab. – User123 May 25 '19 at 09:07

1 Answers1

0

you must set the PATH within the script or export it from crontab!

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
UtLox
  • 3,744
  • 2
  • 10
  • 13