List of content you will read in this article:
Doing the same thing over and over again can be exhausting. So, having something to do for you can be a blessing. Using cron job (time-based job scheduler) lets the users automate their jobs on any Unix-like operating system.
This has several advantages, such as saving time and focusing on more important tasks instead. In this guide, you will learn about this app, the advantages of using a cron job, and how to use it.
What is cron job?
Before getting in deeper into this command-line utility, let’s get to know its meaning as an expression. The word “Cron” in simple words means “period” or “cycle”.
Cron is an application that lets the users input some commands with their schedules so that the tasks can be done without user interference at the right time and in specific periods.
These periods may differ in different systems. A server administrator can define the frequency of each cron using Linux commands. For instance, cron can be 1 minute in one system and a year in another.
These tasks are called cron jobs. Users can specify different task types and the time that the tasks have to be done. This system works non-stop, so you can make it do the tasks anytime you want.
A cronjob is a daemon that describes a background process running non-interactive tasks. This program is a simple text file containing the commands to run at a particular time.
/etc/crontab is the default system crontab or cron table cron configuration file. Only system administrators have access to change or edit the system crontab file.
Unix-like operating systems can handle multiple admins. Each one of the admins can control a crontab file and write the commands to run the tasks at the right time.
Applications of Cron job
This program can be used for different purposes. Since the program is limitless, you can make any use of it. We mention some of the uses below:
- Checking the broken links in the websites
- Deleting cache files
- Monitoring disk space
Advantages of a Cron job
If you’re running a website as an administrator, you may hear about cron job because of its advantages. We mention some of these pros below:
- Saving time
- Saving energy
- Saving manpower
Cron job limits
Any program can have its limits as well as its advantages. Cron job is not an exception either. It’s better to consider these before using it.
60 seconds is the longest time between tasks, and you can’t repeat a job every 59 seconds or less. So it may not be useful for such tasks.
Centralized on only one computer
Cron job can be centralized on only one computer, and you can’t distribute it on multiple computers. So if the computer breaks, the tasks won’t run automatically, and you have to execute them manually.
No options for retrying the mechanism
This program is designed to run the task once at a specific time. If the task fails to run properly, cron won’t execute again until the next scheduled time.
⭐⭐Experience unparalleled control, flexibility, and scalability with our VPS hosting solutions, empowering you to unleash the full potential of your online presence.⭐⭐
You have to understand cron’s syntax and formatting, so you can create a cron job. Crontab syntax has 5 fields that you must fill in first to get started.
The minute when the command will run ranges from 0 to 59.
The hour when the command will run ranges from 0 to 23.
Day of the month
The day of the month the command will run ranges from 1 to 31.
The month when the command will run ranges from 1 to 12. In this system, 1 represents January and 12 will be December.
Day of the week
The day of the week the command will run ranges from 0 to 6. In this system, 0 represents Sunday, and 6 will be Saturday.
You can’t miss even one of the fields; all need to be filled. Now we present an example to show you how this system works. Let’s say you want your cron job to run root/backup.sh every Sunday at 8:45 pm so that the command will be like this:
45 20 * * 0 root/backup.sh
45 and 20 represent 8:45 pm. We have two asterisks in this example in place of the day of the month and month. The asterisks show that this command should be repeated regardless of the date and month. Then we have 0, which represents Sunday in this command.
If writing these commands seems hard, you can use free tools such as Crontab Generator or Crontab.guru to help you fill the fields properly. So your commands will be accurate and without any problems.
Cron job operators
To fill the fields properly and set the right time for each command, you need to know the cron job operators. By using operators, you will specify the values you want to enter in each field.
Using this operator shows that you want the cron job to run the command in every possible value. For instance, using * in the minute field says that the command should be run every minute.
The comma (,)
To list multiple values, you can use commas. For instance, entering 0,6 in the Day of the Week fields means that the command should be run every Sunday and Saturday.
The hyphen (-)
To determine a specific period, you should use this operator. For example, when you want to run a command from January to March using cron jobs, you must write 1-3 in the Month field.
You can divide values using a separator. For example, to set up a cron job every 12 hours, you should fill the Hour field with the script */12.
You can use this operator in the fields of the day-of-month and day-of-week. For example, writing 5L in the day-of-week field shows the last Friday of a month.
Using this operator means that you want the Cron job to run a command on the closest weekday from a specific time. For instance, if the first day of the month falls on a Sunday, typing 2W in the day of month field will run the command on the following Tuesday, which is the 3rd of a month.
This operator specifies the day of the week followed by a number from 1 to 5. For example, 1#3 means the third Monday of the month.
Question mark (?)
You may use this operator in the day of the month field and day of the week field. It shows “no specific value”.
Cron Job Special Strings
Some special strings are simpler to use. You can use these strings instead of the first 5 fields that we mentioned before. Each string determines a specific period. You have to write an @ followed by a phrase to use them:
@hourly: Once an hour.
@daily or @midnight: Every day at midnight.
@weekly: Once a week at midnight on Sunday.
@monthly: Once on the first day of every month.
@yearly: Once a year at midnight on January 1st.
@reboot: Only once at startup.
Cron Job Tips
When configuring cron jobs on your Linux VPS, you should be aware of the potential for CPU and RAM overload, depending on the nature and frequency of the tasks you're scheduling. Here are a few suggestions to better configure your cron jobs and optimize your Linux VPS.
Schedule Jobs Intelligently: If you have multiple heavy tasks, try not to schedule them all simultaneously. Spreading them out will make sure your VPS resources are not overwhelmed.
Use Nice and Ionice: nice and ionice are Linux commands used to manipulate the priority of processes. nice is used for CPU priority, and ionice is for I/O priority. If you have a job that is not particularly important and could potentially use many resources, you could lower its priority with these commands to help keep your server running smoothly.
Monitor Your Resources: Use monitoring tools like htop, top, or vmstat to keep an eye on your system's resource usage. These tools can give real-time feedback and help you identify jobs using too many resources.
Limit Resource Usage: You can limit the resources a process can use with the ulimit command in Linux. This can prevent a single process from using up all your available resources.
Optimize Your Scripts: If you're running your scripts as cron jobs, ensure they are as efficient as possible. Unoptimized scripts can use more CPU and memory than necessary.
Use Cgroups: Cgroups(Control groups) is a Linux kernel feature to limit, police and account the resource usage for certain processes. You can use it to prevent any process from hogging the system's resources.
Remember, it's important to remember your system's specifications when configuring your cron jobs and other settings. The needs of a system with a large amount of resources will differ from those of a system with fewer resources. Monitor your system's performance regularly to ensure it's operating at its best.
Having something to do your repetitive job is a way to save time and energy. It also lets you focus on more important jobs. And this is exactly what Cron job does.
Cron job is the perfect job scheduler for web developers and system administrators. All they need to do is enter the commands and set the best timing for them to run.
We hope this article can help you familiarise yourself with this program, its features, and its pros and cons. We tried to help you learn more about the basis of this program.
People Are Also Reading: