How to Schedule Tasks/Jobs in CentOS 8

How to Schedule Tasks/Jobs in CentOS 8

What Do I Need?

  • A Dedicated or VPS Linux Server
  • CentOS

What is Cron?

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.

  1. List Scheduled Cron Jobs
  1. To display the currently scheduled Cron jobs for the current server, use the following command:
crontab -l

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

  1. Edit CronTabs
  1. Editing your crontab file can help a great deal when trying to accomplish certain tasks:
crontab -e

  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)

  1. Using At – the Alternative to Cron
  1. 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.
  2. For CentOS use the following command:
yum install at

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

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

  1. 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

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

  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.

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 Set up a VSFTPD Server on a CentOS 7 VPS or Dedicated Server

Brief Description FTP is usually insecure exposing clear-text passwords, userna
2 min read
Avi Ilinsky
Avi Ilinsky
Hosting Expert

How To Set up a VSFTPD Server on an Ubuntu 16.04 VPS or Dedicated Server

Brief Description FTP data is usually insecure since information (usernames, pa
2 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to use phpMyAdmin to develop a website (without MySQL experience)

Brief description A web developer who is not well versed into coding websites f
2 min read
Idan Cohen
Idan Cohen
Marketing Expert

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester
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.
Click to go to the top of the page
Go To Top