afshin
afshin

Reputation: 1833

Create cron job on aws ebs to run a python script

I have a Elastic BeanStalk server on aws being used as a JSON server. The data files need to be updated daily. I have a python script that I want to run every weekday to update the data files. I created .ebextensions folder and created cronjob.config below. I want to run update_data_files.py daily. I used the template for cronjob.config from : https://aws.amazon.com/premiumsupport/knowledge-center/cron-job-elastic-beanstalk/

Question: How do I run this script as cronjob? what is the path to the uploaded folder in ebs so that the python script will run?

python3 update_data_files.py

.....cronjob.config.....

files:
    "/etc/cron.d/mycron":
        mode: "000644"
        owner: root
        group: root
        content: |
            5 16 * * 1-5 root /usr/local/bin/myscript.sh
    "/usr/local/bin/myscript.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/bin/bash
            date > /tmp/date
            # Your actual script content

            python3 update_data_files.py

            exit 0
commands:
    remove_old_cron:
        command: "rm -f /etc/cron.d/*.bak"

Upvotes: 1

Views: 1706

Answers (1)

arudzinska
arudzinska

Reputation: 3331

First of all, the cronjob.config file describes two files: /etc/cron.d/mycron and /usr/local/bin/myscript.sh.

The first file is placed in the /etc/cron.d directory which is one of the places where you put your cron jobs in Linux. mycron file is your cron job in this case, it contains the line:

5 16 * * 1-5 root /usr/local/bin/myscript.sh

which instructs to run myscript.sh at 16:05 from Monday to Friday (as a root user). After placing a file in this directory cron will check it automatically and run the jobs at specified times. You don't need to do anything more.

The second file, /usr/local/bin/myscript.sh, is the one that is run by the cron job.

Considering the question where you can find these files - you provided the full paths to them in their names above.

The crons which were setup in the /etc/cron.d directory cannot be viewed with the crontab -l or sudo crontab -l commands, but this doesn't mean that they won't run - the files in this directory are constantly monitored and parsed as cron jobs.

If you think your script is not being executed, make a simple test by modifying your cron job. For e.g.:

* * * * * root echo $(date) >> /tmp/cron.log; /usr/local/bin/myscript.sh

This job will print the current date and time (every 1 minute) to the cron.log file created in /tmp. After that it will try to execute your command. You can check whether the cron jobs are executed at all by checking this log file.

Then, if your command silently fails, you can redirect the standard output and standard error of your script to some log file to check what goes wrong with its execution.

Upvotes: 2

Related Questions