• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / Linux / RH134 / Scheduling Jobs Using Cron

Scheduling Jobs Using Cron

December 17, 2019 by The Urban Penguin

RHCSA 8 Study Guide

For many years Linux administrators have relied on scheduling jobs using cron. The main system configuration file for the crond is /etc/crontab but we also have the extension directory, /etc/cron.d and directories that we can add scripts into for execution, /etc/cron.hourly, /etc/cron.daily and so forth. Users may create their own crontabs with the command crontab -e. The following shows the main /etc/crontab file:

# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- 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,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

If we need to schedule a task to run each weekday, Monday to Friday at 15:30 we can add a new file below /etc/crond.d/. Working as root we create the file:

# echo "30 15 * * 1-5 root df -h \
> /tmp/diskfree" > /etc/cron.d/diskfree

To schedule a task to run every 10 minutes to cron file may look similar to this:

# echo "*/10 * * * * root df -h \
> /tmp/diskfree10" > /etc/cron.d/diskfree10

The system cron files include the additional field for the user account context to use when executing the task. As an alternative to scheduling jobs using cron system files, we can also use the crontab command to create and manage user cron jobs which don’t use the user field. Just as with at, we can control access to user crons using the /etc/cron.deny and /etc/cron.allow files. By default only the /etc/cron.deny file exists out of the two files and is empty. An empty deny files then will deny no user accout but we adding a list of names, one per line will deny thoses listed users. Creating the file /etc/cron.allow will grant access only to the listed users in the allow file. An empty /etc/cron.allow file allows access to no user, which is the same as denying acces to all.

The command crontab is used to manage user crontabs. When editing user files we don’t include the user field as the account context is always the owner of the crontab file.

To edit or create a new crontab for your own account:

$ crontab -e

To edit or create a new crontab for another user we work as root:

$ sudo crontab -e -u user1

To list a user crontab we use crontab -l and to delete a user crontab it is crontab -r. The default is to work with the current user but, as root, we can use the option -u to work in the context of another user.

If scheduled jobs are not executed because the system was down it is the crond makes reference to the /etc/anacrontab file. Previous to RHEL 8, this was handled by a separate service but was migrated to crond with the release of RHEL 8. It is also this file that causes the execution of the scripts in the /etc/cron.{time} directories.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: RH134

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Linux Technologies
    • Apache HTTPD Server
    • Learning PHP
    • Learning PUPPET
    • Learning SAMBA
    • Linux File-Systems
    • Monitoring with Nagios Core
    • MYSQL
    • openLDAP Directories on Linux
  • LPI Training from The Urban Penguin
    • Complete Linux Essentials
    • Live and Pluralsight hosted courses
    • LPI Linux Essentials
    • LPI Linux Essentials for Raspberry Pi
    • LPIC-1 Linux Administrator
    • LPIC-2 Certified Linux Engineer
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
  • Online Instructor-led Courses
    • Bash Scripting Masterclass
    • Nftables Masterclass
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
  • OpenStack
    • Citrix Videos
    • Pluralsight
    • Raspberry Pi Tutorials
    • Udemy
  • Operating System Tutorials
    • Learning SUSE
    • Learning Ubuntu
    • Linux Foundation Training
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
    • Solaris 11 OCA 1ZO-821
  • Scripting – the power of repetition!
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
    • Learn C Programming using Linux and the Raspberry Pi
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell

© 2022 The Urban Penguin · All Rights Reserved