Cron jobs are added tasks to the cron daemon, for it to run them periodically, at specific times.
You usually want your jobs or tasks executed every, Monday, or every day at 5 pm, etc. Cron is very powerful and flexible, so you can tweak it a lot. Let’s see what cron is.
Cron is a time-based job scheduler in Unix-like computer operating systems. Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email
Now under Linux, Cron Jobs can be added in two ways.
Let’s see now the first way to add a job to the cron. You’ll need to run this command.
This will edit the actual user cron script, the file you’ll see could be empty or already have some jobs in it.
The syntax of this file is:
Min Hour Day Month DoW /path/to/command arg1 arg2 … arg
You can put an ‘*’ asterisk on any or all field to indicate that at all Hours, minutes, etc. should the command be executed.
30 * * * * /home/user/backup.sh
And backup.sh may look like this:
rsync --progress --partial -avz /folder/to/copy/ email@example.com:/remote/folder
You can also use intervals, in any field, example:
30 23 * * 1-5 /home/user/script.sh
This will execute in all week days at 23:30 the scrip.sh
Now the second way is, if you want to execute a task hourly, weekly, monthly or daily, just insert your scrip in one of these folders:
Depending on what you need or want, you should make your script executable once in that folder, and root will execute it, you will need to have permissions to write on those folders.
Linux Cron Jobs, should be used everytime you need to execute a task periodically, do not forget to read the man pages of crontab and crond
If you enjoyed the article, please share it
If you want to receive articles similar to this every month sign in to our Newsletter and joing other 2.000 readers
You can see an example of what is going to be sent to you here