tar: Options '-[0-7][lmh]' not supported by *this* tar

I make a script that work perfectly without error when I execute it in terminal, but when he executed in crontab, i receive this error.
tar: Options '-[0-7][lmh]' not supported by *this* tar

Open in new window

This is part of my script:
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases'|$EGREP -vi 'information_schema|performance_schema|mysql')"
for db in $DBS
LOG "$db"
$MYSQLDUMP --single-transaction --add-drop-table --allow-keywords -q -c -u $MUSER -h $MHOST -p$MPASS $db $i > $BACKUP/$HOST-$NOW-$db.sql
$TAR -czvf $HOST-$NOW-$db.sql.tar.gz $HOST-$NOW-$db.sql 1>/dev/null

Open in new window

Why and how to solve this error ? Thanks.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

It seems that you have two different flavors of "tar" on your machine.
The shell used by your terminal obviously has the working version of tar first in its PATH,
but the "TAR" variable you set in the script contains the path to the other version, or contains more than just "/path/to/tar"

So please check what "TAR" is set to in the script and run "which tar" on your terminal, then compare the results.

The solution should be setting the TAR variable in the script to what you got with "which tar" on the terminal.

If there is no difference between the content of "$TAR" and the result of "which tar" it might be that one of the values for
$HOST (probably) or maybe $NOW or $db is not set in the script, and the terminal shell has a preset variable for the missing value which cron has not.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
If the same shell script was used in both situations, my guess is that the difference lies in the PATH variable. A cronjob executes with your permissions, but does not necessarily have the same environment that you have if you log in.

As WoolMilkPorc suggests, this also indicates you may have more than one tar installed.

It's also possible that your script will work with either tar if you remove the '-' from the front of the first parameter passed to tar.
enyrixAuthor Commented:
# which tar
# whereis tar
tar: /bin/tar /usr/lib/tar /usr/share/man/man1/tar.1.gz

Open in new window

In my script I already have:
TAR="$(which tar)"

Open in new window

I just tested this, but same error

Open in new window

But when I using this:

Open in new window

 /usr/lib/tar: Permission denied

My script use root user. I verified permissions, the second are empty folder:
# ls -al /bin/tar
-rwxr-xr-x 1 root root 353840 Feb  4  2014 /bin/tar
# ls -al /usr/lib/tar
total 16
drwxr-xr-x  2 root root  4096 Feb  4  2014 .
drwxr-xr-x 78 root root 12288 Aug  4 15:19 ..

Open in new window

Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

enyrixAuthor Commented:
Same error if I remove the '-' before tar options.
I don't consider this to be good practice:

TAR="$(which tar)"

It's just going to do exactly what the shell would do if you simply used the tar command without all the substituted variable rigamarole; it is still dependent on the environment's PATH variable.
Same error if I remove the '-' before tar options.

That's kinda odd. The '-' was the only thing in common between the tar options and the error message's list of exclusions.
/usr/lib/tar is just a folder, not an executable.

What's in $HOST inside the script, and what's in $HOST on the terminal?


echo NOW $NOW
echo db $db

 to the cron script, just before "$TAR ... ..." and run it once via terminal and once  via cron. What do you get?

By the way, you're missing the "$BACKUP" path component in the tar command which you used to redirect the output of mysqldump!
Under some (strange) circumstances this could lead to the said error, too.
enyrixAuthor Commented:
When y tested, I miss to uncomment my variable TAR.
I tested, error given:
czvf: not found

I also tested directly this whitout variables for tar:
tar -czvf $HOST-$NOW-$db.sql.tar.gz $HOST-$NOW-$db.sql 1>/dev/null

Open in new window

How can you determine what $HOST is set to in the crontab environment? If that were not set, you might get this error message.
@jmcg: As for $HOST - see my comments above.
enyrixAuthor Commented:
You saw right, error is HOST:
[2015-08-17 13:18:01]: HOST
[2015-08-17 13:18:01]: NOW 20150817-1318
[2015-08-17 13:18:01]: db api

In my script I'm using this in :

Open in new window

Why I am unable to get the hostname when he executed in crontab ?
enyrixAuthor Commented:
I changed my script for using this:
HOST=$(cat /etc/hostname)

Open in new window

This worked!

But I want to learn why I cannot use 'export HOSTNAME' ?
Which is your OS?

HOSTNAME is not a standard variable in some systems, and if it were, there wouldn't be any need to export it,
because it should already have been exported by e.g. /etc/profile or whatever global shell initialization file.

cron generally has a very restricted environment, and most cron implementations do not set HOSTNAME.

In your OS HOSTNAME is obviously empty, because it doesn't exist at all and/or because it is not set under cron.
enyrixAuthor Commented:
# lsb_release -a
No LSB modules are available.
Distributor ID:	Ubuntu
Description:	Ubuntu 14.04 LTS
Release:	14.04
Codename:	trusty

Open in new window

Ubuntu has Vixie cron, and this cron implementation (like many others) only sets SHELL, MAILTO, LOGNAME and HOME automatically.
PATH is set to /usr/bin.

All other variables must be set either by "VAR=value" (not "VAR=$(cmd)"!) in the crontab itself  or inside the cron scripts.
enyrixAuthor Commented:
Ok thanks! I will remember it when I will use other script in crontab.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.