2

I am working on a project that pulls data to a database, and we'd like to use Anacrontab to automate the running of this program. I've created a dummy program (located in /etc/ directory) that simply appends a timestamp every time the program was run. I've realized I need to have a shell file to use it in anacron, so I created a shell file that simply runs the python program (located in /etc/ directory).

[root@atlantic etc]# cat joey_dummy_python_program.py (My Python Program for automation)

import time
timestr = time.strftime("%Y%m%d-%H%M%S")
# Open a file with access mode 'a'
file_object = open('sample.txt', 'a')
# Append timestamp at the end of file
file_object.write('\n'+'Program Run at: '+timestr)
# Close the file
file_object.close()

[root@atlantic etc]# cat joey_dummy_python_program.sh (.sh file to simply run the python file)

#!/bin/bash
python3 /etc/joey_dummy_python_program.py 
[root@atlantic etc]# 

when I invoke my own program (the python program alone and bash program invoking the python program), I see that it appended timestamps for those runs, so I know the python program and bash one both work... however when I setup a anacron job with the files mentioned above, and ran it I didn't see any corresponding timestamps appended so my job is not working... (see below)

# /etc/anacrontab: configuration file for anacron
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1       5       cron.daily              nice run-parts /etc/cron.daily
7       25      cron.weekly             nice run-parts /etc/cron.weekly
@monthly 45     cron.monthly            nice run-parts /etc/cron.monthly
@daily  0       example.daily   /bin/bash /etc/joey_dummy_python_program.sh

Next, I forced my anacron entry to be run with the -f and -n parameters to force all jobs to be executed immediately while disregarding timestamps and delays... then I checked my file that would show the timestamp of any time the program was run... no new timestamps were appended for when the anacron job was started. (see both below...)

[root@atlantic etc]# sudo anacron -fdn
Anacron started on 2022-02-15
Will run job `example.daily'
Will run job `cron.daily'
Will run job `cron.weekly'
Will run job `cron.monthly'
Jobs will be executed sequentially
Job `example.daily' started
Job `example.daily' terminated
Job `cron.daily' started
Job `cron.daily' terminated
Job `cron.weekly' started
Job `cron.weekly' terminated
Job `cron.monthly' started
Job `cron.monthly' terminated
Normal exit (4 jobs run)
---------------------------------------------------------------------
[root@atlantic etc]# cat sample.txt
This program appends a time stamp with each time it was run, created to test anacron.


Program Run at: 20220215-184204
Program Run at: 20220215-184216
Program Run at: 20220215-185559
----
after set up of anacron job:

Does anyone have any insight as to why this Anacron job may not be working? Once I get this working I will apply what I learned directly to my real python program I want automated.

Thanks for reading.

0 Answers0