简体   繁体   中英

df shows double the actual size on FreeBSD when run via cron

I have written a bash script that collects data from a server and sends it to a log file that is being monitored by a splunkforwarder. The server runs FreeBSD and has a ZPool that is shared via Samba. So of course one of the things I'd like splunk to keep an eye on is how much space is used on said share.

To keep things readable, I have extracted the lines from my script that achieve this:

#!/usr/bin/env bash
while read disk used avail ; do
        # In reality I pass these values to a function
        # that adds some formatting, but let's keep it
        # simple for the moment
        echo $disk " " $used " " $avail
done < <(df | awk '/datapool\// {print $1, $3, $4}')

When I run these lines from the command line I get the correct numbers (I have compared the output with zfs list ). But when I run this script via cron, the numbers I get are all doubled.

Let's look at the exact output on my machine:

# Manually
datapool/myshare   4842023944   24758292883

# Via cron
datapool/myshare   9684047889   49516585766

This only happens on FreeBSD. I have another ZFS storage server that runs on Debian and when I run the exact same script there, I get correct numbers all of the time.

I have tried running the cron job as different users (including root), but it's always the same result. I have also set the PATH environment variable to the same value that I have when I run this script manually (instead of the shortened version that cron usually uses).

I have absolutely no clue regarding the cause of this problem and I really don't know how to debug this problem. Plus I couldn't find any information on the internet that is even remotely related to this problem.

I really hope that you can help me out here and ANY hint is greatly appreciated :)

df(1) uses the environment variable BLOCKSIZE to determine which units are used when reporting disk space.

By default, for historical reasons, a block size of 512 is used. It is usual to set this variable within the environment of one's login shell to a more convenient value. For example:

setenv  BLOCKSIZE  "K"

cron(8) runs with a different environment to the login shell. The symptoms you are seeing are consistent with BLOCKSIZE being undefined for cron 's environment, and set to a value of k or 1024 for the environment of the login shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM