1

Hope somebody can point me in the right direction in solving this problem. I have this bash with if statement.

checkPID=(MySQL queries to count columns)

if [[ $checkPID -eq 1 ]]
then
echo "PID already exist, running update queries instead"

(MySQL update queries here)

else

echo "PID does not exist, running insert queries"

(MySQL insert queries here)

fi

When I run this bash script on the command line everything works as expected, but when I automate this via crontab it doesn't follow the if condition regardless of the value of the checkPID variable.

AYSMAN
  • 11
  • 1
  • How do you run it, via file, or directly in your term ? – Ôrel Sep 06 '22 at 07:28
  • when I test it i run with the bash command. when I want to automate it i run it via crontab – AYSMAN Sep 06 '22 at 07:46
  • via a script file or inline in crontab ? – Ôrel Sep 06 '22 at 07:50
  • 1
    Standard crontab troubleshooting: does anything in it depend on the environment (working directory, `PATH`, other environment variables, etc)? Have you captured output & errors from it (e.g. by adding something like `>>/tmp/cronjob.log 2>&1` to the crontab command, then checking the file for indications of what's going wrong)? – Gordon Davisson Sep 06 '22 at 08:14
  • 2
    I suggest that you do at the start of your cron-script a `set -x; echo $BASH_VERSION`. This will not only show you all the statements being executed, but you can also verify that `bash` is indeed used. And, don't forget that in your `bash` command line you have a different environment than with cron. – user1934428 Sep 06 '22 at 09:05
  • Also, you didn't post your crontab, and don't provide any meaningful error description. How can you expect help here? – user1934428 Sep 06 '22 at 09:06
  • Hi, Thank you guys for your replies. @user1934428 my crontab please see below. */10 * * * * sh /home/servo/scripts/slavereport.sh – AYSMAN Sep 08 '22 at 01:13
  • Hi @GordonDavisson did what you suggested I got this error message on the log /home/servo/scripts/slavereport.sh: 34: /home/servo/scripts/slavereport.sh: [[: not found – AYSMAN Sep 08 '22 at 01:40
  • 1
    @AYSMAN That error suggests the script is running under dash (which doesn't support `[[ ]]` conditionals) instead of bash (which does). Does the script have a bash [shebang line](https://stackoverflow.com/questions/25165808/should-i-use-a-shebang-with-bash-scripts) (i.e. `#!/bin/bash` or `#!/usr/bin/env bash`, and *not* `#!/bin/sh`)? Also, in the crontab entry, do you run it directly or use something like the `sh` command to run it? – Gordon Davisson Sep 08 '22 at 02:09
  • Hi @GordonDavisson, Thank you for this. I changed the crontab entry from "sh" to "bash" and this made the script run as expected. Adding this >>/tmp/cronjob.log 2>&1 om crontab absolutely helped. Cheers! – AYSMAN Sep 08 '22 at 03:25
  • @AYSMAN It's generally better to set a proper shebang in the script, make sure it has execute permission set, and then run it directly (without either the `sh` or `bash` commands). That way, the script itself controls what it runs in, which is where the control really should be. – Gordon Davisson Sep 08 '22 at 03:38
  • Hi @GordonDavisson so you mean this "#!/bin/bash" on top of the script file? – AYSMAN Sep 08 '22 at 05:34
  • Don't put crucial information, such as the content of your crontab, in a comment. Write it into your question. BTW, you aren't running your script as bash in this way, but by sh. Also, if this is the **complete** crontab, I don't see where you set up your environment. This makes me wonder: Did you ever check the stderr from your script when executed by cron???? – user1934428 Sep 08 '22 at 06:28
  • @AYSMAN Yes, that's the shebang line. – Gordon Davisson Sep 08 '22 at 07:53

1 Answers1

0

Your crontab line uses dash (/bin/sh) instead of bash.

*/10 * * * * sh /home/servo/scripts/slavereport.sh –
change to:
*/10 * * * * bash /home/servo/scripts/slavereport.sh –

Since you are using bash operations, remember to put #!/bin/bash as the first line in the script.

Fixing either of these should also resolve the issue. It isn't required to do both.

James Risner
  • 5,451
  • 11
  • 25
  • 47