Special Commands: (Crontab, mail, bc, sed, awk ) 5
===================================================================================
============================================
51) crontab
- Linux Crontab is a powerful utility that is used for Scheduling and
Automating Tasks in Unix-like operating systems.
- It facilitates the users to run the scripts or Linux Commands at specified
times and intervals.
- It is ideal for repetitive tasks such as system maintenance, backups, and
updates.
- If you want any job or work to be scheduled on your Linux or Unix Operation
System, then the Linux ‘Crontab’ Command will be a popular one.
- The ‘Crontab’ Command on Linux works in the system’s background.
- The ‘Crontab’ Linux Command goes on to check the time on the device and
when a particular time arrives,
it performs pre-defined tasks automatically.
- Crontab works by allowing the users to schedule tasks or commands that help
to run automatically at specified times and intervals.
The Linux Crontab Format is represented by the following syntax:
MIN HOUR DOM MON DOW CMD
MIN (Minute) --> Specifies the minute when the command will run
It ranges from 0 to 59.
HOUR --> Denotes the hour of the day when the command is scheduled to
execute.
It spans from 0 to 23.
DOM (Day of Month) --> Specifies the day of the month for the task.
It ranges from 1 to 31.
MON (Month)--> Indicates the month during which the command will be
executed.
It varies from 1 to 12.
DOW (Day of Week)--> Specifies the day of the week for the task.
It is represented by numbers from 0 to 7, where both 0 and 7 correspond
to Sunday.
CMD (Command)
Represents the actual command or script that will run at the scheduled
time.
Ex:
1) Scheduling a Job For a Specific Time:
- The basic usage of cron is to execute a job in a specific time as
shown below.
- This will execute the Full backup shell script (full-backup) on 10th
June at 08:30 AM.
- The time field uses 24 hours format. So, for 8 AM use 8, and 8 PM use
20.
30 08 10 06 * /home/maverick/full-backup
--> 30 – 30th Minute 08 – 08 AM 10 – 10th Day 06 – 6th Month
(June) * – Every day of the week
2) To Schedule a Job for Every Minute using Cron:
* * * * * CMD
--> The * means all the possible units — i.e. every minute of every
hour throughout the year.
More than using this * directly, you will find it very useful in
the following cases.
When you specify */5 in the minute field means every 5 minutes.
When you specify 0-10/2 in the minute field means every 2 minutes
in the first 10 minutes.
Thus the above convention can be used for all the other 4 fields.
3) To Schedule a Job For More Than One Time (e.g. Twice a Day):
The following script takes an incremental backup twice a day
every day.
This example executes the specified incremental backup shell
script (incremental-backup) at 11:00 and 16:00 every day.
The comma-separated value in a field specifies that the command
needs to be executed at all the mentioned times.
00 11, 16 * * * /home/maverick/bin/incremental-backup
--> 00 – 0th Minute (Top of the hour) 11, 16 – 11 AM and 4 PM * –
Every day * – Every month * – Every day of the week
4) To Schedule a Job for a Within Certain Range of Time (e.g. Only on
Weekdays):
If you wanted a job to be scheduled for every hour within a
specific range of time then use the following.
Cron Job every day during working hours: This example checks the
status of the database every day (including weekends)
during the working hours 9 a.m – 6 p.m
00 09-18 * * * /home/maverick/bin/check-db-status
--> 00 – 0th Minute (Top of the hour) 09-18 – 9 am, 10 am, 11 am,
12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* – Every day * – Every month * – Every day of the week
Cron Job every weekday during working hours:
This example checks the status of the database every weekday
(i.e. excluding Sat and Sun) during the working hours 9 a.m – 6 p.m.
00 09-18 * * 1-5 /home/maverick/bin/check-db-status
--> 00 – 0th Minute (Top of the hour) 09-18 – 9 am, 10 am, 11 am,
12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
* – Every day * – Every month 1-5 -Mon, Tue, Wed, Thu and Fri
(Every Weekday)
5) To Schedule a Background Cron Job for Every 10 Minutes:
Use the following, if you want to check the disk space every 10
minutes.
*/10 * * * * /home/maverick/check-disk-space
--> It executes the specified command check-disk-space every 10
minutes throughout the year.
===================================================================================
============================================
52) mail
- As the name suggests, it is a console application that is used for sending
and receiving emails.
Writing the message directly in the command line:
To send a simple email, use the “-s” flag to set the subject in quotes which
is followed by the email of the receiver.
After this, mailx waits for the content of the email. To enter new lines,
keep hitting enter.
After the content is written, press Ctrl+D & EOT will be displayed by mailx.
$ mail -s "A mail sent using mailx" [email protected]
Hey person,
Hope you're fine these days
Thanks
EOT
===================================================================================
============================================
53) bc
- bc command is used for command line calculator.
- It is similar to basic calculator by using which we can do basic
mathematical calculations.
- Arithmetic operations are the most basic in any kind of programming
language.
Syntax:
bc [ -hlwsqv ] [long-options] [ file ... ]
ex:
Input : $ echo "12+5" | bc
Output : 17
===================================================================================
============================================
54) sed
- SED command in UNIX stands for stream editor and it can perform lots of
functions on file
like searching, find and replace, insertion or deletion.
Though most common use of SED command in UNIX is for substitution or for
find and replace.
By using SED you can edit files even without opening them, which is much
quicker way to find and replace something in file,
than first opening that file in VI Editor and then changing it.
- SED is a powerful text stream editor. Can do insertion, deletion, search
and replace(substitution).
- SED command in unix supports regular expression which allows it perform
complex pattern matching
Syntax:
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Ex: 1)Replacing or substituting string
Sed command is mostly used to replace the text in a file.
The below simple sed command replaces the word “unix” with “linux” in the file.
$sed 's/unix/linux/' vishal.txt
2) Replacing the nth occurrence of a pattern in a line :
Use the /1, /2 etc flags to replace the first, second occurrence
of a pattern in a line.
The below command replaces the second occurrence of the word
“unix” with “linux” in a line.
$sed 's/unix/linux/2' vishal.txt
3) Replacing all the occurrence of the pattern in a line : The
substitute flag /g (global replacement) specifies the sed command to replace all
the occurrences of the string in the line.
$sed 's/unix/linux/g' vishal.txt
4) Replacing from nth occurrence to all occurrences in a line :
Use the combination of /1, /2 etc and /g to replace all the
patterns from the nth occurrence of a pattern in a line.
The following sed command replaces the third, fourth, fifth…
“unix” word with “linux” word in a line.
$sed 's/unix/linux/3g' vishal.txt
5) Replacing string on a specific line number :
You can restrict the sed command to replace the string on a
specific line number. An example is
$sed '3 s/unix/linux/' vishal.txt
6) Duplicating the replaced line with /p flag :
The /p print flag prints the replaced line twice on the terminal.
If a line does not have the search pattern and is not replaced,
then the /p prints that line only once.
$sed 's/unix/linux/p' vishal.txt
7) Printing only the replaced lines :
Use the -n option along with the /p print flag to display only
the replaced lines.
Here the -n option suppresses the duplicate rows generated by the
/p flag and prints the replaced lines only one time.
$sed -n 's/unix/linux/p' vishal.txt
8) Replacing string on a range of lines : You can specify a range of
line numbers to the sed command for replacing a string.
$sed '1,3 s/unix/linux/' vishal.txt
9) Deleting lines from a particular file :
SED command can also be used for deleting lines from a particular file.
SED command is used for performing deletion operation without even
opening the file Examples:
1. To Delete a particular line say n in this example
Syntax:
$ sed 'nd' filename.txt
Example:
$ sed '5d' filename.txt
2. To Delete a last line
Syntax:
$ sed '$d' filename.txt
3. To Delete line from range x to y
Syntax:
$ sed 'x,yd' filename.txt
Example:
$ sed '3,6d' filename.txt
4. To Delete from nth to last line
Syntax:
$ sed 'nth,$d' filename.txt
Example:
$ sed '12,$d' filename.txt
5. To Delete pattern matching line
Syntax:
$ sed '/pattern/d' filename.txt
Example:
$ sed '/abc/d' filename.txt
===================================================================================
============================================
55) awk