PROJECTS NOTES HOME

Cronjob test job

Read info about cron: vim /etc/crontab

cron helper - Crontab.guru - The cron schedule expression editor

1 fix serve time

2 create a python file

import datetime

# Get the current timestamp
current_time = datetime.datetime.now()

# Create a formatted message
message = f"This is a test cron job. Current time: {current_time}"

print(message)

Make it executable

chmod o+x my_script.py

3 create a cronjob

crontab -e
# this runs python code every minute and outputs the print message to the file
 * * * * * /usr/bin/python3 /home/arvy/src/cronjobs/my_script.py >> /home/arvy/src/cronjobs/output_my_script.log 2>&1

Log file looks like this after running for a few minutes:

This is a test cron job. Current time: 2023-07-11 12:32:01.462375 This is a test cron job. Current time: 2023-07-11 12:33:01.501960 This is a test cron job. Current time: 2023-07-11 12:34:01.576551