Linux what is cron

Linux Jargon Buster: What is a Cron Job in Linux?

In this part of our Linux Jargon Buster, you’ll learn about cron in Linux. You’ll also learn the basics of creating cron jobs by editing crontab.

In this part of our Linux Jargon Buster series, you’ll learn about cron in Linux. You’ll also learn the basics of creating cron jobs by editing crontab.

What is a cron job in Linux?

Cron is a command line utility to run small and quick commands on a scheduled basis. This is a handy, classic sysadmin tool for automating various tasks by combining it with othe tools. For example, some people combine rsync and cron to automatically create a daily or weekly backup at a certain time. Some people use it to analyze server logs and combine it with mail function to send an email if there is certain kind of error detected in the logs. Cron is like the Swiss army knife. It can be used for a variety of use cases. It’s really up to your imagination on what to use it for. Getting started with cron is super easy, and only takes a matter of seconds to get started. But before I show you that, I’ll discuss something else that often confuses Linux users.

Cron vs cron job vs crontab

You are likely to come across three terms that sound similar to each other: cron, cron job and crontab. Let me quickly tell you what are those: cron: This is the actual program you install on your system and run as a daemon. cron job: A job in Linux is a program that is up and running. Cron can handle multiple tasks and run them at their scheduled time. Each of these tasks are referred to as ‘cron jobs’. crontab: This is the file (and command) where you define what task to run and how often to run it. A crontab can have multiple cron jobs in it in a tabular form where each row is a cron job. Let me share a sample cron job example that runs every hour and prints “Linux is cool!” to a file name crontab_log.txt.

0 * * * * echo "Linux is Cool!" >> ~/crontab_log.txt

Even the simplest of the cron job may look scary and intimidating. This is because you need to know how to read a cron job properly. I’ll take this theoretical knowledge of cron to the next level in the next section.

Getting started with Cron

Let’s start with (another) simple example of how Cron works. To create Cron jobs, or the commands that cron will execute, you simply run:

crontab file

It will pull up a file to edit cron jobs with: All the lines that start with # (that being all the lines) only serve to help guide you on how to use cron, and can be removed if you don’t need them. We’ll be using the following as our first Cron job though:

  • minute in this context simply means what minutes of an hour the job will run. So specifying 0 would run at the very beginning of an hour, and specifying 5 would run on the fifth minute of an hour.
  • Next, the hour declaration specified what hours of a day a job can run, ranging from values 0-23. The reason there isn’t a 24th hour option here is that 23 goes to the very end of what would be 11:59, at which point your at midnight, or hour 0 of the next day. This same logic applies to the minute declaration mentioned previously.
  • day specifies what day of a month a job can run on, that being 1-31 (unlike the previous examples which started on 0 ).
  • month specifies which months of the year a job can run on, and takes values ranging from 1-12.
  • Lastly, week specifies which days of the week a job runs on, starting on Sunday, with values from 0-6, unlike the last two.
Читайте также:  Open tar gz files linux

You then specify command which is just the command you want to run.

crontab explanation

If you want more help understanding the minute hour day month week part, I’d highly recommend the Crontab guru website, which can greatly help break down what exactly is going on:

Following the previous example of * * * * * touch ~/crontab_test though, you’ll simply be running touch ~/crontab_test every minute.

Let’s put that into the crontab and then see it in action:

cron example 1

If you then wait until the next minute, you’ll see the crontab_test file located in your home directory:

result of cron job

And that’s the basics to using cron!

A useful example of cron job

That was far too simple (and useless probably).

Say you want to do something a bit more complex, say a backup script that needs to copy files from multiple directories on your system into a single folder, and then archives that backup into a single file.

This can be easily done by simply putting our commands into a script, which can then in turn be called by cron.

Let’s use the following script as an example:

#!/usr/bin/bash echo "Backing up. " mkdir -p ~/.local/tmp/ tar -Pc ~/Documents/ -f ~/.local/tmp/backup.gz

This script does the following things:

  1. Makes sure the ~/.local/tmp/backup_dir directory exists on the system.
  2. Puts everything from ~/Documents/ into an archive located at ~/.local/tmp/backup.gz.

Let’s run the script manually first so we can see that it actually works.

First, let’s put the script at ~/backup_script like so:

complex cron example

Then just copy the script contents that were shown above into the file.

You then need to tell the system that our script is allowed to be executed by simply running chmod +x ~/backup_script:

complex cron example 1

You can then test run our script by running ~/backup_script.sh, which simply tells our system the path to our script:

complex cron result

You could then restore this backup by just running tar -xf ~/.local/tmp/backup.gz -C output_dir, where output_dir is the directory to save the files to.

This script can now run on a scheduled basis by using Cron!

For example, if you wanted the script to run daily at 3 a.m., you could use the following syntax in cron:

And then you’d have backups being created on a daily basis.

Wrapping Up

