1

i'm facing a strange situation and i searched and haven't found anything. I have a bash script that take a date and add 72 hours to it. There is no problem when i run it from terminal but when it runs by cronjob it gaves me an error:

if [ $# -eq 3 ] ; then

# ----------
# PARAMETERS
# ----------
start_YYYYMMDDHH=${1}
ini_YYYYMMDDHH=${2}
flen=${3}
ini_HH=${ini_YYYYMMDDHH:8:2}
start_YYYY=${start_YYYYMMDDHH:0:4}
start_MM=${start_YYYYMMDDHH:4:2}
start_DD=${start_YYYYMMDDHH:6:2}
start_HH=${start_YYYYMMDDHH:8:2}
start_II=00
start_SS=00
end_YYYYMMDDHH=`date --utc +%Y%m%d%H%i%s -d "${start_YYYY}-${start_MM}-${start_DD} ${start_HH}:${start_II}:${start_SS} UTC +${flen} hours"`
end_YYYYMMDDHH=${end_YYYYMMDDHH:0:10}
end_YYYY=${end_YYYYMMDDHH:0:4}
end_MM=${end_YYYYMMDDHH:4:2}
end_DD=${end_YYYYMMDDHH:6:2}
end_HH=${end_YYYYMMDDHH:8:2}
end_II=00
end_SS=00

and when i run it by cron it gaves me this error:

date: extra operand ‘-d’ Try 'date --help' for more information.

do you have any idea why this is happening?

11
  • I tried adding it in the crontab file, works fine for me.
    – ss_iwe
    Commented Feb 21, 2017 at 13:52
  • @saisasanka the weird thing is that it works in another machine also and i tested !!!! any idea what can be the problem?
    – Aria R.
    Commented Feb 21, 2017 at 13:53
  • Does the machine which is not working have another date binary which is called which does not have the -d operand? Perhaps another shell with an date buildin. Try to call the date command using it's absolute path.
    – Lambert
    Commented Feb 21, 2017 at 13:55
  • Well, Check if there are different OS on the machines and also the crontab format or the shell you are using to execute this program perhaps.
    – ss_iwe
    Commented Feb 21, 2017 at 13:56
  • 1
    I would start the script with a shebang (#!/bin/bash) as you tell it's a bash script.
    – jlliagre
    Commented Feb 21, 2017 at 15:53

2 Answers 2

2

You have different path settings when run by cron, so always use absolute paths in cron scripts or services.

1
  • but i tried to run the echo $path from my crontab and it gave me the same PATH. and if it is how to tell crontab to use my main PATH?
    – Aria R.
    Commented Feb 21, 2017 at 14:45
0

i found the solution:

end_YYYYMMDDHH=$(date --date="${start_YYYY}-${start_MM}-${start_DD} ${start_HH}:${start_II}:${start_SS} UTC +${flen} hours" -u "+%Y%m%d%H")

it work properly but for some reason if you move this part: -u "+%Y%m%d%H" to the begining of the command it gave that error!

the very strange thing is the same command works on another machine!

1
  • Different implementations of the date utility will work differently. On Ubuntu, you're using GNU date, and AFAIK, this command should work with the most current implementation of GNU date. The GNU date manual states that the formatting string should be last on the command line.
    – Kusalananda
    Commented Feb 22, 2017 at 8:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .