Search code examples
mysqlbashcron

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`;

Solution

  •  [[: 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