du command to be used in shell script without affecting the server load
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
du command to be used in shell script without affecting the server load
Hi,
I need to use du command in a shell script to monitor the disk usage on nearly 1000+ servers. We are doing this via cfengine. I used du command in the script(which is run by a cron)to find the size of the directories used by the users. But my manager suggests that using du command can affect the system load since there are too many servers involved here. What can I do to run this script without affecting the system load? Is there any command like sleep() to be added in script to resolve this?
I don't know much about cfengine, but surely the du cmd is running on the target nodes anyway?
If you're worried about the overhead of returning the results to the master (if that's what you are doing), consider using snmp instead - it may use less.
would be nice to know what is the goal, probably df can be used instead, or other tools. Are they networked filesystems? (nfs, or similar)? Probably you need to run it only on one single host....
Here i am trying to find the size of the directories(which is actually the users workspace) inside a filesystem. So df will not help.
First i checked filesystem space(using df) and if the usage is more than 50, i tried finding the directories which takes more space in that filesystem. That is the goal.
It is not an nfs. How can i use nice and snmp in this case? I was told to do a random check with du so that it doesn't affect the system load.
du needs to walk through the filesystem to calculate usage, and that is always going to cause high I/O load on the server. You could get a similar report by enabling quotas and giving each user a really big quota -- perhaps larger than the filesystem so that the quota never gets in the way. Then you could use repquota to get a quick report of how much space each user was using. Note that this will not be by directory, just a total per filesystem for each user.
hi thank you @rknichols .. but this doesn't concern users home dir.. We already have this feature enabled and quota check is done which is a different task. This concerns only the directories inside a filesystem, which are the workspaces for the users.
Perhaps you could give an example (made up names if you feel paranoid).
However, quotas per user are ALL files per user if enabled on each fs. IE not just homedirs.
How frequently do you need to do this per user / per fs ? EG once a day shouldn't hurt anyone, especially if done out of hrs.
hi thank you @rknichols .. but this doesn't concern users home dir.. We already have this feature enabled and quota check is done which is a different task. This concerns only the directories inside a filesystem, which are the workspaces for the users.
Disk quotas are kept per filesystem, so unless the workspaces are in the same filesystem as the home directories, ... .
Unfortunately disk quota will not work here. Can i sort the directories based on size without using du command? is it possible?
The thing is, we have this large filesystem of TB/ZB size with so many directories and i need to find the directories which are using more space(i.e) more than a particular size limit per user(which has to be calculated from the Size of df command). du is the issue here.
probably you can find other ways, but it looks like du is the direct way. Actually you can try ls -lR and parse the output (and other similar tricks as well).
If the dirs you are interested in are top-of-partition dirs, then snmp could be an option - otherwise not.
Certainly, du is the conventional way to check space on a per dir basis. If you are really worried about potential performance issues, consider running at low priority (ie use the 'nice' cmd) and/or run du out of hrs.
As pan64 points out, just listing everything out and then calculating (possibly on another node) is another option.
Incidentally, instead of 'l' use 's' if you only need the sizes & names (ie not owners/perms/dates etc)
I need some other info about the dir as well. So I am thinking to go with "nice"command. Can anyone help me with the command inside my script? I am not so familiar with using nice command.
Below is the du command I use in script:
The problem with using the nice command here is that the major impact from du is I/O activity, and, when a process wakes up after waiting on a disk I/O operation, it starts running at a fixed priority regardless of its CPU usage history or "nice" setting. I doubt you'll see much difference between running
Code:
RETVAL=$(du -sk --time --time-style=+s "$1")
and
Code:
RETVAL=$(nice -19 du -sk --time --time-style=+%s "$1")
Note also that it's hard to test this. If you re-run the same du command while all of the needed inodes are still cached in memory, it runs almost instantaneously with little or no I/O activity.
Actually you can try ls -lR and parse the output (and other similar tricks as well).
This is a really good suggestion.
I just ran some strace tests on a small directory structure (85G, 3500 files), and du was consistently issuing around 15 times the number of fstat calls as ls - and taking more than 10 times as much CPU time (not much in my case).
I did several runs to ensure all the data was in cache, and the numbers consistent. The ls output has the dir name on one line and the total on the next - easy enough to parse in awk, perl, whatever.
Not wanting to derail the thread, just adding some support to pan64 idea.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.