Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
#!/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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.