0
#!/bin/sh

string1=$(date +"%T")
string2=$(date -r merchant.xml +"%T")
StartDate=$(date -d "$string1" +"%s")
FinalDate=$(date -d "$string2" +"%s")
echo Since,  $(date -d  "0 $StartDate sec - $FinalDate sec" +"%H:%M") HOURS, mail has not been updated | mail -s "Merchant File Staleness" hello@gmail.com

it is my shell script name hp.sh and output is

Since, 00:55 HOURS, mail has not been updated. 

The crontab is

0 * * * * /tmp/hp.sh 

and output of crontab is

Since, 07:04 HOURS, mail has not been updated.

The output of both is different. I need output of my shell script using crontab every hour.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Without knowing more, it looks like you may have a *localtime* and *UTC* difference (plus some variation in gmail processing time). What is your *timezone*? – David C. Rankin Mar 04 '20 at 07:40
  • I am using default UTC timezone – Hardik Pahwa Mar 04 '20 at 08:55
  • What does `ls -al /etc/localtime` show? Is it a symlink? If so, to what? The reason I ask is that what you run and the times shown should default to your localtime setting while what is logged by the system is often logged in UTC. So your user may see a time different than the cron system users does when it triggers your script. – David C. Rankin Mar 04 '20 at 09:18
  • I am getting this output lrwxrwxrwx 1 root root 23 Aug 31 2018 /etc/localtime -> /usr/share/zoneinfo/UTC when i run ls -al /etc/localtime . Please help me in Resolving the issue. – Hardik Pahwa Mar 04 '20 at 09:30
  • That confirms it, you are using UTC. There isn't an explanation I can see why you would have a 7 hour difference in time. (Unless you are dual-booting with windows and you are set to US mountain-time there). Only other test I can think of is to run your script as you. The `su` to root and run it as root and see if there is a difference. I run your script (without the `mail` part and all is well) – David C. Rankin Mar 04 '20 at 09:33
  • But i want the script run every hour through crontab and through crontab , I am getting different output from the script. – Hardik Pahwa Mar 04 '20 at 09:37
  • I'm confused at what you are saying. `cron` will run your script every hour. The time `hh:mm` will be the time *difference* between the last modification time of `merchant.xml` and current time. If the file hasn't been modified in 7 hours and 4 minutes, then `07:04 HOURS` will be displayed. If the cron script runs `55` minutes after the file was modified, then you will get `00:55` -- that's not tied to timezone, that's tied to the last modified time of `merchant.xml`. Is that the problem? – David C. Rankin Mar 04 '20 at 09:56
  • Basically , When i run the script through sh command I am getting the correct output and when i try to run the script through crontab , I get different Output . what should i do so that i get correct ouput through cron – Hardik Pahwa Mar 04 '20 at 10:09
  • I know my shell Script is Correct but crontab is not display the output of my shellscript correct – Hardik Pahwa Mar 04 '20 at 10:10
  • What is writing to `merchant.xml` between the time the `cron` script runs every hour? If it may or may not be updated -- that is where your different time some from. – David C. Rankin Mar 04 '20 at 10:59

1 Answers1

0

Check the TZ variable, both system and user-defined - cron will happily oblige to whatever is set there, even if it is different from system settings.

Rado
  • 66
  • 3