Net Assured

Net Assured

Network and Security Consultancy

T 07789 400408
Email: info@netassured.co.uk

Net Assured Limited
84 Goodacre, Orton Goldhay, Peterborough, PE2 5LZ

  • Home
  • Services
  • Blog
  • About
  • Contact
FREEQUOTE

Executing Python scripts from crontab

Sunday, 01 April 2018 / Published in Automation

Executing Python scripts from crontab

I’ve been slowly converting my bash scripts to Python. This post details how I solved an issue with executing Python scripts from crontab. Most of my bash scripts are scheduled to run daily using crontab and work fine. The first script I converted to Python was a bash script used to backup the configurations of my Cisco SG300 switches.

The new Python script that uses Netmiko would run fine from a shell but not from crontab. Please note that I use Ubuntu. what follows may differ depending on the Linux distro your using.

The issue was proven to be environment paths required in crontab to run the script. The Python script itself executed fine but as the script was importing the Netmiko library I think thats where it was failing.

Troubleshooting the problem

To troubleshoot the issue a log file was configured in crontab for the entry to kick off the script. This would help provide some visibility into what was happening.

To speed up the testing the script was configured to fire every minute and output to a file /var/log/output.log

* * * * * python /Scripts/cisco_sg300_backup_tftp.py >> /var/log/output.log 2>&1

First check that the script was execute by cron:

grep CRON /var/log/syslog

Mar 25 09:46:01 SV-NA1-VIR-22 CRON[8535]: (root) CMD (python /Scripts/cisco_sg300_backup_tftp.py >> /var/log/output.log 2>&1)

The contents of the log file:

more /var/log/output.log

Traceback (most recent call last):
  File "/Scripts/cisco_sg300_backup_tftp.py", line 17, in 
    from netmiko import ConnectHandler
  File "build/bdist.linux-x86_64/egg/netmiko/__init__.py", line 8, in 
  File "build/bdist.linux-x86_64/egg/netmiko/ssh_dispatcher.py", line 4, in 
  File "build/bdist.linux-x86_64/egg/netmiko/a10/__init__.py", line 2, in 
  File "build/bdist.linux-x86_64/egg/netmiko/a10/a10_ssh.py", line 4, in 
  File "build/bdist.linux-x86_64/egg/netmiko/cisco_base_connection.py", line 3, in 
  File "build/bdist.linux-x86_64/egg/netmiko/base_connection.py", line 24, in 
  File "build/bdist.linux-x86_64/egg/netmiko/utilities.py", line 8, in 
ImportError: No module named serial.tools.list_ports

You can see from output that there was an issue with importing the Netmiko library.

Fixing the problem

Knowing that the script executes manually from a shell I took the environment variables and copied them into crontab.

nickm@SV-NA1-VIR-22:/Scripts$ sudo env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
LANG=en_GB.UTF-8
HOME=/home/nickm
LANGUAGE=en_GB:en
MAIL=/var/mail/root
LOGNAME=root
USER=root
USERNAME=root
SHELL=/bin/bash
SUDO_COMMAND=/usr/bin/env
SUDO_USER=nickm
SUDO_UID=1000
SUDO_GID=1000

Through trial and error I found out which of the above output was required in crontab. For completeness I’ve include the full output of my crontab file:

nickm@SV-NA1-VIR-22:~$ sudo crontab -l

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
SHELL=/bin/bash
SUDO_COMMAND=/usr/bin/env
HOME=/home/nickm

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
#
# Run the reboot modem script every 15 minutes
*/15 * * * * /Scripts/reboot_modem.sh
# Execute the VMware esxi host backups @ 1pm every day
0 13 * * * /Scripts/esxi_host_backups.sh
# Execute the Cisco SG300 backups @ 2pm every day
0 14 * * * python /Scripts/cisco_sg300_backup_tftp.py
# Execute the Palo Alto firewall backups @ 3pm every day
0 15 * * * /Scripts/palo_fw_backups.sh
# Execute the Juniper SRX firewall backups @ 4pm every day
# 0 16 * * * /Scripts/juniper_srx_backups.sh

I’m not sure why ‘HOME=/home/’ had to be set to nickm given that cron is run as root. If someone could enlighten me I’d appreciate it. I can drive linux pretty well but I’m no Linux mechanic.

Summary

Boom! with the environment variables the script now works fine. I’m hopeful executing Python scripts from crontab will work for all my future scripts too.

Tagged under: python, scripting

Categories

  • Automation
  • Networking
  • Security
  • Starlink
  • Ubiquiti UniFi

NEWSLETTER SIGNUP

By subscribing to our mailing list you will always be updated with the latest news from us.

We never spam!

Menu

  • Home
  • Services
  • Blog
  • About
  • Contact
  • Privacy Policy
  • Cookie Policy

Get In Touch

T 07789 400408
Email: info@netassured.co.uk
NET ASSURED LIMITED
84 Goodacre | Orton Goldhay
Peterborough | PE2 5LZ
Company Registration: 9480811
VAT Registration: 207 5161 32
  • GET SOCIAL

© 2015 - 2026 Copyright by Net Assured Limited | All rights reserved

TOP
Manage Consent
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behaviour or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
View preferences
  • {title}
  • {title}
  • {title}