I'm running Ubuntu16.
I'm trying to run very simple bash scripts and cron jobs!

I'm trying to get cron to run the following bash script on a daily basis:

echo "Hello James how is your day going" 

I can run the script from the command line no problem, but cron won't? My Cron job is set up as such:

0 15 * * * /tmp/myjob.sh

What am I missing?

  • 3
    You have one wrong assumption. Your script and cronjob are working fine. What you actually are complaining about is the issue that you do not see the output it produces in your shell, or you even want a popup coming asking giving you the output. With the cronjob you have atm. everything happens in the background. – Ziazis Aug 9 '17 at 14:46
  • You're spot on, thats exactly what I'm trying to achieve! – SimplySimplified Aug 9 '17 at 14:46

What you actually need first of all is this script here:

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";

notify-send "Hello James how is your day going"

You can test by running it every minute.

*/1 * * * * /tmp/myjob.sh

This will give you a popup every minute with how you are doing ;)

  • I think you accepted the wrong answer ;) – Ziazis Aug 10 '17 at 6:32
  • Do you mean the answer before yours? Thought i accepted them both!! But yours worked, thanks. Now going to decipher it so that I fully understand what I've written. – SimplySimplified Aug 10 '17 at 8:01
  • Here is how to get the current user's $DISPLAY: askubuntu.com/a/744751/566421 – pa4080 May 25 '18 at 7:37
  • In addition */1 * * * * is equal to * * * * * – pa4080 May 25 '18 at 7:38

First, you should start bash scripts with 'shebang': #!/bin/bash (don't forget the # key). Also give the file execution permission:

chmod +x /tmp/myjob.sh 

Then on cron I would suggest you to put as:

0 15 * * * /tmp/myjob.sh

You cron job is set to run every day as 15:00 (3pm). I would suggest you to put it to run every 2 minutes to test first.

crontab -u yourusername -e

Add example to turn off monitor in 2 minutes:

*/2 * * * * XAUTHORITY=/home/yourusername/.Xauthority DISPLAY=:0.0 xset dpms force off > /dev/null

restart cron

service cron restart

No need to create a .sh file!

  • Wouldn't that be really annoying? – Zanna May 25 '18 at 8:16

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.