Yearly Archive 18 November 2017

How to Install and Enable Bash Auto Completion in CentOS/RHEL

Crontab – Quick Reference

Setting up cron jobs in Unix, Solaris & Linux

cron is a unix, solaris, Linux utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. 

cron meaning – There is no definitive explanation but most accepted answers is reportdly from Ken Thompson ( author of unix cron ), name cron comes from chron ,the Greek prefix for ‘time.’.
What is cron ? – Cron is a daemon which runs at the times of system boot from /etc/init.d scripts. If needed it can be stopped/started/restart using init script or with command service crond start in Linux systems.

This document covers following aspects of Unix, Linux cron jobs to help you understand and implement cronjobs successfully

  1. What is crontab?
  2. What is a cron job or cron schedule?
  3. Crontab Restrictions
  4. Crontab Commands
  5. Crontab file – syntax
  6. Crontab Example
  7. Crontab Environment
  8. Disable Email
  9. Generate log file for crontab activity
  10. Crontab file location

1. What is crontab?

Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times. File location varies by operating systems, See Crontab file location at the end of this document.

2.What is a cron job or cron schedule?

Cron job or cron schedule is a specific set of execution instructions specifing day, time and command to execute. crontab can have multiple execution statments.

3. Crontab Restrictions

You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

4. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e    Edit crontab file, or create one if it doesn’t already exist.
crontab -l    crontab list of cronjobs , display crontab file contents.
crontab -r    Remove your crontab file.
crontab -v    Display the last time you edited your crontab file. (This option is only available on a few systems.)

5. Crontab file

Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.

B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .

6. Crontab Examples

A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.

30     18     *     *     *         rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :

min hour day/month month day/week Execution time
30 0 1 1,6,12 * — 00:30 Hrs  on 1st of Jan, June & Dec.
0 20 * 10 1-5 –8.00 PM every weekday (Mon-Fri) only in Oct.
0 0 1,10,15 * * — midnight on 1st ,10th & 15th of month
5,10 0 10 * 1 — At 12.05,12.10 every Monday & on 10th of every month
:

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

7. Crontab Environment

cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

8. Disable Email

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .

>/dev/null 2>&1

9. Generate log file

To collect the cron execution execution log in a file :

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log

10. Crontab file location

User crontab files are stored by the login names in different locations in different Unix and Linux flavors. These files are useful for backing up, viewing and restoring but should be edited only with crontab command by the users.

  • Mac OS X
    /usr/lib/cron/tabs/
  • BSD Unix 
    /var/cron/tabs/
  • Solaris, HP-UX, Debian, Ubuntu
    /var/spool/cron/crontabs/
  • AIX, Red Hat Linux, CentOS, Ferdora
    /var/spool/cron/

Modify Ubuntu ens network interface card to eth0

  1. Display current interface cards and verify that eth0 doesnt exist:
    ifconfig -a
  2. Edit grub file configuration to disable new naming convention
    vim /etc/default/grub
  3. Look for GRUB_CMDLINE_LINUX= include the following net.ifnames=0 biosdevname=0″
    From:
    GRUB_CMDLINE_LINUX=””
    To
    GRUB_CMDLINE_LINUX=”net.ifnames=0 biosdevname=0″
  4. Save and exit the file
  5. Reload grub config file:
    update-grub
  6. Edit interfaces file
    vim /etc/network/interfaces
    From:
    # The primary network interface
    auto ens16
    iface ens16 inet dhcpTo:
    # The primary network interface
    auto eth0
    iface eth0 inet dhcp
  7. Reboot your machine/Restart networking service

3 Linux Network Monitors

Learn more about your network connections with the iftop, Nethogs, and vnstat tools.

You can learn an amazing amount of information about your network connections with these three glorious Linux networking commands. iftop tracks network connections by process number, Nethogs quickly reveals what is hogging your bandwidth, and vnstat runs as a nice lightweight daemon to record your usage over time.

iftop

The excellent iftop listens to the network interface that you specify, and displays connections in a top-style interface.

This is a great little tool for quickly identifying hogs, measuring speed, and also to maintain a running total of your network traffic.

If you have just one network interface, run iftop with no options. iftop requires root permissions:

$ sudo iftop

When you have more than one, specify the interface you want to monitor:

$ sudo iftop -i wlan0

Just like top, you can change the display options while it is running.

  • h toggles the help screen.
  • n toggles name resolution.
  • s toggles source host display, and d toggles the destination hosts.
  • s toggles port numbers.
  • N toggles port resolution; to see all port numbers toggle resolution off.
  • t toggles the text interface. The default display requires ncurses. I think the text display is more readable and better-organized (Figure 1).
  • p pauses the display.
  • q quits the program.

text display

