Post

Crontab Basics

Crontab Basics

To schedule jobs in Linux, use crontab. For more details, see this YouTube guide: https://www.youtube.com/watch?v=QZJ1drMQz1A

Setting up a Cron Job

To setup a cron job use the following commands.

  1. Use the following command to edit your existing crontab jobs, if any.
    1
    
    crontab -e
    
  2. Once you create a job and save it, use this command to list the jobs that are active.
    1
    
    crontab -l
    

Creating a Cron Job

  1. After running crontab -e, add your command to the file. Here is an example command that runs Every Hour, on the hour Monday through Friday. See this link to read the cron job: https://crontab.guru/#00__1-5. Then this script writes the logs to this log file with a date and time: /home/ec2-user/xlogs/tvcron-`date +\%Y\%m\%d\%H\%M\%S`.log.
    1
    
    0 0 * * 1-5 ./tradingview_automator.py >> /home/ec2-user/xlogs/tvcron-`date +\%Y\%m\%d\%H\%M\%S`.log 2>&1
    
  2. To automate removing your older log files, add this command to crontab. This removes any file older than 10 days.
    1
    
    0 7 1,15 * * find /home/ec2-user/xlogs/tvcron-* -type f -mtime +10 | xargs rm
    
This post is licensed under CC BY 4.0 by the author.