Why do these work at the command line and not in cron ?

Posted on 2006-06-03
Last Modified: 2011-09-20
Hello linux experts !

I have a time sensitive problem, need the answer quick, but also ned to know why these work from console, as root but
not in roots crontab.....

This works
bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' > "/home/dude/domains/"

This does not, (prints empty file ?)
30 * * * * bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' > "/home/dude/domains/"

same with ,
works fine ...
bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" | mail -s "found ips"

but does not
* * 1 * * bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" | mail -s "found ips"

Thanks in advance....
Question by:ghboom
    LVL 34

    Expert Comment

    by:Duncan Roe
    Most likely explanation is that "bfd" is found in your PATH when running as root from the console but not in a job running from cron,
    PATH inherited by cron jobs is very short: will not in general include /usr/local/bin for instance. It will probably (but not certainly) find awk & grep, but where is your "bfd" command?
    From bash, enter "type bfd"; from other shells enter "which bfd" to get the full pathname of the bfd command. Change your command line to:
      <full_path_of_bfd> -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' > "/home/dude/domains/"
    Make sure that works from the command line, then try it in the cron job.

    If problems persist, repeat for awk & grep (use their full pathnames)
    LVL 34

    Accepted Solution

    Also / alternatively you might need to set working directory for bfd for it to run properly, e.g.:
      30 * * * *cd /root;bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' > "/home/dude/domains/"
    or again, there might be other environment variables that bfd depends on. My "man cron" says it only sets  USER, HOME & SHELL - trim your environment down to that at the command line and see if bfd still works.

    Author Comment

    duncan, tried your first comment then added your second one (cd /root)
    and prints file nicely ;)


    Author Comment

    Hold on one sec !
    Ok it worked Kind of but somthing happened to BFD now !
    at console typing bfd -a produces no output  ;(

    but I can see that ips.txt has been updated and contains info...

    I had to remove the line from crontab, and resart apf

    I know you dont owe it to me, but any suggestions on
    getting cron to run the command like bash would ?

    Here is what I tried, I added the complete path to bfd,
    30 * * * * cd /root;/usr/local/sbin/bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' >"/home/dude/domains/"

    thinking that the working dir of bfd was perminently changed, the ips.txt was fine,
    but still no ouput from bfd -a

    I think I should create a file

    #! /bin/bash
    bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' >"/home/dude/domains/"

    but dont want to mess with bfd/apf  so any other comment would be great !

    LVL 34

    Expert Comment

    by:Duncan Roe
    I guessed cd/root because that's where you start off when you log in as root.
    Actually I'm not familiar with bdf / apf, was only answering from the point of view of why cron jobs usually don't work.
    Creating a file can certainly be a good idea. Be aware that the file inherits its environment from cron (including working dir).
    Check the man pages of bfd / apf for reference to environment variables - your file should create any that are required. For testing, run a cron job that logs the output from "env" and "pwd".
    Get your shell down to just what was logged - then you can test your script.
    I'd suggest as a starting point:

    #!/bin/bash -p
    export PATH=/bin:/usr/bin:/usr/local/sbin
    cd /root
    bfd -a | grep -P "[0-9]{1,3}(\.[0-9]{1,3}){3}" |awk '{print $1}' >"/home/dude/domains/"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
    I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now