I have a simple python script which I am using to automate updates to a dhcp config file.
The Idea is that it puts the new config file in the dhcpd directory runs a check and if that returns ok it can restart the service. My code looks like this:
syslog.syslog(syslog.LOG_INFO, 'INFO: file copied to /etc/dhcp/conf.d')
return_code = subprocess.call(['dhcpd -t -cf /etc/dhcp/dhcpd.conf'], shell=True)
if return_code != 0:
print('dhcp config test failed, exiting script')
syslog.syslog(syslog.LOG_ERR, 'ERROR: dhcp config test failed, exiting script')
sys.exit()
else:
print('dhcp config test passed restarting service')
syslog.syslog(syslog.LOG_INFO, 'INFO: config check passed, restarting service')
return_code = subprocess.call(['service', conf['service_name'], 'restart'])
if return_code != 0:
print('dhcpd service failed to restart')
syslog.syslog(syslog.LOG_ERR, 'ERROR: dhcpd service failed to restart')
else:
print('dhcpd service restarted')
syslog.syslog(syslog.LOG_INFO, 'INFO: service restarted')
email_results()
This script is kicked off by a cron job, when it runs it always fails at this bit:
print('dhcp config test failed, exiting script')
If I run the script manually it always works fine and continues to the end as expected.
If I open the python shell and run the important commands by hand it seems to work fine:
python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> return_code = subprocess.call(['dhcpd -t -cf /etc/dhcp/dhcpd.conf'], shell=True)
Internet Systems Consortium DHCP Server 4.3.3
Copyright 2004-2015 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Config file: /etc/dhcp/dhcpd.conf
Database file: /var/lib/dhcp/dhcpd.leases
PID file: /var/run/dhcpd.pid
>>> print(return_code)
0
I have tried using "shell=True" and also tried without. I have also tried subprocess.check_call with the same results.
Where am I going wrong here?