0

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 \
  http://server/location/jobadmin.php?menu=16

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?

Thanks!

Ian

3
  • 2
    Use absolute file names instead of relative file names.
    – mailq
    Oct 19, 2011 at 15:27
  • You're a big fan of answering question in comments, aren't you ? :)
    – adaptr
    Oct 19, 2011 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, 2011 at 16:44

1 Answer 1

1

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.

2
  • 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, 2011 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, 2011 at 15:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .