LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-24-2016, 04:07 AM   #1
QuestionMqrk
LQ Newbie
 
Registered: Oct 2016
Posts: 29

Rep: Reputation: Disabled
Question Using "grep" within a CRON Job


Hi everyone;

Recently I set up a little CRON job that saves logs of the SmartCTL results.
Code:
>0 11 * * * /usr/sbin/smartctl -aixcA -d sat /dev/sda > /home/user_name_censored/CRON/smartctl-output-$(/bin/date +\%d\%m\%y)
Now i'm looking to make it save only a certain part of the Smart results; preferably just the Attribute List to show if the drive would fail soon.

I've been told to try "grep" to search for patterns in the text file but am having trouble understanding how to use it. Any guides online are either too complicated for me to understand or not exactly what i'm looking for. Any help would be greatly appreciated.
 
Old 10-24-2016, 04:27 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,925

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
Code:
grep PATTERN [FILE]
So... and these are on the command line, not through any cron job:
Code:
# smartctl -a /dev/sda
smartctl 5.41 2011-06-09 r3365 [x86_64-linux-3.10.0+2] (local build)
Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net

Vendor:               LSI
Product:              MR9271-4i
Revision:             3.45
User Capacity:        3,999,688,294,400 bytes [3.99 TB]
Logical block size:   512 bytes
Logical Unit id:      0x600605b00b6d7d501eeb815b5ba29659
Serial number:        005996a25b5b81eb1e507d6d0bb00506
Device type:          disk
Local Time is:        Mon Oct 24 11:11:22 2016 CEST
Device does not support SMART

Error Counter logging not supported
Device does not support Self Test logging
But if I only want the "Serial number"
Code:
# smartctl -a /dev/sda | grep "Serial number"
Serial number:        005996a25b5b81eb1e507d6d0bb00506
There are more advanced ways to specify patterns, for example to get the capacity and serial:
Code:
# smartctl -a /dev/sda | grep "Serial\|Capacity"
User Capacity:        3,999,688,294,400 bytes [3.99 TB]
Serial number:        005996a25b5b81eb1e507d6d0bb00506
You'll also see that grep checks the lines in order and if they match the pattern they'll be displayed, regardless of the order in the pattern.

You're also now at the stage where your cron line is getting a bit complicated so you might want to consider creating a shell script and calling that from cron.
 
Old 10-24-2016, 05:34 AM   #3
QuestionMqrk
LQ Newbie
 
Registered: Oct 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
Thumbs up Solved

So, thanks to your brief explanation, I came up with this:
Code:
0 11 * * * /usr/sbin/smartctl -aixcA -d sat /dev/sda | grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_Uncorrectable > /home/user_name_censored/CRON/smartctl-output-$(/bin/date +\%d\%m\%y)
Basically pulling all the infos I need from Smart and putting into a file. I've given it a test run and it works seamlessly. Your examples of having grep filter out multiple items was particularly helpful. Thank you!
 
Old 10-24-2016, 05:40 AM   #4
QuestionMqrk
LQ Newbie
 
Registered: Oct 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
A small follow-up question - how can I get it to, rather than create a new file each time it runs, edit one file by adding the date, followed by the results, to the top (or bottom, if it's too complicated to have the most recent first) of the file?

So for now the command I posted above creates a file for each log; but I'd prefer to have everything in one place.

So it can be:

24.10.16

5 Reallocated_Sector_Ct PO--CK 200 200 140 - 0
10 Spin_Retry_Count -O--C- 100 100 051 - 0
196 Reallocated_Event_Count -O--CK 200 200 000 - 0
197 Current_Pending_Sector -O--C- 199 198 000 - 35
198 Offline_Uncorrectable ----C- 200 200 000 - 0

25.10.16

5 Reallocated_Sector_Ct PO--CK 200 200 140 - 0
10 Spin_Retry_Count -O--C- 100 100 051 - 0
196 Reallocated_Event_Count -O--CK 200 200 000 - 0
197 Current_Pending_Sector -O--C- 199 198 000 - 35
198 Offline_Uncorrectable ----C- 200 200 000 - 0


...and so forth.
 
Old 10-24-2016, 06:03 AM   #5
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 2,925

Rep: Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192Reputation: 1192
Now you really should be looking at turning this in to a script as it's going to start to get really unreadable on a single line:

Code:
0 11 * * * /bin/date >> /home/user_name_censored/CRON/smartctl-output && /usr/sbin/smartctl -aixcA -d sat /dev/sda | /bin/grep -w "Reallocated_Sector_Ct\|Spin_Retry_Count\|Reallocated_Event_Count\|Current_Pending_Sector\|Offline_Uncorrectable" >>  /home/user_name_censored/CRON/smartctl-output
Note that this will append results to the file /home/user_name_censored/CRON/smartctl-output and the date output will be the full date format.
 
Old 10-24-2016, 07:52 AM   #6
QuestionMqrk
LQ Newbie
 
Registered: Oct 2016
Posts: 29

Original Poster
Rep: Reputation: Disabled
This is exactly what I meant. Thanks for the help.

Regarding creating a script; it will only be running for about a month and I won't be using this computer for anything else Terminal-Related so I think it's fine. Cheers though.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Cron job error "PHP Notice: Undefined variable:.........." branddonchau CentOS 3 05-27-2015 04:08 AM
"top -n1 >> my_log_file" doesn't work in cron job 5883 Linux - General 3 07-28-2014 04:34 PM
How do I make a cron job run "every two weeks"? Seventh Linux - Newbie 12 01-13-2006 11:55 AM
"permission denied" error when cron job executes jillu Linux - Newbie 11 11-02-2004 01:19 PM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration