# Disk usage - Crontab backup



## FestusHagen (Aug 3, 2009)

Hi all,

FreeBSD 7.1-RELEASE

I have a strange issue and don't know where to look to it's resolution.

The following code produces two different results depending on if it's run from the command line or from a cron job...

From a cron job it is exactly twice the block count.

Any ideas??

Command line:

```
sh SpaceUsed.sh
```
user Cron job:

```
30	4	*	*	*	/path_to/SpaceUsed.sh
```
Example output from command line

```
Space used: 912924
```
Example output from a crontab

```
Space used: 1825848
```
And the code.  (to run, TARGET needs to be edited)

```
#!/bin/sh

#set -x

TARGET="/path"
LOGNAME="test.log"

SpaceUsed()
{
  # for demo simplicity
  du -s "${1}" | cut -f1
#  echo $(($(du -s "${1}" | cut -f1) * 1024)) | sed -e :x -e 's/\([0-9][0-9]*\)\([0-9][0-9][0-9]\)/\1,\2/' -e 'tx'
}

echo " Space used: $(SpaceUsed ${TARGET})" >> ${TARGET}/${LOGNAME}
exit 0
```

Thanks all!

-Enjoy
fh  : )_~


----------



## danger@ (Aug 3, 2009)

may be a BLOCKSIZE issue.


----------



## DutchDaemon (Aug 3, 2009)

cron has its own set of variables, and they may differ from yours. This looks like a df -b (512-blocks) / df -h (1024-blocks) type difference, i.e. $BLOCKSIZE. 

In your own shell, run [cmd=]echo $BLOCKSIZE[/cmd], and then take the output (without the leading dollar sign) and set the resulting variable at the top of the crontab. E.g., if the output in your shell is


```
$BLOCKSIZE=K
```

then put this at the top of the crontab:


```
BLOCKSIZE=K
```


----------



## FestusHagen (Aug 3, 2009)

Awesome, That was it!

I did as suggested adding BLOCKSIZE to the crontab, however I did remove it and just added a "k" to the du command.

I assumed the user crontab would have the users environment.
Thats what I get for ASSUMING ... Embarrassment!

If all else fails ... BE EXPLICIT!

ie:

```
du -[color="Red"]k[/color]s "${1}" | cut -f1
```

Thank you all for doing what you do!

-Enjoy
fh  : )_~


----------

