LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-10-2009, 08:20 PM   #1
Kuro
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Rep: Reputation: 0
Crontab and Grep date problem... should be an easy fix


Hey all.

I am having a problem getting my Crontab to work properly.

I'm using the bash shell.

I want to set up a crontab that emails me reports of login activity taken from my auth.log every night.

This is what I have in my crontab -l
I've read that crontab takes minimal environment so I added
/bin/grep and /bin/cat


Code:
# m h  dom mon dow   command

59 23 * * * /bin/cat /var/log/auth.log |/bin/grep "`date \"+%b %e\"`" |mail -s "Testing crontab" user@gmail.com

But I get nothing.
I've tested everything else, and the problem seems to be when I grep the date. This code works in the command line, but not in Crontab.

I can grep other things, but when I want this specific grep for a specific date, it doesn't work.

Is there something different with Crontab than the shell?

I have no problem if I put this command line into a script and using Crontab to run the script, but I'm just curious as to why crontab wont run the command straight.

Thanks for any input.
 
Old 12-10-2009, 08:54 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
My suggestion to you: Put everything into a shell script. That way, if you want to add more code, you just edit the shell script and don't clutter up the crontab entries. Also, debugging is easier
 
Old 12-10-2009, 09:29 PM   #3
Kuro
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
My suggestion to you: Put everything into a shell script. That way, if you want to add more code, you just edit the shell script and don't clutter up the crontab entries. Also, debugging is easier
Yeah I know, it works with a script no problem... I just can't figure out why it wouldnt work in just crontab... its essentially the same thing, and the only thing I've read that may be different, is the environment.
 
Old 12-10-2009, 09:36 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
Actually, ghostdog74 means put that into a script and call the script from cron. As he said, it's easier to amend.
Anyway, you prob need the full path the the 'date' cmd, its not a shell built-in.
On the Suse box I've got here, it's /bin/date. Try 'which date'.
 
Old 12-10-2009, 09:46 PM   #5
Kuro
LQ Newbie
 
Registered: Dec 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Actually, ghostdog74 means put that into a script and call the script from cron. As he said, it's easier to amend.
Anyway, you prob need the full path the the 'date' cmd, its not a shell built-in.
On the Suse box I've got here, it's /bin/date. Try 'which date'.
I thought thats what he meant.

I have the script in a file in my /home/user/status.sh

My previous crontab setting if I did a crontab -l was


Code:
# m h  dom mon dow   command

18 21 * * * /home/user/status.sh

My status.sh was
Code:
#!/bin/sh

CurrentDate=$(date "+%b %e")
echo $CurrentDate

cat /var/log/auth.log |grep "${CurrentDate}" |sed '/CRON/d' > /home/user/$CurrentDate.log

cat /home/user/"$CurrentDate".log  |mail -s "Todays attempts" user@gmail.com
Good idea though. I never thought about date... My 'which date' revealed

/bin/date

If I were to use full path, would it be |/bin/grep "`/bin/date \"+%b %e\"`" ?
 
Old 12-10-2009, 09:54 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can just export your PATH in your script
Code:
export PATH=/bin:$PATH
 
Old 12-11-2009, 12:44 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,357

Rep: Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367Reputation: 2367
cron jobs don't login as you, so the $PATH (if any) will be the one set in the cron env. That's why you have to either specify a PATH list in full or use full paths to all cmds/files.
Of course, you could start by sourcing your home/you/.bashrc or .bash_profile to get your normal PATH settings first.
 
  


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
Using `date '+%Y%m%d'` in crontab? stefanlasiewski Linux - Software 6 07-01-2015 02:25 PM
run crontab exclude specific date elainelaw Linux - Newbie 5 12-30-2009 11:53 AM
grep does not work in crontab script blizunt7 Linux - General 5 08-24-2007 02:19 PM
EASY fix for XMMS MP3 problem.. drumvudu SUSE / openSUSE 7 11-05-2005 09:00 PM
Mouse Problem - SHould be easy to fix! VIP3R Linux - Hardware 2 05-17-2003 05:39 PM


All times are GMT -5. The time now is 06:49 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