• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

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

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/dudes.net/public_html/bans/ips.txt"

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/dudes.net/public_html/bans/ips.txt"

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

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

Thanks in advance....
0
ghboom
Asked:
ghboom
  • 3
  • 2
1 Solution
 
Duncan RoeSoftware DeveloperCommented:
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/dudes.net/public_html/bans/ips.txt"
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)
0
 
Duncan RoeSoftware DeveloperCommented:
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/dudes.net/public_html/bans/ips.txt"
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.
0
 
ghboomAuthor Commented:
duncan, tried your first comment then added your second one (cd /root)
and prints file nicely ;)

Thanks
GHBoom
0
 
ghboomAuthor Commented:
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/dudes.net/public_html/bans/ips.txt"

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/dudes.net/public_html/bans/ips.txt"

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


0
 
Duncan RoeSoftware DeveloperCommented:
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/dudes.net/public_html/bans/ips.txt"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now