Pages

Sunday, October 6, 2013

HIDING BACKGROUND PROCESS OUTPUT: THE /DEV/NULL TRICK

When running a command on a Linux or Unix-like system, the program often produces output:

  1. Standard Output (stdout): Normal results, messages, or requested data (File Descriptor 1).

  2. Standard Error (stderr): Warnings, errors, and diagnostic messages (File Descriptor 2).

When you run a long-running process in the background, this output can clutter your console, even after you move on to other tasks.


THE SOLUTION: REDIRECTING TO /DEV/NULL

To hide all output and keep your terminal clean, you must redirect both stdout and stderr to a special location called /dev/null.

/dev/null is a virtual device known as the "bit bucket" or "black hole." Any data written to it is instantly discarded.

THE STANDARD REDIRECTION COMMAND

The standard, most effective command to silence all output is:

yourcommand > /dev/null 2>&1

How This Works:

  1. > /dev/null: This redirects Standard Output (stdout, or FD 1) to the black hole (/dev/null).

  2. 2>&1: This redirects Standard Error (stderr, or FD 2) to the same place where stdout (FD 1) is currently pointing. Since stdout is pointing at /dev/null, stderr is also sent there and silenced.


RUNNING IN THE BACKGROUND (THE FULL COMMAND)

To combine output hiding with background execution, you simply add the ampersand (&) at the end.

yourcommand > /dev/null 2>&1 &

This command accomplishes two things:

  1. Silences All Output: (> /dev/null 2>&1)

  2. Sends to Background: (&) - It immediately returns control of the terminal to you, allowing the process to run independently.


PERSISTENT BACKGROUND EXECUTION (NOHUP)

If you are running a command in the background and want it to continue running even after you log out or close your terminal session (a common need for server-side tasks), use nohup.

The Command for Persistent, Silent Background Task:

nohup yourcommand > /dev/null 2>&1 &

How This Works:

  • nohup: Ensures the process ignores the "hang up" signal sent when a terminal is closed.

  • > /dev/null 2>&1: Silences all output (otherwise, nohup would create a file called nohup.out).

  • &: Runs the entire sequence in the background.

This is the ideal way to run a Linux application in the background that you expect to survive your session and generate no console noise.

Rsync command

Rsync (Remote Sync) is a powerful utility for copying and synchronizing files and directories, particularly across networked locations. Unlike simple copy commands, Rsync is designed for speed and efficiency by only transferring the parts of files that have actually changed.

This makes it the go-to tool for backups, file migration, and maintaining synchronized mirrors between servers.


THE CORE CONCEPT: DIFFERENTIAL COPYING

The key to Rsync's efficiency is its "delta transfer" algorithm.

  • Checks for Differences: Rsync first compares the files in the source location to the files in the destination location. It does this quickly by comparing file sizes and modification times.

  • Transfers Only the Delta: Instead of copying entire files that have been modified, Rsync calculates the differences (the "delta") within the files. It only transfers those small blocks of changed data, dramatically reducing the amount of network bandwidth and time required.

KEY BENEFIT: RESUMABLE TRANSFERS

One of Rsync's most valuable features is its ability to handle interruptions gracefully:

  • If a large transfer is interrupted (e.g., due to a lost network connection, power failure, or timeout), you can simply re-run the exact same rsync command.

  • Rsync will pick up where it left off, checking files and continuing the sync from the point of interruption, saving you from restarting the entire copy process.


UNDERSTANDING THE COMMAND SYNTAX

The basic Rsync command follows a simple pattern:

rsync [OPTIONS] [SOURCE] [DESTINATION]

EXAMPLE BREAKDOWN

The provided example is a very common and practical use of Rsync:

rsync -av source/public_html/ destination/public_html/

1. The Source and Destination:

  • source/public_html/: The location where the files currently reside (the directory to be copied from).

  • destination/public_html/: The location where the files should be copied to (the target directory).

2. The Options (-av):

The power of Rsync lies in its flags, with -av being the most widely used combination for general synchronization.

  • -a (Archive Mode): This is a shorthand for several important flags (-rlptgoD). It ensures the synchronization is comprehensive and preserves file integrity by:

    • recursing into directories.

    • linking files.

    • preserving permissions.

    • time-stamps (modification times).

    • group ownership.

    • owner (user) ownership.

    • Device files and special files.

  • -v (Verbose): This simply tells Rsync to output detailed information about the files being transferred, giving you confirmation and real-time progress.

Using this command ensures that the files in the destination directory are an exact, efficient, and attribute-preserving mirror of the source directory.

Install Grsecurity on 32 bit OS is : Kernel

Ideal way to install Grsecurity on 32 bit OS is : Just follow the steps given below.

Fetch the sources:

Download kernel from kernel.org

#wget http://www.kernel.org/pub/linux/kernel/v2.6/longterm/v2.6.32/linux-2.6.32.51.tar.gz

Downlaod latest Grsecurity patch from below URL :

#wget http://grsecurity.net/stable/grsecurity-2.2.2-2.6.32.51-201201021326.patch

Extract:
tar xjf linux-2.6.32.51.tar.gz

Patch the kernel:

#cd linux-2.6.32.51

#patch -p1 < ../grsecurity-2.2.2-2.6.32.51-201201021326.patch

Now start making the kernel :

# make clean && make mrproper

Edit your kernel as per your need :

# make menuconfig

Compile your kernel and install it:

# make bzImage

# make modules

# make modules_install

Make sure it’s working ok with the help of following command :

# depmod 2.6.32.51-grsec

Installing and booting the new kernel :

# cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.32.51-grsec

There is also a file called “System.map” that must be copied to the same boot directory.

# cp System.map /boot

Do not forget to make changes in /etc/grub.conf

also go to grub prompt after this and fire below command :

# grub > savedefault –-default=0 –-once

Now reboot server :

#Shutdown -r now.