UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
Data that is sent to standard out can be discarded when sent to the character device null.
Syntax:
command > /dev/null program > /dev/null cat filename > /dev/null cp filename /dev/null ln -s /dev/null file
null discards all data passed to it. The most popular way to redirect data to null is by using the greater than sign: >. There are other ways to get rid of data using null without using the redirect to pass the data to it. One way is to create a soft link to the device or copy a file to the device. It is also possible to alias incoming mail to be sent to the device. Here is how some of these methods work.
Flavors: AT&T, BSD
Shells: All
$ ls -l /etc | tee /tmp/root.txt > /dev/null
This command takes a listing of the /etc directory, saves it into the file /tmp/root.txt, and sends what would normally be displayed to standard out, where it gets discarded and is never seen.
Flavors: AT&T, BSD
Shells: All
$ /usr/local/bin/build_report.sh < /dev/null
The output generated by programs might be unnecessary at times. This can include errors, debugging information, or an excessive amount of data that is of no concern. This type of data is best redirected to /dev/null.
Flavors: AT&T, BSD
Shells: All
# cat /dev/null > /usr/adm/error.log
This is one of the safest ways to zero a file out. Because there are no contents in null (zero), you can make the file have a size of zero without closing the file.
This is important because if the file is currently in an open state by a process, the process remains unaffected but the file ends up with file size equal to zero. This technique works very well when it comes time to zero out the files in the /var/adm directory.
Flavors: AT&T and BSD
Shells: All
# cp /dev/null /usr/local/app/error.log
This use of the copy command has the same affect as example three and the cat redirect method.
Flavors: AT&T, BSD
Shells: All
# rm /usr/local/app/error.log # ln -s /dev/null /usr/local/app/error.log
If there are log files that you don't want to have to deal with time and time again, remove them and create a soft link from the file to null. When this is in place, example three will have to be done on a routine basis, because otherwise it fills up the disk. Make sure that no processes have the file open when it is removed. It is not advisable to use this technique on any of the files within the directories /var/adm, /usr/adm, /var/spool, and /var/spool.
Flavors: AT&T, BSD
Shells: All
An alias entry in the /etc/aliases file can redirect incoming mail to null.
# vi /etc/aliases
Step 1: Edit the /etc/aliases file.
# ident @(#)aliases 1.13 92/07/14 # >>>>>>>> The program "newaliases" will have to be run after # > NOTE > this file is updated for any changes to # >>>>>>>> show through to sendmail nobody: /dev/null
Step 2: Make the necessary changes to the file.
Step 3: Run newaliases (Always run newaliases, it tells mail to reread this file)
This is one way to disable users' email access while maintaining their accounts' activity. Another good use for this technique is for controlling inbound spam. You can redirect spam to null and never worry about it taking up disk space on your system.
Flavors: AT&T, BSD
Shells: All
0 22 * * * /bin/sh /usr/local/bin/backup_full.sh 2>&1 > /dev/null
The previous example is a crontab entry that runs a scheduled full backup in which cron redirects any errors or messages to null. A crontab is one of the best uses for redirecting output to null. If any output by a process is executed by a cron job, that output is sent to the user who owns the scheduled cron job. When null is set up for the command in the crontab, whatever would be sent to standard out and emailed to the user is discarded.
By now the many uses for using /dev/null should be clear. Whether it's to zero a file or to get rid of unwanted data, null can have a wide range of uses for a UNIX administrator. It keeps your mailbox free of worthless mail, keeps your log files under control, and zeroes important files safely.
In my early years as an administrator, database applications were locked to open log files. These files would grow to enormous sizes and fill up a filesystem in a matter of days. By simply removing log files that were open by the database applications with the remove ( rm) command, I caused the application to crash rather severely. I learned quickly that redirecting null using the cat command kept the DBAs (database administrators) away from my desk, and the dirty looks stopped.
One of the most annoying things is to find out that your filesystem has run out of disk space because a log file grew over 200MB during the weekend while you were out. Clear these logs out by redirecting them to /dev/null.
Man pages:
aliases, null, cron, crontab
UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.