Cron with notify-send
I found the answer:
$ crontab -l
# m h dom mon dow command
* * * * * export DISPLAY=:0.0 && export XAUTHORITY=/home/ravi/.Xauthority && sudo -u ravi /usr/bin/notify-send Hey "How are you"
notify-send doesn't work in crontab (doesn't send a notification)
So finally, I follow this topic (http://ubuntuforums.org/showthread.php?t=1533494) and it works now. Thanks you Philibobby !
Crontab of python script using notify-send
I'm using a little bit different code, but the end result is what you were trying to achieve: get notify-send
to work from a python script via crontab
.
Here's my Python 3.x script:
import subprocess
subprocess.Popen(['notify-send', 'Running.'])
And here's how I setup crontab
:
DISPLAY=:0.0
XAUTHORITY=/home/matrix/.Xauthority
*/1 * * * * /usr/bin/python3 /home/user/path/to/my/script.py
This should work with Ubuntu 16.04 and python3, I cannot speak for other OS's, but you can give it a try. Use which
to check the path of your python
or python3
installation.
By the time I finished this answer the "Running." notification popped up at least 3 times.
Executing notify-send from fish script as cronjob
if I echo $DISPLAY in my terminal I get :0.0
Yes, but your cronjob doesn't run in your terminal.
In Unix, environment variables are passed from parent processes to their children when they're started.
The fish inside your terminal is a child of that terminal, which has $DISPLAY set to contact X.
But your cronjobs are run by your cron daemon, which is typically a child of your init process, which in turn doesn't have any parent. So it inherits the environment of init.
Set $DISPLAY in your script. This isn't pretty (and I can't say I like the approach of having a cronjob that sends notifications to begin with), but it should work, at least if you have the typical setup with one X server.
Note that fish is entirely irrelevant in this case - it would happen no matter what you picked as shell.
Some plausible alternatives (though I've not looked into them far):
- Run a
watch
job in a terminal or via your DE's autostart mechanism. This just reruns things every X seconds, but has$DISPLAY
- Use systemd's timer stuff, in particular as a user. There's a command to "upload" an environment variable to systemd, so it can then use it in timers.
notify-send not working from cron
Not sure why but this worked. Rather calling the notify-send directed added that in a script.
* * * * * export DISPLAY=:0.0 && /bin/sh /home/notifyCustom.sh
send email and notify-send do not work in crontab
When run as cron, you don't have any environment set up. Not even a PATH. So it is probably not finding notify-send or ssmtp.
Either add a PATH at the beginning of your script, or give fully-qualified path names to commands that you issue.
Related Topics
One Core Exclusively for My Process
Export Not Working in My Shell Script
Sed Command with -I Option (In-Place Editing) Works Fine on Ubuntu But Not MAC
Centos Through a Vm - No Urls in Mirrorlist
Linux Cross-Compilation for Arm Architecture
Adding Timestamp to a Filename with Mv in Bash
Shell: Redirect Stdout to /Dev/Null and Stderr to Stdout
Loading Elf File in C in User Space
Managing a User Password for Linux in Puppet
Sudo: Docker-Compose: Command Not Found
How to Set a Static Ip Address in a Docker Container
Grep from Tar.Gz Without Extracting [Faster One]
Why Is Kernel Mapped to the Same Address Space as Processes
Run Bash Commands from Txt File
Output File Lines from Last to First in Bash
How to Communicate with a Linux Kernel Module from User Space Without Littering /Dev with New Nodes