1
#!/bin/bash

ddd=$(date +%Y-%m -d "-1 month")
xmessage  -timeout 10 $ddd

If I try this simple script I get this to work fine from terminal but when I start this script via cron the variable is always empty? I have tried many different syntax but the result is the same. Works in Terminal but not from cron.

Zanna
  • 70,465
Peter
  • 11
  • xmessage won't work in cron without a bit more preparation. Cron has a different environment than your terminal. – user535733 Dec 26 '17 at 13:16
  • If you are not logged in there is no display where xmessage would be able to show. Try setting also the DISPLAY variable as you set ddd, before you call xmessage. Type set | grep DISPLAY to see what is your display (probably DISPLAY=:0). Use that line before calling xmessage, so it will know where to display. – nobody Dec 26 '17 at 13:35
  • ddd=$(date +%Y-%m) = Working and ddd=$(date +%Y-%m -d "-1 month") is not working :-( – Peter Dec 26 '17 at 15:10
  • Check /var/log/syslog/, it may contain more information on what cron is stumbling upon. – Harald Dec 27 '17 at 13:00

1 Answers1

1

man date says:

SYNOPSIS
       date [OPTION]... [+FORMAT]

It should work either way, but you're on the safe side using date the way the manpage tells you:

ddd=$(date -d "-1 month" +%Y-%m)

With a script

#!/bin/bash
ddd=$(date -d "-1 month" +%Y-%m)
xmessage -timeout 10 $ddd

and the cronjob line

* * * * * DISPLAY=:0 /path/to/script.sh

it works very well on my system – see How to start a GUI application from cron? and the Cron HowTo.

dessert
  • 39,982
  • GNU tools are fairly flexible when it comes to ordering arguments and options. I'm inclined to think the DISPLAY variable was the problem – muru Dec 27 '17 at 16:37
  • @muru I know, but the manpage tells me so. ;P Are you sure FORMAT is treated like an ordinary option? Either way, let me edit… I assumed DISPLAY=:0 was the real problem here, so I added a working solution. – dessert Dec 27 '17 at 16:43
  • the manpage synopses always use the safest, simplest description. Whether the ordering matters is given in the extended description, and it doesn't particularly matter for +FORMAT – muru Dec 27 '17 at 16:48