Figure 1: The text display is readable and organized.
When you toggle the display options, iftop continues to measure all traffic. You can also select a single host to monitor. You need the host’s IP address and netmask. I was curious how much of a load Pandora put on my sad little meager bandwidth cap, so first I used dig to find their IP address:
$ dig A pandora.com
[...]
;; ANSWER SECTION:
pandora.com.            267     IN      A       208.85.40.20
pandora.com.            267     IN      A       208.85.40.50

 

$ sudo iftop -F 208.85.40.20/24 -i wlan0

Is that not seriously groovy? I was surprised to learn that Pandora is easy on my precious bits, using around 500Kb per hour. And, like most streaming services, Pandora’s traffic comes in spurts and relies on caching to smooth out the lumps and bumps.

You can do the same with IPv6 addresses, using the -G option. Consult the fine man page to learn the rest of iftop’s features, including customizing your default options with a personal configuration file, and applying custom filters (see PCAP-FILTER for a filter reference).

Nethogs

When you want to quickly learn who is sucking up your bandwidth, Nethogs is fast and easy. Run it as root and specify the interface to listen on. It displays the hoggy application and the process number, so that you may kill it if you so desire:

$ sudo nethogs wlan0

NetHogs version 0.8.1

PID USER   PROGRAM              DEV    SENT   RECEIVED       
7690 carla /usr/lib/firefox     wlan0 12.494 556.580 KB/sec
5648 carla .../chromium-browser wlan0  0.052   0.038 KB/sec
TOTAL                                 12.546 556.618 KB/sec 

Nethogs has few options: cycling between kb/s, kb, b, and mb, sorting by received or sent packets, and adjusting the delay between refreshes. See man nethogs, or run nethogs -h.

vnstat

vnstat is the easiest network data collector to use. It is lightweight and does not need root permissions. It runs as a daemon and records your network statistics over time. The vnstatcommand displays the accumulated data:

$ vnstat -i wlan0
Database updated: Tue Oct 17 08:36:38 2017

   wlan0 since 10/17/2017

          rx:  45.27 MiB      tx:  3.77 MiB      total:  49.04 MiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Oct '17     45.27 MiB |    3.77 MiB |   49.04 MiB |    0.28 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated        85 MiB |       5 MiB |      90 MiB |

   daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
         today     45.27 MiB |    3.77 MiB |   49.04 MiB |   12.96 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated       125 MiB |       8 MiB |     133 MiB |

By default it displays all network interfaces. Use the -i option to select a single interface. Merge the data of multiple interfaces this way:

$ vnstat -i wlan0+eth0+eth1

You can filter the display in several ways:

  • -h displays statistics by hours.
  • -d displays statistics by days.
  • -w and -m displays statistics by weeks and months.
  • Watch live updates with the -l option.

This command deletes the database for wlan1 and stops watching it:

$ vnstat -i wlan1 --delete

This command creates an alias for a network interface. This example uses one of the weird interface names from Ubuntu 16.04:

$ vnstat -u -i enp0s25 --nick eth0

By default vnstat monitors eth0. You can change this in /etc/vnstat.conf, or create your own personal configuration file in your home directory. See man vnstat for a complete reference.

You can also install vnstati to create simple, colored graphs (Figure 2):

$ vnstati -s -i wlx7cdd90a0a1c2 -o vnstat.png

vnstati

Figure 2: You can create simple colored graphs with vnstati.

See man vnstati for complete options.

Linux Rename Eth0 Network Interfaces

The best way to rename Ethernet devices is through udev. It is the device manager for the Linux kernel. Primarily, it manages device nodes in /dev. It is the successor of devfs and hotplug, which means that it handles /dev directory and all user space actions when adding/removing devices, including firmware load.

The order of the network interfaces may be unpredictable under certain configurations. Between reboots it usually stays the same, but often after an upgrade to a new kernel or the addition or replacement of a network card (NIC) the order of all network interfaces changes. For example, what used to be rl0 now becomes wlan0 or what used to be eth0 now becoems eth2 or visa versa.

Step #1: Find out the MAC address of the Ethernet device

Type the following command:
# ifconfig -a | grep -i --color hwaddr
Sample outputs:

eth0      Link encap:Ethernet  HWaddr b8:ac:6f:65:31:e5  
pan0      Link encap:Ethernet  HWaddr 4a:71:40:ed:5d:99  
vmnet1    Link encap:Ethernet  HWaddr 00:50:56:c0:00:01  
vmnet8    Link encap:Ethernet  HWaddr 00:50:56:c0:00:08  
wlan0     Link encap:Ethernet  HWaddr 00:21:6a:ca:9b:10 

Note down the MAC address.

Step #2: Rename eth0 as wan0

To rename eth0 as wan0, edit a file called 70-persistent-net.rules in /etc/udev/rules.d/directory, enter:
# vi /etc/udev/rules.d/70-persistent-net.rules
The names of the Ethernet devices are listed in this file as follows:

# PCI device 0x14e4:0x1680 (tg3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="b8:ac:6f:65:31:e5", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

