0
  1. I set up to run notify-send every minute,

    $ crontab -l
    1 * * * * /usr/bin/notify-send -t 0 "hello"
    

    Why does it not work? Do I need to restart OS after editing the crontab file?

  2. Does the following mean that cron is running?

    $ ps aux | grep -i cron
    root      1038  0.0  0.0  23660  2420 ?        Ss   Apr20   0:00 cron
    
  3. Can I specify a more frequent schedule, such as 30 seconds? Can the time be specified as decimals?

    0.5 * * * * /usr/bin/notify-send -t 0 "hello"
    
2

Your first problem is that you have the wrong syntax for running a job every minute:

1 * * * * /usr/bin/notify-send -t 0 "hello"

The 1 in the first field means that the job runs only at 1 minute after each hour. Change it from 1 to *:

* * * * * /usr/bin/notify-send -t 0 "hello"

The second problem is that cron jobs run in a very limited environment. On my system (Linux Mint), the only environment variables that are set are $HOME, $LOGNAME, $PATH, $LANG, $SHELL, and $PWD -- and $PATH is normally set to "/usr/bin/:/bin".

At the very least, the lack of a setting for $DISPLAY means that notify-send can't display anything.

A quick experiment with:

* * * * * DISPLAY=:0.0 notify-send "hello from crontab"

resulted in this error:

(notify-send:18831): GLib-GObject-CRITICAL **: g_object_unref: assertion `G_IS_OBJECT (object)' failed

(I'm running the Gnome desktop.)

In another experiment, I copied my entire interactive environment into a script, then edited the script so it sets all the environment variables explicitly and invokes notify-send. That actually works; I'm now getting a pop-up "hello from crontab" message every minute.

I'm certain that I don't need all my interactive environment for this to work, but I don't know exactly which environment variables are needed or what their values need to be. It's very likely that some of the needed variables are set when the current login session is started, and that they'll change if I logout and login again. It's also very likely that the details will vary depending on which desktop environment you're using.

This is not a complete solution, but it should give you a starting point -- and perhaps someone else can add the relevant details.

1

Your cron job almost certainly is running. However, you cannot (easily) interact with your GUI from cron so unfortunately notify-send will fail.

You can prove whether or not your cron job is running by modifying the crontab line as follows

1 * * * * ( date; notify-send /usr/bin/notify-send -t 0 "hello"; echo ) >>cron.log 2>&1

This will write the date and any output from the notify-send command to a log file called cron.log in your home directory.

Please note, however, that as copied from your question this will only run at the first minute of every hour (1 * * * *). To run every minute you would need to use * * * * * ("every minute" instead of "1st minute"). Kudos to @KeithThompson for pointing this out.

The granularity of cron is one minute. If you need to run a job more frequently than this you may want to consider either a standalone daemon, or two lines in cron one of which is preceded by sleep 30.

  • how do you schedule notify in crontab? My eventual goal is to schedule backup by rsync in crontab, and get a desktop notification 5 mins before backup. How can I do that? – Tim Apr 21 '15 at 22:51
  • @Tim that's sufficiently different to this question that I'd recommend you ask it separately. (It's hard, though.) – roaima Apr 21 '15 at 22:53
  • Then how can you schedule notification in cron? – Tim Apr 21 '15 at 22:54
  • 1
    This makes the same mistake as the original question: the 1 in the first field means it runs once an hour, at NN:01, not once every minute. – Keith Thompson Apr 21 '15 at 22:56
  • @Keith ah yes, well spotted. +1 to you, thank you. I'll edit – roaima Apr 21 '15 at 23:17
0
$ crontab -l
*/1 * * * * /usr/bin/notify-send -t 0 "hello"

try this if you want to run your cron every minute.

  • 1
    Please would you explain why you think this is any different to the problematic crontab the OP has posted. – roaima Apr 21 '15 at 22:50
  • 2
    @roaima: It's different in that it actually runs every minute, rather than one minute after each hour as the cron job in the question would. * * * * * would also work; the /1 is not needed. But notify-send still won't work in that environment. – Keith Thompson Apr 21 '15 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.