Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Scheduling tasks with cron

#1
Scheduling tasks with cron

Cron is a scheduling daemon that executes tasks at specified intervals. These tasks are called cron jobs and are mostly used to automate system maintenance or administration tasks. For example, you could set a cron job to automate repetitive tasks such as backing up database or data, updating the system with the latest security patches, checking the disk space usage, sending emails, and so on. The cron jobs can be scheduled to run by the minute, hour, day of the month, month, day of the week, or any combination of these.

Some advantages of cron


These are a few of the advantages of using cron jobs:

  • You have much more control over when your job runs i.e. you can control the minute, the hour, the day, etc. when it will execute.
  • It eliminates the need to write the code for the looping and logic of the task and you can shut it off when you no longer need to execute the job.
  • Jobs do not occupy your memory when not executing so you are able to save the memory allocation.
  • If a job fails to execute and exits for some reason it will run again when the proper time comes.

Installing the cron daemon


Luckily Fedora Linux is pre-configured to run important system tasks to keep the system updated. There are several utilities that can run tasks such as cron, anacron, at and batch. This article will focus on the installation of the cron utility only. Cron is installed with the cronie package that also provides the cron services.

To determine if the package is already present or not, use the rpm command:

$ rpm -q cronie Cronie-1.5.2-4.el8.x86_64

If the cronie package is installed it will return the full name of the cronie package. If you do not have the package present in your system it will say the package is not installed.
To install type this:

$ dnf install cronie

Running the cron daemon


A cron job is executed by the crond service based on information from a configuration file. Before adding a job to the configuration file, however, it is necessary to start the crond service, or in some cases install it. What is crond? Crond is the compressed name of cron daemon (crond). To determine if the crond service is running or not, type in the following command:

$ systemctl status crond.service
● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor pre> Active: active (running) since Sat 2021-03-20 14:12:35 PDT; 1 day 21h ago Main PID: 1110 (crond)

If you do not see something similar including the line “Active: active (running) since…”, you will have to start the crond daemon. To run the crond service in the current session, enter the following command:

$ systemctl run crond.service

To configure the service to start automatically at boot time, type the following:

$ systemctl enable crond.service

If, for some reason, you wish to stop the crond service from running, use the stop command as follows:

$ systemctl stop crond.service

To restart it, simply use the restart command:

$ systemctl restart crond.service

Defining a cron job


The cron configuration


Here is an example of the configuration details for a cron job. This defines a simple cron job to pull the latest changes of a git master branch into a cloned repository:

*/59 * * * * username cd /home/username/project/design && git pull origin master

There are two main parts:

  • The first part is “*/59 * * * *”. This is where the timer is set to every 59 minutes.
  • The rest of the line is the command as it would run from the command line.
    The command itself in this example has three parts:
    • The job will run as the user “username”
    • It will change to the directory
      /home/username/project/design
    • The git command runs to pull the latest changes in the master branch.

Timing syntax


The timing information is the first part of the cron job string, as mentioned above. This determines how often and when the cron job is going to run. It consists of 5 parts in this order:

  • minute
  • hour
  • day of the month
  • month
  • day of the week

Here is a more graphic way to explain the syntax may be seen here:

 .---------------- minute (0 - 59) | .------------- hour (0 - 23) | | .---------- day of month (1 - 31) | | | .------- month (1 - 12) OR jan,feb,mar,apr … | | | | .---- day of week (0-6) (Sunday=0 or 7) | | | | | OR sun,mon,tue,wed,thr,fri,sat | | | | | * * * * user-name command-to-be-executed 

Use of the asterisk


An asterisk (*) may be used in place of a number to represents all possible values for that position. For example, an asterisk in the minute position would make it run every minute. The following examples may help to better understand the syntax.

This cron job will run every minute, all the time:

 * * * * [command] 

A slash (/) indicates a multiple number of minutes The following example will run 12 times per hour, i.e., every 5 minutes:

*/5 * * * * [command]

The next example will run once a month, on the second day of the month at midnight (e.g. January 2nd 12:00am, February 2nd 12:00am, etc.):

0 0 2 * * [command]

