Skip to main content

Have you ever wondered how to start a program or script on a schedule in Linux? Your Linux distribution probably has several chronological tasks to keep it running smooth. You can set these task to any moment of the day, week, month, or year. Creating a program or script is one thing. Often, you will find yourself running it in the command line. Today we say not anymore. Why do something a computer can do on its own? After all, aren’t they here to make our lives easier?

First, we should cover some basics. Then we will show you a cool site that produces cron jobs with a web-based GUI. We will show you how to use it to start a Python program. Let’s break out the slippers, jump into the lazy chair and code some time into our day.

BASICS

We will start by showing you how to edit your crontab. This will also create one if you do not already have one.

crontab -e
edit Crontab

You will be given a choice of editors to view it in. Choose whatever you know how to use best out of the choices. We will be using Vim as always. Notice we have a tone of comments explaining many features that can be used. This includes an example to help you understand how to write the commands correctly in line with the file to be executed. Next, we will show you how to list the contents of your crontab.

crontab -l
Show Crontab

Here you will have one of two answers. You may get a message saying your user does not have a crontab. Otherwise, you will see the contents of your crontab. Now we will show you how to remove a crontab.

crontab -r

You probably do not want to use this command unless you really want to nuke your crontab. Generally, it is a good idea to just edit your crontab to get rid of anything undesirable.

Below we have a diagram to help you visualize the meaning of the commands along with the sensitivity of their placements.

* * * * * command execution line
| | | | |
| | | | \_ _ day of week (0 – 6) (Sunday=0)
| | | \_ _ month (1 – 12)
| | \_ _ day of month (1 – 31)
| \_ _ hour (0 – 23)
\_ _ minute (0 – 59)

Notice the placement of the asterisk, they coincide with the placement of the commands. They are also program excepted placeholders. Each placement is explained by matching variables with their coinciding length of time. Now we will show you an example of what that might look like.

10 * * * * sh .bash/bin/test.sh > .log/cron.log

Notice here we are setting this task for the 10-minute mark of every hour. Leaving the other time fields as asterisk. Then we have the location of the script we want to execute. Following this, we are indicating that we would like a log stored in a specified location. We will use logs going further to help us know if our test are successful.

CRONTAB GENERATION

Writing a crontab was very hard to wrap my head around the first time. So to give you a good jumping off point, we will mention crontab generator. This site offers you a GUI to interact with. You make your choices as to when to run the executable file, then give it the location in your system of where the file resides. Click “Generate Crontab Line”, and then you copy and paste the line into your crontab. It’s great when you have zero experience, or perhaps you’re a bit lazy. Either way, this tool will help you grow and learn to write them yourself.

Crontab Generator

CACHE KILLER

Now we will demonstrate how to use your crontab to run a task as simple as deleting cache on a schedule. We will use a simple program to delete cache files. You can run this code directly on your server or workstation, since Python is included as part of many distributions. We will keep it simple as we are talking about crontab and not Python. Let’s create a directory and then create a file to write to.

mkdir test && cd test && touch cache_kill.py

We will also need some test cache files to test with.

mkdir cache && cd cache && touch cache-1.cache  cache-2.cache  cache-3.cache  cache-4.cache

Now we will edit our python file using Vim.

vi ~/test/cache_kill.py

Now we will type a to edit the file we just created.

a

Now, we will copy the code below and paste it into your editor.

import os
from os import listdir
  
folder_path = '~/test/cache/'  #Write path to files you might want deleted
  
for file_name in listdir(folder_path):
  
    if file_name.endswith('.cache'):  #Write the extension of the files you want deleted
  
        os.remove(folder_path + file_name)

Now we will save and close Vim. First, we type Esc to stop editing.

Esc

Now we type will ZZ to save and close the editor with edits.

ZZ

We will explain the code, as you should never just copy and paste strange programs into your system. The first few lines import the needed packages to run this program. In this case, that is os and from os, listdir. The next line tells where to point this program by defining the path. The next line tells listdir about that file path. The following line tells what file extensions to delete in the defined file path. Finally, we come to the line that tells our operating system to remove the files that end with the defined file extension along the defined file path. Now let’s schedule this to happen within a reasonable test time. This way we don’t have to wait weeks or months to see our progress.

crontab -e

We used Vim to edit this file, so we will need to type a to edit the crontab.

a

Now we will paste in the following in to make this program delete our test cache files every 15 minutes. You can see the time code at the beginning. Then you come to where you call the Python script. Then we are using >> to log every time it runs to the log file specified at the end.

*/15 * * * * python3 ~/test/cache/cache_kill.py >> ~/test/cache_clear.log 2>&1
Crontab Example

Now we will save and close Vim. First, we type Esc to stop editing.

Esc

Now we type will ZZ to save and close the editor with edits.

ZZ

Now we wait 15 minutes to see if all our files are deleted as scheduled. You may also look to see if the log file is created in your test directory. Finally, you might not want this to run every 15 minutes from now on. So let’s clean up our crontab.

crontab -r

You can keep your test directory now for an example. This program can delete any file extension you feed it. It comes in handy when changing image formats. You may also decide to run a Bash script in place of a Python program. Simply use it in place of the Python program. Thanks for reading. We hope you like your new crontab powers!

Leave a Reply