I am trying to automate a Python script to continuously capture and write network packets to files. The script relies heavily on the use of Wireshark's dumpcap CLI command, and uses the subprocess Popen module to run the command in a shell. During normal execution, the script creates a new directory and begins capturing network data to that new directory. The script runs to completion when executed manually on the command line, however, when I have cron execute the job the script terminates before the capture begins with the following error:
Traceback (most recent call last):
File "./netcapture.py", line 19, in <module>
dumpcap = subprocess.Popen(['dumpcap', '-i', '3', '-a', 'duration:30', '-b', 'duration:10', '-w', filepath], cwd=r'/var/tmp/traces/')
File "/usr/lib64/python2.6/subprocess.py", line 642, in __init__ errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1238, in __execute_child raise child_exception
OSError: [Errno 2] No such file or directory
the filepath variable is
netdata_02_24_2021/trace.pcap
which is the name of the newly created directory and a filename & type for dumpcap to write the network data to. I am running this script with Python 2.6 on RHEL 6 for reference. The cron job syntax is as follows:
0 0 * * * cd /var/tmp/traces/ && python ./netcapture.py &
Things I know:
- cron is able to find and execute the script
- the current working directory that the script executes in is correct
- subprocess.Popen works when executed by cron, as it is used prior to the error being thrown to successfully make a
mkdir -p netdata_02_24_2021
call in the current working directory
- the pathing is correct for the capture write. I am able to execute this script manually and it recognizes the new directory and writes to it
Things I have tried:
- providing the full path for all directories
- switching the subprocess.Popen() calls to subprocess.check_call() calls
- using the shell=True syntax in my subprocess.Popen() calls
At this point, I'm not sure what could be causing this execution issue with cron. Any help would be greatly appreciated.