Breaking News
Home / Sys Admin / Unix / How to redirect output and errors from a process to /dev/null?

How to redirect output and errors from a process to /dev/null?

Unix SystemMost of the times, we run a script (say cron job etc.) we are not interested in the output.  So how do you ignore it, by redirecting it to /dev/null? But what is this /dev/null?

Null device (referenced via /dev/null) which is a special file that discards all data written to it (but reports that the write operation succeeded), and provides no data to any process that reads from it (yielding EOF immediately).

So when you run the script or programs from the command line, you can append >/dev/null to command line, to redirect output to the bit bucket instead.

However, when you run these from a cron job, any output that the script would send to stderr is sent as a mail to the owner of the script, which can be very annoying if the script runs regularly, with output you don’t need or want, or can do anything about.

The graceful way to handle this in any shell is to Redirect the output of stderr to stdout, and then redirect this combined output to /dev/null:

./ >/dev/null 2>&1

Here is the magic as follows:

   1. We send standard output to /dev/null, using > /dev/null in the first part.
   2. In the 2nd part, 2>&1 ensures that we redirect the standard error (file descriptor 2), to wherever standard output (file descriptor 1) is going, which is, as already established, to the bit bucket.


Check Also

How to list disks information in Linux?

In order to check the disk in the system we can use the List Hardware …

Advertisment ad adsense adlogger