Using crontab to create a cron job


Cron jobs run in the background and constantly check the /etc/crontab file, and the /etc/cron.*/ and /var/spool/cron/ directories. Each user has a unique crontab file in /var/spool/cron/ .

These cron files are not supposed to be edited directly. The crontab command is the method you use to create, edit, install, uninstall, and list cron jobs.

The same crontab command is used for creating and editing cron jobs. And what’s even cooler is that you don’t need to restart cron after creating new files or editing existing ones.

$ crontab -i

This opens your existing crontab file or creates one if necessary. The vi editor opens by default when calling crontab -e. Note: To edit the crontab file using Nano editor, you can optionally set the EDITOR=nano environment variable.

List all your cron jobs using the option -l and specify a user using the -u option, if desired.

$ crontab -l
$ crontab -u username -l

Remove or erase all your cron jobs using the following command:

$ crontab -r

To remove jobs for a specific user you must run the following command as the root user:

$ crontab -r -u username

Thank you for reading. cron jobs may seem like a tool just for system admins, but they are actually relevant to many kinds of web applications and user tasks.

Reference


Fedora Linux documentation for Automated Tasks



https://www.sickgaming.net/blog/2021/04/...with-cron/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  Fedora - PipeWire: the new audio and video daemon in Fedora Linux 34 xSicKxBot 0 5 Yesterday, 05:36 PM
Last Post: xSicKxBot
  Fedora - Contribute to Fedora Kernel 5.12 Test Week xSicKxBot 0 16 05-07-2021, 04:58 PM
Last Post: xSicKxBot
  Fedora - Introducing the Fedora i3 Spin xSicKxBot 0 20 05-05-2021, 07:51 PM
Last Post: xSicKxBot
  Fedora - Fedora Linux 34 is officially here! xSicKxBot 0 18 05-01-2021, 06:04 PM
Last Post: xSicKxBot
  Fedora - What’s new in Fedora Workstation 34 xSicKxBot 0 32 04-29-2021, 02:41 PM
Last Post: xSicKxBot
  Fedora - How to rebase to Fedora 34 on Silverblue xSicKxBot 0 30 04-28-2021, 03:17 PM
Last Post: xSicKxBot
  Fedora - Contribute at the Fedora 34 CoreOS Test Day xSicKxBot 0 32 04-23-2021, 06:03 PM
Last Post: xSicKxBot
  Fedora - Something bugging you in Fedora Linux? Let’s get it fixed! xSicKxBot 0 38 04-19-2021, 12:26 PM
Last Post: xSicKxBot
  Fedora - Fedora Workstation 34 feature focus: Btrfs transparent compression xSicKxBot 0 43 04-15-2021, 12:23 PM
Last Post: xSicKxBot
  Fedora - Contribute at Fedora Linux 34 Upgrade, Audio, and Virtualization test days xSicKxBot 0 44 04-06-2021, 10:27 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] How to Check Your TensorFlow Versi...
Last Post: xSicKxBot
Today 01:57 AM
» Replies: 0
» Views: 5
(Indie Deal) FREE Slash It, Resident Evi...
Last Post: xSicKxBot
Today 01:56 AM
» Replies: 0
» Views: 5
News - Katy Perry’s Pokémon Song ‘Electr...
Last Post: xSicKxBot
Today 01:56 AM
» Replies: 0
» Views: 5
News - Here’s A Look At The Rockin’ Open...
Last Post: xSicKxBot
Today 01:56 AM
» Replies: 0
» Views: 6
MiniGDX Kotlin Game Development Framewor...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 7
Mobile - Mad Skills Motocross 3’s traile...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
Fedora - PipeWire: the new audio and vid...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
News - Random: Check Out These “Mock-Ups...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
News - Video: Pokémon Fan Reimagines The...
Last Post: xSicKxBot
Yesterday 05:36 PM
» Replies: 0
» Views: 5
News - Where Is Xur Today? (May 14-18) -...
Last Post: xSicKxBot
Yesterday 05:35 PM
» Replies: 0
» Views: 75

[-]
Twitter

Copyright © SickGaming.net 2012-2020