0

I have created a .sh file which runs a drupal console command via bash. I have tried to run this script in linux cron and all other commands work with exception to the drupal console command.

How can I get the drupal console command to work using cron?

Error output:

/var/www/html/***/removeusers.sh: line 79: drupal: command not found

crontab:

16 16 * * * bash /var/www/html/****/removeusers.sh --db_user=**** --db_password=**** --db_name=**** --db_host=localhost  >> /var/log/removeusers.log 2>&1

removeusers.sh:

 #!/bi/bash -x
PATH=/bin:/sbin:/usr/bin:/usr/sbin
/bin/sh /etc/profile

#removeusers.sh

# Help menu
print_help() {
cat <<-HELP
This script is used to remove users of a Drupal database
you need to provide the following arguments:

  1) Database username.
  2) Database password.
  3) Database name.
  4) Database host.

Example: (sudo) bash ${0##*/} --db_user=USER --db_password=PASSWORD --db_name=DBNAME --db_host=HOST
HELP
exit 0
}

while [ "$#" -gt 0 ]; do
  case "$1" in
    --db_user=*)
        db_user="${1#*=}"
        ;;
    --db_password=*)
        db_password="${1#*=}"
        ;;
    --db_name=*)
        db_name="${1#*=}"
        ;;
    --db_host=*)
        db_host="${1#*=}"
        ;;
    --help) print_help;;
    *)
      printf "***********************************************************\n"
      printf "* Error: Invalid argument, run --help for valid arguments. *\n"
      printf "***********************************************************\n"
      exit 1
  esac
  shift
done

if [ -z "${db_user}" ] || [ -z "${db_password}" ] || [ -z "${db_name}" ] || [ -z "${db_host}" ]; then
  printf "*********************************************\n"
  printf "* Error: Please provide valid access. *\n"
  printf "*********************************************\n"
  print_help
  exit 1
fi

uidList=($(mysql -u ${db_user} -p${db_password} ${db_name} -h ${db_host} -N -B -e "select uid from authmap;"))


# return array instance
# echo ${uidList[$i]}
# return array length
# echo ${#uidList[@]}

# split array into groups of 10 and 

for (( i=0; i<=${#uidList[@]}; i++))
    do
        echo ${#uidList[@]} "entries available to delete"
        counter=0
        batch=10
        while [ $counter -le $batch ]
        do
            echo "Attempting to delete user id" ${uidList[$i]}
            if drupal user:delete --user-id="${uidList[$i]}"; then
                echo $counter 'of' $batch 'in group before server rest'
            else
                printf 'delete failed\n'
                break
            fi
            ((counter++))
            ((i++))
        done
        echo All done
        sleep 10
        if [ ${#uidList[@]} -lt 0 ]; then
            break
        fi
    done
  • Cron doesn't run in a shell environment. At the very least you'll need to supply the full path to the drupal command. – Alfred Armstrong Jun 6 '18 at 15:38
  • I've added cd /path/to/file.sh if I bash the file manually the drupal console command works and the script executes perfectly. In theory I should be able to run the whole script by calling it in cron but it fails when it gets to the drupal console command, even with a path to the file. – Bruce Davidson Jun 6 '18 at 15:46
  • I meant in the script you need to use /path/to/drupal to call the drupal command, if it's not finding it. – Alfred Armstrong Jun 7 '18 at 8:28
  • I follow you now. The bash file is inside drupal, so it runs perfectly in bash. I'm currently investigating a possible problem with crontab's environment variables vs my own user. – Bruce Davidson Jun 7 '18 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.