Friday 22 February 2019

cron command in Linux with Examples

Cron Command, Linux Tutorial and Material, Linux Certifications, Linux Study Material

The cron is a software utility, offered by Linux-like operating system which automates the scheduled task at a predetermined time. It is a daemon process, which runs as a background process and performs the specified operations at the predefined time when a certain event or condition is triggered without the intervention of a user. To deal with a repeated task frequently is an intimidating task for the system administrator and thus he can schedule such processes to run automatically in the background at regular interval of time by creating a list of those commands using cron. It enables the users to execute the scheduled task on a regular basis unobtrusively like doing the backup every day at midnight, scheduling updates on a weekly basis, synchronizing the files at some regular interval. Cron checks for the scheduled job recurrently and when the scheduled time fields match the current time fields, the scheduled commands are executed. It is started automatically from /etc/init.d on entering multi-user run levels.

Syntax:


cron [-f] [-l] [-L loglevel]


Options:


◈ -f : Used to stay in foreground mode, and don’t daemonize.
◈ -l : This will enable the LSB compliant names for /etc/cron.d files.
◈ -n : Used to add the FQDN in the subject when sending mails.
◈ -L loglevel : This option will tell the cron what to log about the jobs with the following values:
    ◈ 1 : It will log the start of all cron jobs.
    ◈ 2 : It will log the end of all cron jobs.
    ◈ 4 : It will log all the failed jobs. Here the exit status will not equal to zero.
    ◈ 8 : It will log the process number of all the cron jobs.

The crontab (abbreviation for “cron table”) is list of commands to execute the scheduled tasks at specific time. It allows the user to add, remove or modify the scheduled tasks. The crontab command syntax has six fields separated by space where the first five represent the time to run the task and the last one is for the command.

◈ Minute (holds a value between 0-59)
◈ Hour (holds value between 0-23)
◈ Day of Month (holds value between 1-31)
◈ Month of the year (holds a value between 1-12 or Jan-Dec, the first three letters of the month’s name shall be used)
◈ Day of the week (holds a value between 0-6 or Sun-Sat, here also first three letters of the day shall be used)
◈ Command

The rules which govern the format of date and time field as follows:

◈ When any of the first five fields are set to an asterisk(*), it stands for all the values of the field. For instance, to execute a command daily, we can put an asterisk(*) in the week’s field.
◈ One can also use a range of numbers, separated with a hyphen(-) in the time and date field to include more than one contiguous value but not all the values of the field. For example, we can use the 7-10 to run a command from July to October.
◈ The comma (, ) operator is used to include a list of numbers which may or may not be consecutive. For example, “1, 3, 5” in the weeks’ field signifies execution of a command every Monday, Wednesday, and Friday.
◈ A slash character(/) is included to skip given number of values. For instance, “*/4” in the hour’s field specifies ‘every 4 hours’ which is equivalent to 0, 4, 8, 12, 16, 20.


Permitting users to run cron jobs:


◈ The user must be listed in this file to be able to run cron jobs if the file exists.

/etc/cron.allow

◈ If the cron.allow file doesn’t exist but the cron.deny file exists, then a user must not be listed in this file to be able to run the cron job.

/etc/cron.deny

Note: If neither of these files exists then only the superuser(system administrator) will be allowed to use a given command.

Sample commands:


◈ Run /home/folder/gfg-code.sh every hour, from 9:00 AM to 6:00 PM, everyday.
00 09-18 * * * /home/folder/gfg-code.sh
◈ Run /usr/local/bin/backup at 11:30 PM, every weekday.
30 23 * * Mon, Tue, Wed, Thu, Fri /usr/local/bin/backup
◈ Run sample-command.sh at 07:30, 09:30, 13:30 and 15:30.
30 07, 09, 13, 15 * * * sample-command.sh

Tuesday 19 February 2019

date command in Linux with examples

date command, Linux Tutorial and Material, Linux Certifications, Linux Study Materials

date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.

Syntax:


date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

Options with Examples


1: date (no option) : With no options, the date command displays the current date and time, including the abbreviated day name, abbreviated month name, day of the month, the time separated by colons, the time zone name, and the year.

Command: 
$date
Output: 
Tue Oct 10 22:55:01 PDT 2017
Note : Here unix system is configured in pacific daylight time.

2: -u Option: Displays the time in GMT(Greenwich Mean Time)/UTC(Coordinated Universal Time )time zone.

