2

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:

#!/bin/bash
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?

Yaron
  • 13,173
  • 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 09 '17 at 14:46
  • You're spot on, thats exactly what I'm trying to achieve! – SimplySimplified Aug 09 '17 at 14:46

3 Answers3

5

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

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

#Code:
DISPLAY=:0 
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 ;)

Ziazis
  • 2,174
1
crontab -u yourusername -e

Add example to turn off monitor in 2 minutes:

MAILTO=""
*/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!

Zanna
  • 70,465
neo76
  • 11
1

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.

Adonist
  • 350