LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-24-2007, 10:59 AM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
grep does not work in crontab script


Hey all,
I have a script that does a grep for a file in a directory.
Following is just idea of what Im doing.
Code:
#!/bin/bash

cd /home/user
grep -l sometext *
When i schedule this in crontab grep does not find the text, however if I simple run this from the command line, grep finds the text just fine. So I thought i was an environment issue. Then on the top of the script I added and exported ALL env variables of my current shell, then scheduled the script again, but still nothing.

Is there something funny about running grep in a scheduled script?

Thanks all!
 
Old 08-24-2007, 11:25 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,815
Blog Entries: 15

Rep: Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662
When you run a script from command line it inherits your environment including the PATH variable that tells it where to find commands.

When you run in cron it gets minimal environment. When you start the script it executes bash (due to the interpreter line) and may lose even that minimal environment.

Therefore it is important you include the environment within the script itself.

So type "which grep" to find out where grep is:
which grep
/bin/grep

Then in your script either set the PATH variable to include that directory (/bin in the above) OR just use the fully qualified path to the grep command.
e.g.

Either
Code:
#!/bin/bash

PATH=/bin
cd /home/user
grep -l sometext *
Or
Code:
#!/bin/bash

cd /home/user
/bin/grep -l sometext *
You don't have to worry about "cd" because it is built into bash itself.
 
Old 08-24-2007, 11:37 AM   #3
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
From Command line:
Code:
$ which grep
/bin/grep
within Script (as cronjob)
Code:
+ env
SHELL=/bin/sh
OLDPWD=/home/vcs
USER=vcs
PATH=/usr/bin:/bin
PWD=/home/vcs/log/vvcs
HOME=/home/vcs
SHLVL=1
LOGNAME=vcs
_=/usr/bin/env

+ which grep
/bin/grep
So i dont think the location of the grep command is my issue.
This is just weird.
 
Old 08-24-2007, 02:16 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,815
Blog Entries: 15

Rep: Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662
Have you tried:

Code:
#!/bin/bash

cd /home/user
grep -l sometext *  2>&1 | tee >/tmp/script.log
So it will send stderr to stdout then add both to /tmp/script.log to see what if any errors it may be spawning? The cronlog itself only records the success or failure of scheduling - not the actual output if any that the script may have generated.

Also have you verified the user running the cron is the same as owns the files you're trying to grep from (and /home/user)? It might be a simple permissions issue.
 
Old 08-24-2007, 02:43 PM   #5
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Original Poster
Rep: Reputation: 30
Yes, when i am scheduling the job in crontab, I output to log, and issue 2>&1. I see that the grep does in-fact find all the files in the directory (as to indicate i am in the correct directory), but the grep returns nothing. And all permissions are set to allow owner and other rwx. I am the owner, and I am issuing the crontab job.

Im about to give up, and see about another route to solve my issue.
I wish you could help, and I appreciate it!!, I feel like I just cant get you the information you need to assist me.
 
Old 08-24-2007, 03:19 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,815
Blog Entries: 15

Rep: Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662Reputation: 1662
Quote:
I see that the grep does in-fact find all the files in the directory (as to indicate i am in the correct directory), but the grep returns nothing
That sentence suggests what the issue is.

The "-l" flag of grep specifically returns ONLY the file name - not the line that matched.

From the grep man page:
Quote:
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed. The
scanning will stop on the first match.
It sounds as if you want the text rather than the file name - if so just do the "grep sometext *" (i.e. without the -l flag).

If you need BOTH the file name AND the matching text you could do something like:

Code:
for FILE in `grep -l sometext *`
do echo $FILE
   grep sometext $FILE
   echo ""
done

The first grep -l gets you the list of files. Then the loop will print each file name and below it the text that matched from the file listed. The echo "" is just to insert a line between each file and its output for legibility.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and Grep in Script - almost there chess Programming 8 05-01-2007 10:15 AM
grep script kaiserbeto Linux - Newbie 2 11-15-2006 02:50 PM
grep script/syntax carriehoff Linux - Newbie 6 08-31-2006 02:13 PM
Shell script with grep MicahCarrick Programming 4 08-15-2006 02:08 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:24 PM.

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