Friday, November 16, 2018

Tuesday, November 13, 2018

How to automate jobs or schedule the task in Linux ?

How to automate jobs or schedule the task in Linux ?
Crontabs:

Recently, i had to download source code from the repository every day.I Need to take latest code every time when i start new task. But  it takes a lot of time to get the source code and building it. When i searched i got lot of information which is very use full and helped me a lot and that actually it reduces my work. I am sharing the same thing here which makes your job easy using crontab.


usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]

-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting
                  user's crontab)

-s (selinux context)


Parameters:
.---------------- minute (0 - 59)"
|  .------------- hour (0 - 23)"
|  |  .---------- day of month (1 - 31)"
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ..."
|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) 
                  OR 
                  sun,mon,tue,wed,thu,fri,sat"
|  |  |  |  |"
*  *  *  *  * user-name  command to be executed"

Example 1:

First we need to create the script to run your jobs. Put all the necessary commands to fetch the source and building activity. Once the script is ready, go to the terminal and type the command : 

crontab -e

This will open the temp file then insert the command :

32 22 * * * sh /home/WorkspaceVirat/getsrc.sh

Each parameter is explained above .After giving proper date and timing to when to execute the script (getsrc.sh), save the temp file. This will create a new job. At the given time the source code will automatically downloaded and build to your mentioned path.

If you want to see the cron jobs Type command:

 crontab -l

This will display all the cron jobs that are currently running.

By default Linux server will send mail to you regarding the completed jobs that you are created using crontab.
These mails are stored in 

/var/spool/mail/





Monday, November 12, 2018

How do exclude some file type when grep word ? How do exclude binary files when grep word ?

How do exclude some file type when grep word ? How do exclude binary files when grep word ?

Answers:


grep -Inr  "your file name"

where,

I   -> Indicates exclude binary file for the current search.
n   -> Prefix each line of output with the 1-based line number
       within its input file
r   -> Read all files under each directory.

Example:

grep -Inr  "helloworld"

Description:

This will search the given word in all files recursively under the current path except in the binary files. If the match found it will display the file and line number where exactly it appears.




grep -Inr --exclude='*.FileType' "Search word"

Example:
 grep -Inr --exclude='*.html' "helloworld"

Description:

This will search the given word in all files recursively under the current path except in the binary files and .html files. Because as we specifically mentioned exclude the files of type html. If the match found it will display the file and line number where exactly it appears.