+5 votes
182 views
Cron and Crontab - Manage time in Linux

in Linux / Unix by (552k points)
reopened | 182 views

1 Answer

+3 votes
Best answer

Cron has the ability to execute processes in the background, transparent to the user, can be configured and then run without the user intervening.

It is widely used in servers to perform maintenance, backup or specific tasks of some web.

To create cron command we can use the terminal window and write crontab -e

The first time it is executed, it will request that we indicate which will be the default text editor to use vi, nano or other.

image


Here we will add our crontab rules. The rules or crontab command has the following scheme

mh sun dow user command
  • m - express minutes in which the script will be executed, the value goes from 0 to 59
  • h - express the exact time, in 24 hour format, the values ​​go from 0 to 23, being 0 at 12:00 midnight.
  • sun - express day of the month, you can specify 10 if you want to run every day 10 of the month
  • dow - expresses the day of the week, can be numeric (0 to 7, where 0 and 7 are Sunday) or the first 3 letters of the day in English: mon, tue, wed, thu, fri, sat, sun.
  • uer - defines the user that will execute the command, can be root, or another different user as long as he has permission to execute the script.
  • command - is the command or absolute path of the script to be executed, example: /home/user/script.sh, if it calls a script, it must be executable.

So for example you can create a monkey
Create a backup every day at 10:30
30 10 * * * user / home / user / backup.sh

Update the operating system on Fridays at 1:00
00 01 * * 6 root apt-get -y update root user
Another variant for the same would be to use the name of the day Friday (Friday)

00 01 * * fri root apt-get -y update root user

A more complete and complex command
Every August 30th at 11:30 PM carry out server maintenance tasks

30 23 30 08 * user /home/user/servermaintenance.sh


The same can be configured in all server control panels.

For example, from the Directadmin control panel, we have a visual interface to create the cron command

image


It is also possible to create cron command from Cpanel in a very similar way to the previous one

image


If we have a website, it can be used to perform tasks such as updating the web, performing maintenance tasks on the database, performing automatic backups, sending emails such as:

Every day at midnight send newsletter to users
00 00 * * * user /home/user/mydomain.com/newsletter.php


We are going to create an example to synchronize with the Rsync software a local directory with a directory on a server that we assume we are connected to

00 21 * * * rsync -avz --password-file = / etc / rsync.secret / home / dir-local / myuser @ ipdelserver :: dir-emoto

by (3.5m points)
edited

Related questions

+5 votes
1 answer
+5 votes
1 answer
asked Nov 4, 2019 in Linux / Unix by backtothefuture (552k points) | 244 views
+5 votes
1 answer
+3 votes
1 answer
+4 votes
1 answer
asked Jun 27, 2023 in Linux/Unix by backtothefuture (552k points) | 64 views
Sponsored articles cost $40 per post. You can contact us via Feedback

Most popular questions within the last 30 days

10,659 questions
10,791 answers
510 comments
3 users