Schedule Your Scripts Like a God

Sam Ozturk
2 min readAug 10, 2022

--

Chronos-Greek god of time

Cron Jobs

Cron jobs are a utility available in UNIX-like systems to schedule jobs or scripts. The name comes from Greek god of time(well actually Titan, not a god), Chronos who is the predecessor of Zeus. To schedule a job, you will use a standard crontab schedule format below.

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * * command_to_execute

Some example cron jobs will look like:

  • Empty temp folder every Friday at 5pm 0 5 * * 5 rm -rf /tmp/*
  • Backup images to Google Drive every night at midnight 0 0 * * * rsync -a ~/Pictures/ ~/Google\\ Drive/Pictures/

Scheduling a Cron job

You need to add the commands to cron by typing crontab -e which will open up a vim editor.

Active cron jobs can be seen with crontab -l command and can be remove all by crontab -r.

Another user’s cron jobs can be edited using crontab -u <user_name> -e .

Multiple time schedules

  • Run the command at 1st and 15th day of the month at midnight: 0 0 1,15 * *
  • Run the command at every ten minutes */10 * * * *
  • Run the command at every hour from midnight to 5 AM everyday 0 0-5 * * *
  • Every 30 minutes in business hours: */30 9-17 * * 1-5

--

--

Sam Ozturk
Sam Ozturk

Written by Sam Ozturk

AI Engineer & Data Padawan. Non-technical post are at https://medium.com/@confused_matrix

No responses yet