I have a Python Script below that will just run hostname on 4 different hosts and then it will save it to a file. It then will open that file and save these each line or output into a variable which the program will use later. It works fine when I run it manually although everytime I use cronjob I am getting an error.
Note: the whole script is obviously not here if you wondering how I can SSH into these hosts just by the below lines. But the script is failing at the part that I have put below.
Python Script:
#! /usr/bin/python
import os
hostname=['a','b','c','d']
for i in hostname:
os.system('hostname >> hostlist.txt')
data = open('hostlist.txt')
hosta, hostb, hostc, hostd = data.read().splitlines()
Error:
ValueError: too many values to unpack
Cronjob looks like:
00 14 * * * python /tmp/hostname.py &> /tmp/hostnamerror.log
Update:::
Ok I am pretty sure I know the problem after more troubleshooting. It seems that when I run it with Cronjob that it is not creating the file hostlist.txt, although if I ran it manually then it does create this file. So in the Cronjob it opens hostlist.txt as a new file with no variables hence giving me the error message. Does anyone have any idea why running the Python Script as a Cronjob would cause the redirect in the first os.system command not to create a append and create a file?