Locate and identify the line with the NIC from step 1 (look for the MAC address). It may look like above. In this example, the interface eth0 will be renamed to wan0 (change NAME="eth0" to NAME="wan0"):

# PCI device 0x14e4:0x1680 (tg3)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="b8:ac:6f:65:31:e5", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="wan0"

Save and close the file. Reboot the system to test changes:
# reboot
Verify new settings:
# ifconfig -a
# ifconfig wan0
# ifconfig -a | less
# ip addr show

ASUS P8H61-M LX mainboard

P8H61-M LX

REV 3.0 – New H61 B3 Revision
The most cost-effective Intel® H61

choice with super-friendly UEFI BIOS

  • UEFI BIOS (EZ Mode) – Flexible & Easy BIOS Interface
  • AI Suite II – One-stop Access to Innovative ASUS Features
  • EPU – Energy Efficiency All Around
  • Anti Surge – Full-time Power Guardian-Make System Free From Risk
  • CPU
    Intel® Socket 1155 for 2nd Generation Core™ i7/Core™ i5/Core™ i3 Processors
    Supports Intel® 32 nm CPU
    Supports Intel® Turbo Boost Technology 2.0
    * The Intel® Turbo Boost Technology 2.0 support depends on the CPU types.
    * Refer to www.asus.com for CPU support list
  • Chipset
    Intel® H61(B3)
  • Memory
    2 x DIMM, Max. 16GB, DDR3 1333/1066 Hz Non-ECC, Un-buffered Memory
    Dual Channel Memory Architecture
    Supports Intel® Extreme Memory Profile (XMP)
    * The maximum 16GB memory capacity can be supported with 8GB or above DIMMs.
  • ASUS will update the memory QVL once the DIMMs are available in the market.
    * Refer to www.asus.com or user manual for the Memory QVL (Qualified Vendors Lists).
    * Due to OS limitation, when installing total memory of 4GB capacity or more,
    Windows® 32-bit operation system may only recognize less than 3GB.
    Install a 64-bit Windows® OS when you want to install 4GB or more memory
    on the motherboard.
  • Graphic
    Integrated Graphics Processor
    VGA output support : RGB port
    – Supports RGB with max. resolution 2048 x 1536 @ 75 Hz
    Supports DirectX 10.1
  • Expansion Slots
    1 x PCIe 2.0 x16
    3 x PCIe 2.0 x1
  • Storage
    Intel® H61(B3) chipset : 
    4 x SATA 3Gb/s port(s), blue
  • LAN
    Realtek® 8111E , 1 x Gigabit LAN Controller(s)
  • Audio
    Realtek® ALC 887 8-Channel High Definition Audio CODEC *1
  • USB Ports
    Intel® H61(B3) chipset :
    10 x USB 2.0 port(s) (4 at back panel, black, 6 at mid-board)
  • ROG Exclusive Features
    Overclocking Protection :
    – ASUS C.P.R.(CPU Parameter Recall)
  • Special Features
    ASUS EPU :
    – EPU
    ASUS Exclusive Features :
    – AI Suite II
    – Ai Charger
    – Anti-Surge
    – Low EMI Solution
    – ESD Guards
    – ASUS UEFI BIOS EZ Mode featuring friendly graphics user interface
    ASUS Quiet Thermal Solution :
    – Stylish Fanless Design Heat-sink solution
    – ASUS Fan Xpert
    ASUS EZ DIY :
    – ASUS CrashFree BIOS 3
    – ASUS EZ Flash 2
    – ASUS MyLogo 2
  • Back I/O Ports
    1 x PS/2 keyboard (purple)
    1 x PS/2 mouse (green)
    1 x D-Sub
    1 x LAN (RJ45) port(s)
    4 x USB 2.0
    1 x COM port(s)
    1 x Parallel port(s)
    3 x Audio jack(s)
  • Internal I/O Ports
    3 x USB 2.0 connector(s) support(s) additional 6 USB 2.0 port(s)
    4 x SATA 3Gb/s connector(s)
    1 x CPU Fan connector(s)
    1 x Chassis Fan connector(s)
    1 x S/PDIF out header(s)
    1 x 24-pin EATX Power connector(s)
    1 x 4-pin ATX 12V Power connector(s)
    1 x Front panel audio connector(s) (AAFP)
    1 x Internal speaker connector(s)
    1 x System panel(s)
  • Accessories
    User’s manual
    I/O Shield
    2 x SATA 3Gb/s cable(s)
  • BIOS
    32 Mb Flash ROM, UEFI BIOS, PnP, DMI v2.0, WfM2.0, SMBIOS v2.6,
    ACPI v2.0a, Multi-language BIOS
  • Support Disc
    Drivers
    ASUS Utilities
    ASUS Update
    Anti-virus software (OEM version)
  • Form Factor
    uATX Form Factor
    9.6 inch x 7.6 inch ( 24.4 cm x 19.3 cm )
  • Note
    *1: Use a chassis with HD audio module in the front panel to support
     an 8-channel audio output.