How to Schedule Tasks/Jobs in CentOS 8

How to Schedule Tasks/Jobs in CentOS 8

What Do I Need?

  • [tool]A Dedicated or VPS Linux Server[/tool]
  • [tool]CentOS[/tool]

What is Cron?

[openingText]Scheduling tasks/jobs in Linux operating systems rely on Cron. Cron or ‘cron job’ is a time-based task scheduler that users can use to set up and maintain software environments. Usually used to automate systems maintenance or administration, Cron is mostly suitable for scheduling repetitive tasks.[/openingText]

  1. [stepName]List Scheduled Cron Jobs[/stepName]
    [step]
  1. [howToDirection]To display the currently scheduled Cron jobs for the current server, use the following command:
crontab -l

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage][/howToDirection]

  1. [howToDirection]If you need to check the Cron jobs running for a different user, apply the following command:
sudo crontab -u -l

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage][/howToDirection][/step]

  1. [stepName]Edit CronTabs[/stepName]
    [step]
  1. [howToDirection]Editing your crontab file can help a great deal when trying to accomplish certain tasks:
crontab -e

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage]

  1. Each line in the crontab script defines a task. A breakdown of the crontab entries below:
<minute> <hours> <day_of_month> <month> <day_of_week> <command_to_run>
  1. Below is a list of all the possible values for these fields. If you’ve used an asterisk instead of a numeric value, it means that every possible value will be used:
minute: 0 to 59

hours: 0 to 23

day of the month: 1 to 31

month: 1 to 12

day of the week: 0 (Sunday) to 6 (Saturday)

[/howToDirection][/step]

  1. [stepName]Using At – the Alternative to Cron[/stepName]
    [step]
  1. [howToDirection]Cron is by far the most popular scheduling tool; however, there’s another and it’s called AT. It’s actually relatively easier to use. Bonus, you can use certain keywords like ‘midnight’ or ‘teamtime’ (4pm). It isn’t pre-installed in most Linux distros though it’s really easy to install in any case.[/howToDirection]
  2. [howToDirection]For CentOS use the following command:
yum install at

[/howToDirection]

  1. [howToDirection]For Ubuntu use the following command:
sudo apt install -y at

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage][/howToDirection]

  1. [howToDirection]After the installation is complete be sure to enable the daemon, or service:
sudo systemctl enable --now atd.service

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage][/howToDirection]

  1. [howToDirection]It works a little differently. When you call ‘at’ you have to tell it the interval of running the task. For example, to run a certain command after an hour, use the following:
at now + 1 hour

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage][/howToDirection]

  1. [howToDirection]To run a specific task at 6pm, six days from now, run the following command instead:
at 6pm + 6 days

[stepImage]How to Schedule Tasks/Jobs in CentOS 8[/stepImage]

  1. To exit from the at prompt use ‘Ctrl + D’. ‘At’ will show a summary of the tasks you’ve scheduled and the time that they’ll be executed.[/howToDirection][/step]

Next Steps

I’d recommend taking steps to restrict access to Cron as this is a powerful tool in the right and wrong hands. It can be used to negatively impact your system; for example, a bad actor could use cron jobs to continually reinstall a script on a website on your server which permits user escalation outside of root. So matter how many times you fix the problem, it just keeps on coming back.

  • To allow users to access crontabs, /etc/cron.allow and /etc/cron.deny files can be used to allow or deny access respectively. Simply put one username in either of the 2 files to allow or deny access to crontab.
  • If the /etc/cron.allow file exists then the /etc/cron.deny file won’t be used. It’s either-or people.
  • In the default installation only an empty file /etc/cron.deny will exist.
  • If neither of the files exists then only the root user will be given access to schedule a job through Cron.

Conclusion

In conclusion, I’d suggest keeping an eye on this. Monitor it like you would your Xbox controller as no one wants to share if they can help it. Leaving access to cron unrestricted can potentially lead to a hostile takeover of your web server and worse, your client’s data.

How To Examine And Repair A Database in PhpMyAdmin

Over the period of time, database can be corrupted and needs to repaired. This t
less than a minute
Elizabeth Grom
Elizabeth Grom
Hosting Expert

How To Find and Install A New Theme in Drupal 8

This tutorial will help you replace the default Drupal theme with your preferred
less than a minute
Elizabeth Grom
Elizabeth Grom
Hosting Expert

How To Create Views in Drupal 8

This tutorial is compiled to help you create a view in your Drupal 8 platform.
less than a minute
Elizabeth Grom
Elizabeth Grom
Hosting Expert

How To Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, usernam
less than a minute
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert
Click to go to the top of the page
Go To Top
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.