Command: 
$date -u
Output :
Wed Oct 11 06:11:31 UTC 2017

3: –date or -d Option: Displays the given date string in the format of date. But this will not affect the system’s actual date and time value.Rather it uses the date and time given in the form of string.

Syntax:

$date --date=" string "

Command: 
$date --date="2/02/2010"
$date --date="Feb 2 2010"
Output: 
Tue Feb  2 00:00:00 PST 2010
Tue Feb  2 00:00:00 PST 2010

4: Using –date option for displaying past dates:

◈ Date and time of 2 years ago.

Command: 
$date --date="2 year ago"
Output: 
Sat Oct 10 23:42:15 PDT 2015

◈ Date and time of 5 seconds ago.

Command: 
$date --date="5 sec ago"
Output: 
Tue Oct 10 23:45:02 PDT 2017

◈ Date and time of previous day.

Command:
$date --date="yesterday"
Output: 
Mon Oct  9 23:48:00 PDT 2017

◈ Date and time of 2 months ago.

Command: 
$date --date="2 month ago"
Output: 
Thu Aug 10 23:54:51 PDT 2017

◈ Date and time of 10 days ago.

Command: 
$date --date="10 day ago"
Output: 
Sat Sep 30 23:56:55 PDT 2017

5: Using –date option for displaying future date:

◈ Date and time of upcoming particular week day.

Command: 
$date --date="next tue"
Output: 
Tue Oct 17 00:00:00 PDT 2017

◈ Date and time after two days.

Command: 
$date --date="2 day"
Output: 
Fri Oct 13 00:05:52 PDT 2017

◈ Date and time of next day.

Command: 
$date --date="tomorrow"
Output: 
Thu Oct 12 00:08:47 PDT 2017

◈ Date and time after 1 year on the current day.

Command: 
$date --date="1 year"
Output:
Thu Oct 11 00:11:38 PDT 2018

6: -s or –set Option: To set the system date and time -s or –set option is used.

Syntax:

$date --set="date to be set"

Command:
$date
Output: 
Wed Oct 11 15:23:26 PDT 2017
Command: 
$date --set="Tue Nov 13 15:23:34 PDT 2018"
$date
Output: 
Tue Nov 13 15:23:34 PDT 2018

7: –file or -f Option: This is used to display the date string present at each line of file in the date and time format.This option is similar to –date option but the only difference is that in –date we can only give one date string but in a file we can give multiple date strings at each line.

Syntax:

$date --file=file.txt

$cat >> datefile
 Sep 23 2018
 Nov 03 2019
Command:
$date --file=datefile
Output:
Sun Sep 23 00:00:00 PDT 2018
Sun Nov  3 00:00:00 PDT 2019

8: -r Option: This is used to display the last modified timestamp of a datefile .

Syntax:

$date -r file.txt

We can modify the timestamp of a datefile by using touch command.

$touch datefile

$date
Wed Oct 11 15:54:18 PDT 2017
//this is the current date and time
$touch datefile
//The timestamp of datefile is changed using touch command.
This was done few seconds after the above date command’s output.
$date
Wed Oct 11 15:56:23 PDT 2017
//display last modified time of datefile

9: List of Format specifiers used with date command:

%D: Display date as mm/dd/yy.     
%d: Display the day of the month (01 to 31).     
%a: Displays the abbreviated name for weekdays (Sun to Sat).
%A: Displays full weekdays (Sunday to Saturday).
%h: Displays abbreviated month name (Jan to Dec).
%b: Displays abbreviated month name (Jan to Dec).
%B: Displays full month name(January to December).
%m: Displays the month of year (01 to 12).
%y: Displays last two digits of the year(00 to 99).
%Y: Display four-digit year.
%T: Display the time in 24 hour format as HH:MM:SS.
%H: Display the hour.
%M: Display the minute.
%S: Display the seconds.

Syntax:

$date +%[format-option]

Examples:

Command: 
$date "+%D"
Output: 
10/11/17
Command: 
$date "+%D %T"
Output: 
10/11/17 16:13:27
Command: 
$date "+%Y-%m-%d"
Output: 
2017-10-11
Command:
$date "+%Y/%m/%d"
Output: 
2017/10/11
Command: 
$date "+%A %B %d %T %y"
Output: 
Thursday October 07:54:29 17

Sunday 17 February 2019

basename Command in Linux with examples

Linux Tutorial and Material, Linux Guides, Linux Certifications, Linux Study Materials

