I have a python script at /home/pi/update_rrd_data.py that's supposed to store logs in the folder /home/pi/logs_rrd.
This is how I've implemented it:
current_folder = os.path.dirname(os.path.abspath(inspect.stack()[0][1]))
log_folder = os.path.join(current_folder, 'logs_rrd')
if not os.path.exists(log_folder): os.makedirs(log_folder)
file_name = __file__[:-3] + '_' + datetime.strftime(datetime.now(), '%Y%m%d%H%M') + '.log'
log_file = os.path.join(log_folder, file_name)
logging.basicConfig(filename=log_file, level=logging.DEBUG, format='%(asctime)s:%(levelname)s:%(filename)s->%(funcName)s:%(message)s', datefmt='%m/%d/%Y_%I:%M:%S_%p')
logging.info('\nCurrent Folder: {}\nLog Folder: {}\nLog File: {}'.format(current_folder, log_folder, log_file))
When I run the script manually, it works fine and this is what I get in the log file at /home/pi/logs_rrd/update_rrd_data_201709271426.log:
Current Folder: /home/pi
Log Folder: /home/pi/logs_rrd
Log File: /home/pi/logs_rrd/update_rrd_data_201709271426.log
However, when I run it as a cron job, the log files are created in the same folder as the script instead of the logs_rrd folder. This is what I get in the log file at /home/pi/update_rrd_data_201709271445.log:
Current Folder: /home/pi
Log Folder: /home/pi/logs_rrd
Log File: /home/pi/update_rrd_data_201709271445.log
I can't figure out why the log folder value is ok but then the log file removes the logs_rrd part of log folder.
This is how I run the cron:
*/5 * * * * /usr/bin/python /home/pi/update_rrd_data.py
All the folders and files are owned by root so there shouldn't be any write permission issues.