Link to home
Start Free TrialLog in
Avatar of enyrix
enyrixFlag for Canada

asked on

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:
#!/bin/sh
...
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases'|$EGREP -vi 'information_schema|performance_schema|mysql')"
for db in $DBS
do
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
done

Open in new window

Why and how to solve this error ? Thanks.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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.
Avatar of enyrix

ASKER

# which tar
/bin/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
TAR=/bin/tar

Open in new window


But when I using this:
TAR=/usr/lib/tar

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

Avatar of enyrix

ASKER

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?

Add

echo HOST $HOST
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.
Avatar of enyrix

ASKER

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.
Avatar of enyrix

ASKER

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 :
export HOSTNAME
HOST=$HOSTNAME

Open in new window


Why I am unable to get the hostname when he executed in crontab ?
Avatar of enyrix

ASKER

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.
Avatar of enyrix

ASKER

# 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.
Avatar of enyrix

ASKER

Ok thanks! I will remember it when I will use other script in crontab.