Sorry for the vague title, it's a bit hard to describe in one sentence :)

I have a cron job that runs on a FC4 machine. The cron job, running as root, runs just fine, but one line in the script that is running redirects the output to a file and it does not work as expected.

For example:

wget --user=user --password=password \
  --no-check-certificate -O output.txt \

cat output.txt | grep -o \
  '\(location\/autoprocess_test.php?autoprocesskey=true&\)\([a-Z0-9=&-]\)\{1,\}' \
  > jobs.txt

I've noticed that when I run this script manually as root, it works just fine. However, when it runs on the cron, it does not redirect output to the files specified, it just leaves a 0 byte file in it's place.

Not sure if this is something trivial or difficult, but does anyone have any ideas as to why this is happening?



  • 2
    Use absolute file names instead of relative file names.
    – mailq
    Oct 19 '11 at 15:27
  • You're a big fan of answering question in comments, aren't you ? :)
    – adaptr
    Oct 19 '11 at 15:42
  • @adaptr Yep. Unless I really validated the correct reason. This was just a guess in the dark; not worth a real answer.
    – mailq
    Oct 19 '11 at 16:44

First, as mailq already said, when invoking a script from a cron job - that may run as a completely different user - use absolute pathnames.

Second, is the user running this cron job the same user you tested it with ?
ALWAYS test any scripts you intend to run through cron under the exact same circumstances - the environment when run through cron is not the same as a normal interactive user shell!

Lastly - you can probably avoid two of the three relative-pathanme instances by just feeding the output to grep:

grep -o 'complicated regex' <(your wget line) > /absolute/output/file

This avoids relative files completely by using automatic temporary files.

  • Thanks -- I made the script more concise by piping the output rather than writing to files. This still didn't solve the problem, as for some reason cron jobs running as root will not redirect output to a file.
    – Ian P
    Oct 20 '11 at 14:46
  • That sounds like something you should investigate. Is this box running any sort of security framework (SELinux, Apparmor, etc) ?
    – adaptr
    Oct 20 '11 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.