My cronjob should execute a script with mysql queries
If I look into the log of the cronjob, I see the output of the mysql help command I expect to see 2 table names from tables that are of type view
USERNAME
, PASSWORD
and HOSTNAME
are environment variables
My first question is: Is it possible that root gets a different result on echo $USERNAME
than another user?
#!/bin/sh
DBNAME="my_db"
# get all table names that are type of view
query="SHOW FULL TABLES IN $DBNAME WHERE TABLE_TYPE LIKE 'VIEW';"
views=$(mysql -sN -u"$USERNAME" -p"$PASSWORD" -h"$HOSTNAME" -e "$query" "$DBNAME")
echo "$views"
The output that I get is:
mysql Copyright Oracle affiliates. owners. Usage: -?, -I, --auto-rehash 'rehash' and --disable-auto-rehash. (Defaults -A, No table mysql --auto-vertical-output Automatically result -B, (Enables --binary-as-hex --character-sets-dir=name Directory --column-type-info -c, default with -C, -#, --debug-check -T, -D, --default-character-set=name Set --delimiter=name --enable-cleartext-plugin Enable/disable -e, file.) -E, -f, -G, Enable internal named otherwise Disable disabled -i, --init-command=name Will --local-infile -b, -h, -H, -X, --line-numbers (Defaults -L, Don't -n, --column-names (Defaults -N, Don't --sigint-ignore -o, default --pager[=name] option, PAGER. etc. work option -p, Password not -P, order /etc/services, --prompt=name --protocol=name memory). -q, down history -r, --reconnect --disable-reconnect. (Defaults -s, each -S, --ssl other --ssl-ca=name --ssl). --ssl-capath=name --ssl-cert=name --ssl-cipher=name --ssl-key=name --ssl-verify-server-cert Verify hostname default. --ssl-mode=name -t, --tee=name also. --disable-tee. -u, -U, -U, -v, -V, -w, --connect-timeout=# --max-allowed-packet=# The server. --net-buffer-length=# The --select-limit=# --max-join-size=# --safe-updates. --secure-auth (pre-4.1.1) --server-arg=name --show-warnings --plugin-dir=name --default-auth=name Default /etc/mysql/my.cnf The The --print-defaults --no-defaults --defaults-file=# --defaults-extra-file=# Variables and --------------------------------- auto-rehash auto-vertical-output binary-as-hex character-sets-dir column-type-info comments compress debug-check debug-info database default-character-set delimiter enable-cleartext-plugin vertical force named-commands ignore-spaces init-command local-infile no-beep host html xml line-numbers unbuffered column-names sigint-ignore port prompt quick raw reconnect socket ssl ssl-ca ssl-capath ssl-cert ssl-cipher ssl-key ssl-verify-server-cert table user safe-updates i-am-a-dummy connect-timeout max-allowed-packet net-buffer-length select-limit max-join-size secure-auth show-warnings plugin-dir default-auth
-sN
but did not help.sudo mysql -sN -u$USERNAME -p$PASSWORD -h$HOSTNAME -e "SHOW FULL TABLES IN my_db WHERE TABLE_TYPE LIKE 'VIEW';" my_db
Can someone help to find the issue?
EDIT: I found out, that the Environment Variables are empty if I use them in the script and run it as root.
printenv
I can see they are theresudo printenv
they are not thereThe solution for this problem was to add the following line to the top of the script:
. /opt/elasticbeanstalk/support/envvars
This is required if you use AWS Elastic Beanstalk and want to make the Environment Variables available in a script that is triggered from a cronjob file in /etc/cron.d/.