Job Automation with "cron" and "at" in linux

Β·

4 min read

Automation with cron and at

  • As we know DevOps comes as a solution for automation. So, as a DevOps Engineer knowing job automation is mandatory. So, Today we are going to learn about Job Automation. with that said, let's go...

    • If we want to make a particular task reoccur, then this process is known as Job Scheduling and is usually done based on some defined tasks(jobs).

    • For Linux family os, This process is handled by the cron service or a daemon called crond which can be used to schedule tasks (also known as jobs)

Advantages of cron job :

  • You have complete control means can change the minutes, hours, days etc that it will execute.

  • Easier to write the code and manage its operation. It eliminates the looping and timing logic in the task.

  • It will not occupy memory in your system when it's not running.

  • If it fails and exits for some reason, It will start up again when the proper time comes

  • Advantages of infinite loop

  • Even if our system is off but still it is running in the background. But, make sure that the server is not to be shut down.

Disadvantages of the cron job :

  • cron jobs need monitoring. let's see what it means by considering the below scenarios. - if the cron job sends emails every day, the developer has to check the message every day. When there is a huge number of cron jobs running. Daily visual monitoring becomes inefficient.

  • users cannot know the existence of cron-related functionalities since they do not appear in the interface.

  • New developers have to remember to look in the system directories to check if there are cron jobs

  • cron jobs and related scripts need to be tested.

  • No version control. Even if someone removes a job, there's no easy way to track who did it and the reason for it.

  • performance issues. Creating more and more cron jobs in a single machine can overload the machine at some point.

  • /etc/crontab is the file that stores all scheduled jobs

  • /etc/cron.deny is the file used to restrict the users from using cron jobs

  • /etc/cron.allow is used to allow only users whose names are mentioned in this file to use cron jobs. (Doesn't exist by default)

  • /etc/at.deny same as cron. deny restricting at jobs.

  • /etc/at.allow same as cron. allow for allowing users to use at jobs.

Crontab Format

  • To assign a job in the crontab file the format used is as follows :

image

optionExplanation
*This means being treated as a wild card. meaning any possible value.
*/3It is treated as for every 5 minutes, hours, days, or months. Replacing 3 with another numerical value will change this option
2,4,6Treated as an OR, so if placed in hours, this could mean at 2,4, or 6 o-clock
2-10Treats for any value between 2 and 10. so if placed on day of the month this would be days 2 through 10. or if put in hours it would be between 9 and 5.

CRON JOBS :

To check the assigned cron jobs of currently logged-in user

#crontab -l

To check the assigned cron jobs of a particular user

#crontab -l -u <username>

AT JOBS :

  • "at" is used to schedule the job for a particular time or interval. To make it simple, It is used for only one time or only one interval.

The disadvantages of at jobs are

  • It can be modified like cron jobs

  • It cannot be reused

  • the content is not possible to view in a normal human-readable format

Restricting a user from using jobs

  • To restrict a user from using at jobs is exactly the same as a cron job

  • Add user names to /etc/at.deny, it will work exactly like /etc/cron.deny

  • To allow only a few out of many users to remove at. deny as we did for cron. deny and create /etc/at.allow and add user names who are allowed to use at jobs in it, like cron. allow

  • If both at. allow and at.deny exists, then at. allow will have higher priority.

  • If neither at.allow nor at.deny exits, then only root can use at jobs.

That's it for this Article. I will see you in the next article with more learning and more interesting topic.

Until then keep reading, keep learning, and Keep Succeeding...

Don't forget to leave feedback by like πŸ‘, share🀝, commenting πŸ‘‡

Thankyou πŸ‘‹

Did you find this article valuable?

Support CharanWrites by becoming a sponsor. Any amount is appreciated!

Β