And that’s just the introduction to cron jobs. It is widely used by the sysadmins though I am not sure of many usecases for desktop Linux users. If you do, please suggest some in the comment section.

Читайте также:  Установка сети linux ubuntu

Источник

How to Set Up Cron Jobs in Linux

How to Set Up Cron Jobs in Linux

Sooner or later, you might need to use a cron job to schedule various different repetitive tasks in linux in order to automate your process. For example, you might want to run a backup of your database every day at midnight or you might want to run a script that checks for new emails every hour.

In linux, you can use the cron system to schedule a cron job to run at a specific time.

What is cron?

Cron is a scheduling system within Unix-like systems. A daemon called the crond runs in the background and enables the cron functionality. The cron daemon is responsible for scheduling tasks to run at specific times. This daemon reads your system’s crontab file, or cron table, to read the details of your tasks.

What is cron job?

A cron job is any task that you configured and scheduled. As mentioned before, cron jobs are useful for automating repetitive tasks, workflows, routine tasks, or anything else that you want to automate. If you want to check what users on your machine are already using cron jobs, you can check the same file that crond uses.

Change your directory to this:

That will show you all the users that have configured cron jobs. If you want to check the cron jobs of the user you are currently on, you can do the following:

How to schedule a cron job

Configuring a cron job is simple and follows a structured syntax. This syntax is straight forward but very powerful, allowing you to define precisely what you want to do and when. Before we get started, ensure that you have the cron service installed and running. To do so, run the following command:

If it is not running, you can install it using a package manager of your choice.

  • -l : List all the cron jobs
  • -e : Edit the cron jobs, this includes adding, deleting, editing, and re-ordering cron jobs
  • -r : Remove the cron jobs
  • -u username : This is the flag that will be used to specify the user.

Not that these flags can be combined, for example, you edit another user’s cron jobs by running

Here is an example of a cron job:

  • Minute: The cron job will be executed at the minute specified. You can use * to specify every minute. Valid values are 0-59.
  • Hour: The cron job will be executed at the hour specified. You can use * to specify every hour. Valid values are 0-23.
  • Day of month: The cron job will be executed at the day of month specified. You can use * to specify every day. Valid values are 1-31.
  • Month: The cron job will be executed at the month specified. You can use * to specify every month. Valid values are 1-12.
  • Weekday: The cron job will be executed at the weekday specified. You can use * to specify every weekday. Valid values are 0-6.

The command to run in our example was sh script.sh . This tells our system to use the bash shell to run the command. The sh is a shortcut for /bin/sh . The script.sh is the name of the script that will be executed. This can be any file that you have created.

Читайте также:  Линукс просит пароль администратора которого нету

Here’s a summary of the syntax:

Cron job examples

  • 10 0 * * * : This cron job will run 10 minutes after midnight every day.
  • 30 2 * * 6 : This cron job will run on Sunday at 02:30.
  • 0 10 * * 1-5 : This cron job will run every weekday at 10:00.
  • 5 * * * * : This cron job will run every hour on every 5th minute, every day.
  • 10 0 * 3 * : This cron job will run in March at 00:10.

If you want to learn more about this syntax, and create custom ones of your own, we recommend you this handy tool called the crontab guru.

Set up a cron job

Now that you know the syntax, let’s set up a cron job. Let’s create a cron job simply outputs the current time to a text file. This will help us validate that the cron job is executing as we configured it. Let’s create a script called date.sh .

Then add the following code to the file:

Now we must change the permissions of the file so that it is executable. Use the chmod command to do so.

Finally, use the crontab command to add the cron job to the cron table.

Your cron job should look something like this:

This cron job will run every single minute. This allows us to quickly see results. Make sure that the path is accurate for your system then save the file and exit the crontab editor.

Wait a few minutes, then check the output of our date.txt file.

You should see something like this:

You can confirm this by checking the current system time, which should be the same as the time in the date.txt file.

Troubleshoot Cron jobs

There might come a time when you need to troubleshoot a cron job. It is easy to misconfigure them or they might fail to run at all.

The first thing you should do is check the syntax of the cron job. Once again, you can use the crontab command to check the syntax of the cron job and adding the -l flag.

If that looks good to you, then you can move on to checking the logs of the cron job. This will help you determine if the cron job is running as expected. You can look at the logs in /var/log/syslog or /var/log/cron.log . These files will log the time the job was run, the user, and the command executed. The logs will be prefixed with the word CRON so you can’t miss it. Cross-reference these timestamps with your syntax to confirm that it is running at the times specified.

Here’s an example log entry:

The last thing you can do to help troubleshoot your cron job is to redirect the output of the command to a file. That way, you can see what the command is doing and whether or not it succeeded. To do so, simply modify the command in the cron job, like so:

In our example, we are outputting the results of the command to a file called date.txt . We can then check the file to see if the cron job is working as expected.

Источник

Оцените статью
Adblock
detector