Unix Wizdumb: Clean up empty "in use" files with "cat /dev/null"

By Sandra Henry-Stocker  2 comments

This kind of problem can occur because processes can create and hold an open file descriptor, preventing the file's inode from being relinquished and the associated disk space freed up. If you can't kill the process, you might be stuck until you can -- even if that means putting up with scrolling errors that make it nearly impossible to type commands on the console.

A better way to proceed when you notice a file is hogging disk space is to cat /dev/null to the file. This will generally free the space immediately while not requiring the process to be killed and restarted.

The syslog file, for example, is held open by the syslog daemon, syslogd. If this file gets so large that the /var partition on your system is choking, you won't have much luck releasing the space it occupies with an rm command, but you can always cat /dev/null to the file to regain the space. Other files that might benefit from the same procedure include auth_log, messages, sulog and wtmpx.

If prior syslog files (i.e., syslog.0 through syslog.7) exist and are correspondingly large, you should clearly start with these files. Since they are historical "rolled over" files, syslogd is no longer using them and they can be removed without side effects.

# lsof syslog
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
syslogd  137 root    8w  VREG  85,44        0 18059 syslog

Here we have an example of removing a wtmpx file that has been ignored and has grown far too big over the span of several years. Notice that nearly 300 MB is recovered. That's a lot in a file system that's only 1 GB in size.

# lsof wtmpx
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
utmpd   235 root    3r  VREG  85,44     1488 7760 wtmpx
# df -k .
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d44      1021735  607960  352471    64%    /var
# cat /dev/null > wtmpx
# df -k .
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d44      1021735  323688  636743    34%    /var

Now let's do it the wrong way on a similar system. This time we remove the file and no change in the available (or used) space is evident.

# lsof wtmpx
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
utmpd   230 root    3r  VREG  85,44   493644 7760 wtmpx
# df -k .
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d44      1021735  311676  648755    33%    /var
# rm wtmpx
# df -k .
Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d44      1021735  311676  648755    33%    /var

If a file isn't being held open by a process, the lsof command will not generate any output as shown in the example below.

# lsof /var/log/syslog.?
# lsof /var/adm/lastlog
# ls /var/adm/mess*
/var/adm/messages    /var/adm/messages.1  /var/adm/messages.3
/var/adm/messages.0  /var/adm/messages.2
# lsof /var/adm/mess*
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF  NODE NAME
syslogd 241 root    9w  VREG   32,0        0 14659 /var/adm/messages

Of all the files above, only the current messages file is open. The others can be removed with impunity. The messages files is better removed using /dev/null.

2 comments

    Anonymous 2 years ago
    The other reason to truncate is to avoid losing any permissions or ownership that may have been changed from default.The "cat /dev/null >" comes from folks who learned on csh (because it will complain "ambiguous redirect" if you just ">").
    Anonymous 2 years ago
    like most, I learn't this the hard way many years ago.even easier is :# > wtmpx :-)

      Add a comment

      Post a comment using one of these accounts
      Or join now
      At least 6 characters

      Note: Comment will appear soon after you have activated your account.
      Obscene/spam comments will be removed and accounts suspended.
      The information you submit is subject to our Privacy Policy and Terms of Service.

      ITworld LIVE

      IT Management/StrategyWhite Papers & Webcasts

      White Paper

      Top Solutions and Tools to Prevent Devastating Malware

      Custom malware frequently goes undetected. According to Forrester Research, the best way to reduce risk of breach is to deploy file integrity monitoring (FIM) tools that provide immediate alerts. This white paper has been brought to you by NetIQ, the leader in solving complex IT challenges.

      White Paper

      Ten Steps to an Enterprise Mobility Strategy

      Enterprise employees are more mobile, relishing the ability to work productively anywhere, at any time. They may use any means to get connected, often creating financial and security risks for your company. Discover how to get control of your enterprise mobility strategy and ensure mobile worker productivity with these ten steps.

      White Paper

      What You Need to Know About the Costs of Mobility

      Mobile workers want to get connected anywhere, at any time, often at any cost. Enterprise mobility is often a hidden "black" budget in your company. Ensure that your traveling employees are productive everywhere, even while you control cost and security, through an enterprise mobility strategy.

      White Paper

      The 2011 iPass Mobile Enterprise Report

      This industry survey covers trends, recommendations and a policy guide on managing Enterprise Mobility for IT management and CIOs. Get data on employee device liability, as well as smartphone/tablet penetration, budget control and provisioning. Find out how your organization compares, how to ensure mobile worker productivity, and control costs.

      Webcast On Demand

      Customer Spotlight: How IPC The Hospitalist Company Implemented Oracle on VMware

      Have you been looking to hear about customer's experiences with the new VMware vCenter Site Recovery Manager product? View this webcast to learn about VMware customer, Navicure, and their experiences testing and evaluating the recovery manager, their progress in implementing it in their environment and their advice other customers considering using vCenter.

      Sponsor: VMware

      See more White Papers | Webcasts

      Ask a question

      Ask a Question