Cron runs fine in shell, but fails when called by cron

I have the below script doing nightly backups of my databases.

If I execute directly via shell, everything works.

However, if via cron as same user execution, I get this error in the log file: nightly-backups.sh: 9: [[: not found

  1 
  2 timestamp=`date +%Y-%m-%d`;
  3
  4 ##### EXECUTE THE MYSQL BACKUP #####
  5 echo "Starting MySQL Backup:" `date`;
  6
  7 databases=`/usr/bin/mysql -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
  8 for db in $databases; do
  9     if [[ "$db" != "information_schema" ]] && [[ "$db" != _* ]] && [[ "$db" != "mysql" ]] && [[ "$db" != "performance_schema" ]] ; then
 10         echo "Dumping database: $db"
 11         /usr/bin/mysqldump --single-transaction --routines --triggers --databases $db > /home/db_backup/backup/mysql/$db-$timestamp.sql
 12         /usr/bin/pigz /home/db_backup/backup/mysql/$db-$timestamp.sql
 13     fi
 14 done
 15
 16 echo "Finished MySQL Backup:" `date`;

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

 [[: not found

[[ is bash operator. It seems cron executes script with another shell interpreter.
You can provide path to interpreter in first line of your script:

#!/bin/bash

or

#!/usr/bin/env bash


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x