0

I have a Python process that uses os.popen to run tcpdump in the background. It then reads and processes the output from tcpdump. The process runs in the background as a daemon. When I execute this process from the command line, it runs just fine--it fires up tcpdump and reads the output properly. However, I want this process to run automatically at boot and I've directed it to do so in cron. When I do this, my process is running (per the ps command) but tcpdump is not.

Is there some reason the behavior is different starting a process in cron vs starting it from the command line? My code looks something like this:

p = os.popen('/usr/sbin/tcpdump -l -i eth0')
while True:
   data = p.readline()
   # do something with data
kittyhawk
  • 688
  • 2
  • 11
  • 25
  • 2
    Try opening it using `p = subprocess.Popen(['/usr/sbin/tcpdump'], stdout=subprocess.PIPE, stderr=subprocess.PIPE])`, and check `p.stderr` to see if tcpdump failed on startup for some reason. (which certainly appears to be the case). Maybe eth0 wasn't ready yet or something? – dano Jul 11 '14 at 15:26
  • Thanks for getting me thinking about this and helping me fix it. Apparently, the interface wasn't up yet. – kittyhawk Jul 11 '14 at 15:44

1 Answers1

1

cron will send you an email when there is a problem. So the first thing is to look into your mailbox (run mailx to access it).

If there is no mail, make sure the processes write messages to stdout/stderr when there is a problem.

Also: Check that you're using the correct user. On some systems, tcpdump needs to be run as root, so you need to install the job into root's crontab (instead of the one of your normal user).

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820