Web development and Tech news Blog site. WEBISFREE.com

HOME > linux

How to use cron in Linux, registering task schedules

Last Modified : 05 Mar, 2023 / Created : 05 Mar, 2023
959
View Count

If you are operating a web server on a Linux environment, you can utilize cron to automatically run certain tasks at desired times. Further details are explained below.




# How to use linux cron


When should we use cron? It is commonly used in the following cases.
When performing a task unit that is repeated at designated times...For example, if you want to back up files or databases every day, you can use cron to do it. You can also send the file to the desired location after performing the backup.

It is also possible to clear the internal cache. If you use in-memory cache such as Redis, you need to clear it. It is also possible to clean the cache at desired times, such as in the early morning when there is no one around. How can you use cron for this?


! How does cron work and how is it used?


Cron runs continuously as a single service on a server, allowing it to perform desired tasks at predetermined times. For this to work, a task to be performed must be defined within the crontab. To summarize...

1. Starting the cron service.
2. Registering a task in the crontab.
3. Task is executed at the scheduled time.

'Now that we have learned about cron, here are the preparations and installation methods for using it.'


! Installing Linux cron


You can install it using apt-get or apt in environments like Ubuntu.
sudo apt-get install cron

Now cron has been installed. We need to check if cron is working properly.
systemctl status cron

It seems to be working well. Most likely, it will be reported that it is working well. However, if it does not work, please start the cron service as shown below.
systemctl enable cron

Now, we need to register a task. Let's learn how to use crontab.



# Using Crontab to Register Tasks and Schedules


What does Cron need to run? Of course, it requires some kind of backup or tasks to do. This task is to provide what we need to do in crontab.

! Check and register a crontab job list.


To register a job in crontab, first check the status of currently registered cron jobs or tasks.

There are two ways to register and confirm tasks in crontab at this time.

1. Using the crontab command.
2. Check the contents of /etc/crontab file.(system-wide crontab)

Both can use crontab to register and execute tasks. The syntax used is all the same as well. Below is how to use the crontab command, which is the first method.

@ Check the list.
crontab -l

You can check and delete duplicate or unnecessary tasks among the already registered tasks.

@ Registering Task.
Task registration is very simple. It is like registering a schedule using an editor, but you execute it using the -e option in crontab.
crontab -e

You can see a brief description of Crobtab and how to register it on the appearing screen. At the bottom of the screen, you can see the following sentence.
# m h dom mon dow command

This is the also important part and it must be registered in the following order. If you look from the left, the order is as follows:

- Minute
- Hour
- Day of month
- Month
- day of week [1, 2, 3 ...] => [Monday, Tuesday, Thursday, ...]
- Command to execute

You can register the work like this. The asterisk (*) symbol implies that it always executes. Let's take a few examples.


@ Run the command /home/root/test.sh every day at 4 AM.
This is a way to create and run a shell script named test.sh for a certain task every dawn.
00 04 * * * /home/root/test.sh

@ Run a specific script only on Tuesdays at 5 AM every week.
This is a method for executing it only once a week.
00 04 * * 2 /home/root/test.sh

@ Run consecutive dates and days of the week.
If you want to run it from Saturday to Sunday, you can use the hyphen symbol. So, for example, Saturday is 6, so 6-7 is Saturday and Sunday.
00 00 * * 6-7 /home/root/test.sh

The code is very concise. Just register the things to do in order like this, and everything is done. If it doesn't work, check if test.sh is an executable.

By the way, be careful when setting the path, especially when creating a sh file. In this case, it is best to set the path based on the root /.
(X) ./../test.sh
(O) /home/myApp/test.sh 



! Unregist a task from crontab


Now, let's learn how to delete a registered task. Use the -r option as follows.
cron -r

Now, registered tasks will be deleted. Be careful when deleting, as there will be no confirmation prompt. Therefore, if you use -r -i together, it will prompt for confirmation.
cron -r -i

We have looked at all the ways to use cron up until this point.

Now, please try registering and running daily or necessary tasks using cron.
Perhaps you're looking for the following text as well?

    Previous

    How to stop or kill a process in use in Linux

    Previous

    Understanding the Linux Command uptime