basename strips directory information and suffixes from file names i.e. it prints the file name NAME with any leading directory components removed.

Using basename command :


The basename command can be easily used by simply writing basename followed by the file name or the full pathname.

Syntax of basename command :


$basename NAME [SUFFIX]
        or
$basename OPTION NAME

where, NAME refers to the file name or file full pathname and OPTION refers to the options compatible with the basename command and in case, if you want to strip off the suffix of a file you can give the filename followed by the SUFFIX name you want to get rid off.

Example :

/* basename removes directory path and
   returns the file name, kt */
$basename /usr/local/bin/kt
kt

In the first case, the basename command removes the directories name from the full path name given of file kt and in the second case basename removes the suffix .html from kt.html.

Options for basename command :


◈ -a, – -multiple option : This option lets you support multiple arguments and treat each as a NAME i.e you can give multiple file names or full path names with the use of -a option.
/* -a is used for multiple inputs */
$basename -a /usr/local/bin/kt /usr/local/bin/kt.html
kt
kt.html

◈ -s, – -suffix = SUFFIX option : This option removes a trailing suffix SUFFIX,such as a file extension.

/*.html suffix is removed from kt.html
   when followed by -s option */
$basename -s .html kt.html
kt

◈ -z option : This separates the output with NULL rather than a newline.

/* The outputs of the two arguments passed
   are not separated by a newline*/
$basename -az /usr/local/bin/kt kt.html
ktkt

◈ – -help option : This displays the help information and exit.
◈ – -version option : This displays the version information and exit.


Application of basename command :


There are many utilities available which are only useful when it comes to shell scripts and doesn’t have any practical application when used by itself and basename comes under this category.

Example of a shell script which converts gif image files into png image files with the help of basename command :

/*shell script to convert the
gif image file format into
png image file format */

#!/bin/sh
for file in *.gif;do
    #exit if there are no files
    if [! -f $file];then
        exit
    fi
    b='basename $file .gif'
    echo NOW $b.gif is $b.png
    giftopnm $b.gif | pnmtopng >$b.png
done

In the above shell script basename command is used to extract the file names with extension .gif without the extension so that they can be converted into .png files.

Friday 1 February 2019

SAR command in Linux to monitor system performance

sar : System Activity Report

It can be used to monitor Linux system’s resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more.Linux system Monitoring and analyzing aids understanding system resource usage which can help to improve system performance to handle more requests.

SAR Command, Linux Study Materials, LPI Guides, LPI Certifications

By default SAR command displays result on the output screen, in addition result can also be stored in the file specified by the -o filename option.

Any user can collect information about system performance using system activities flags. The SAR command will show only CPU monitoring activity if any flag is not specifies by user.

Note : sar may not installed by default. We need to install sysstat before using it (For example, in Ubuntu, we can install using sudo apt install sysstat). After installing systat, we need to make sure that data collection is enabled. (For example, in Ubuntu, we can enable data collection by marking ENABLED=”true” in /etc/default/sysstat)

Syntax :


$ sar -[ options ] time_interval number_of_tines_to_display


Examples :


1. To see help

SAR Command, Linux Study Materials, LPI Guides, LPI Certifications

2. To verify the sar version :

hduser@mahesh-Inspiron-3543:~$ sar -V
sysstat version 11.2.0
(C) Sebastien Godard (sysstat  orange.fr)

3. To report CPU details total 5 times with the interval of 2 seconds

hduser@mahesh-Inspiron-3543:~$ sar -u 2 5
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:00:20  IST     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:00:22  IST     all      0.25      0.00      0.00      0.00      0.00     99.75
04:00:24  IST     all      0.25      0.00      0.13      0.00      0.00     99.62
04:00:26  IST     all      0.88      0.00      0.25      1.13      0.00     97.75
04:00:28  IST     all      0.00      0.00      0.25      0.13      0.00     99.62
04:00:30  IST     all      0.25      0.00      0.38      0.12      0.00     99.25
Average:        all      0.33      0.00      0.20      0.28      0.00     99.20

4. To report about amount of memory used, amount of memory free, available cache, available buffers total 3 times with the interval of 1 second.

hduser@mahesh-Inspiron-3543:~$ sar -r 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:05:12  IST kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit  kbactive   kbinact   kbdirty
04:05:13  IST   6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60
04:05:14  IST   6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60
04:05:15  IST   6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60
Average:      6067308   2017252     24.95     62300    853612   4303644     35.89   1308856    525628        60

5. To report about file systems mounted on the device total 5 times with the interval of 2 seconds.

hduser@mahesh-Inspiron-3543:~$ sar -F 2 5
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:02:38  IST  MBfsfree  MBfsused   %fsused  %ufsused     Ifree     Iused    %Iused FILESYSTEM
04:02:40  IST     78181     18727     19.32     24.43   6066698    249334      3.95 /dev/sda11
04:02:40  IST       441        55     11.04     11.04         0         0      0.00 /dev/sda1
04:02:40  IST      2123      1747     45.13     45.13         0         0      0.00 /dev/sdb1
04:02:40  IST     28846    205214     87.68     87.68  29589586    145270      0.49 /dev/sda8

6. To report about block devices details total 3 times with the interval of 1 second.

hduser@mahesh-Inspiron-3543:~$ sar -d 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:04:34  IST       DEV       tps  rd_sec/s  wr_sec/s  avgrq-sz  avgqu-sz     await     svctm     %util
04:04:35  IST    dev8-0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:35  IST   dev8-16      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

7. To report run queue length, number of processes and load average

hduser@mahesh-Inspiron-3543:~$ sar -q 2 5
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:01:54  IST   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
04:01:56  IST         0       491      0.21      0.16      0.15         0
04:01:58  IST         0       491      0.21      0.16      0.15         0
04:02:00  IST         0       491      0.19      0.16      0.15         0
04:02:02  IST         0       491      0.19      0.16      0.15         0
04:02:04  IST         0       491      0.18      0.16      0.14         0
Average:            0       491      0.20      0.16      0.15         0

8. To report cpu usage for given core :

hduser@mahesh-Inspiron-3543:~$ sar -P 1 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:16:38  IST     CPU     %user     %nice   %system   %iowait    %steal     %idle
04:16:39  IST       1      0.00      0.00      0.00      0.00      0.00    100.00
04:16:40  IST       1      0.99      0.00      0.99      0.00      0.00     98.02
04:16:41  IST       1      1.00      0.00      0.00      0.00      0.00     99.00
Average:          1      0.66      0.00      0.33      0.00      0.00     99.00

9. To report about network interface, network speed, IPV4, TCPV4, ICMPV4 network traffic and errors

hduser@mahesh-Inspiron-3543:~$ sar -n DEV 1 3 | egrep -v lo
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:04:00  IST     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s   %ifutil
04:04:01  IST enp0s29u1u2      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00
04:04:01  IST    enp7s0      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00

10. To report details about the process, kernel thread, i-node, and the file tables

hduser@mahesh-Inspiron-3543:~$ sar -v 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:25:26  IST dentunusd   file-nr  inode-nr    pty-nr
04:25:27  IST     43219      7584     46874        17
04:25:28  IST     43219      7584     46873        17
04:25:29  IST     43219      7584     46873        17
Average:        43219      7584     46873        17

11. To report messages, semaphores and processes details for all processors and system-wide.

hduser@mahesh-Inspiron-3543:~$ sar -mu -P ALL

12. To report statistics about swapping

hduser@mahesh-Inspiron-3543:~$ sar -S 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:08:09  IST kbswpfree kbswpused  %swpused  kbswpcad   %swpcad
04:08:10  IST   3906556         0      0.00         0      0.00
04:08:11  IST   3906556         0      0.00         0      0.00
04:08:12  IST   3906556         0      0.00         0      0.00
Average:      3906556         0      0.00         0      0.00

13. To report details about I/O operations like transaction per second, read per second, write per second

hduser@mahesh-Inspiron-3543:~$ sar -b 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:08:41  IST       tps      rtps      wtps   bread/s   bwrtn/s
04:08:42  IST      0.00      0.00      0.00      0.00      0.00
04:08:43  IST      2.00      0.00      2.00      0.00     64.00
04:08:44  IST      0.00      0.00      0.00      0.00      0.00
Average:         0.67      0.00      0.67      0.00     21.33

14. To report statistics about context switching, number of processes created per second, number of swap per second

hduser@mahesh-Inspiron-3543:~$ sar -w 1 3
Linux 4.4.0-31-generic (mahesh-Inspiron-3543)     Sunday 18 March 2018     _x86_64_    (4 CPU)

04:09:42  IST    proc/s   cswch/s
04:09:43  IST      0.00    480.00
04:09:44  IST      0.00    637.00
04:09:45  IST      0.00    859.00
Average:         0.00    658.67