1

When i run this shell script manually on a Linux terminal by executing "./filename.sh" at the 12th hour (or any hour i specify). it works perfectly. it follows the IF condition perfectly

But when i use a cronjob to automate this script, for some reason, it never picks up the first part of the IF condition. it echoes "Not working" even while still being in the 12th hour.

i can't get to figure out what the problem is

Running on an Ubuntu 20.04 server.

Note: i edited the cronjob to run every 5mins while i troubleshoot . Here's the cronjob

*/5 * * * * /bin/sh /home/user/filenam.sh
#!/bin/bash

HOURID=`(date '+%H')`

if [[ $HOURID == 12 ]];
then
        echo "It works" > a.txt
else
        echo "Not working" > a.txt
fi
igbins09
  • 167
  • 8
  • 1
    Remove `(` and `)`. – Cyrus Aug 03 '22 at 17:30
  • Add '-vx' to the #! line (`#! /bin/bash -vx`). You will get lot to help you troubleshoot. If you can not figure out what the problem - post log here – dash-o Aug 03 '22 at 17:36
  • A good test would be to have your script output `date > a.txt` and see what cron outputs. You probably have a timezone issue. Also, it would be better to replace your == with -eq for a straight numeric comparison. – UncleCarl Aug 03 '22 at 17:37
  • @Cyrus removing the parentheses didn't work. Added the cronjob to the question. – igbins09 Aug 03 '22 at 17:53
  • 1
    You are using `sh` and not `bash`. – Cyrus Aug 03 '22 at 17:54
  • @dash-o where do i find this log file after adding it to my script? – igbins09 Aug 03 '22 at 17:59
  • @Cyrus it worked. pretty new to this stuff. never knew there was a difference between `sh` and `bash`. because they usually produce the same result. Gotta go read up more on that. thanks – igbins09 Aug 03 '22 at 18:07
  • You can force the stderr to a specific file by inserting the following as the second line: `exec 2>/path/to/lfile.log` . You can also redirect standard error on the cron command line - `*/5 * * * * /bin/sh /home/user/filenam.sh 2>/path/to/file.log` – dash-o Aug 03 '22 at 18:07
  • This just shows (yet again) how bad `cron` is, all ~5 different variants of it. Use [`systemd` timers](https://www.freedesktop.org/software/systemd/man/systemd.timer.html) instead; they have much higher flexibility, system-wide as well as per-user isolated instances, superior security hardening options and logs automatically available in `journalctl` (system-wide ones as well as per-user ones) (which is especially relevant for this case). It is nowadays quite often the case that `cron` is “emulated” by a `systemd` timer unit. And of course “the full thing” is better than just one unit. – Andrej Podzimek Aug 03 '22 at 18:22
  • @igbins09 Recommended reading: ["Difference between sh and Bash"](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash), ["Why does my Bash code fail when I run it with `sh`?"](https://stackoverflow.com/questions/15179446/why-does-my-bash-code-fail-when-i-run-it-with-sh), and the Ubuntu wiki entry ["Dash as /bin/sh"](https://wiki.ubuntu.com/DashAsBinSh). – Gordon Davisson Aug 03 '22 at 18:44
  • @UncleCarl thanks for the `-eq` parameter. it helped – igbins09 Aug 04 '22 at 22:35

0 Answers0