input license here

Comprehensive Guide 2022 for Newbies






Cron jobs: the comprehensive guide to 2022 for newbies


There are many ways to be productive. For example, for busy web developers, they will take advantage of automated processes to handle repetitive tasks. If you use an operating system like Unix, a cron job can save you time by handling the task automatically.


In this article, we will explain what a cron job is, the basics of cron jobs, and how to use cron to schedule task execution.


Surprise offer for you. Web Hosting is promoting this New Year and Christmas


Get Promotion


What is cron job?


Cron is a program to handle repetitive tasks at a later time. The Cron Job issues a command to schedule "work" for a specific action, at a specific time, that needs repetition.


Here's how it works:


If you want to schedule a job once, at a later time, you can use another command. But for cron jobs, cron is the perfect solution.


Cron is a daemon, which means it works in the background to perform non-interactive tasks. In Windows, you are familiar with the background process called Services.


The daemon is always ready and waits until a command tells it to run a certain task – either on the computer or from other computers on the network.


A cron file is a simple text file containing commands to be run at a specific time. The default crontab file in the system is /etc/crontab and located in the crontab . folder /etc/cron.*/. Only system administrators can edit the crontab file on the system.


However, because for operating systems that support multiple users like Unix, each operating system can create its own crontab file. It can run jobs any time it wants. The cron daemon will examine the file and run the command in the background.


With cron jobs, you can automate system maintenance, monitor disk space, and set up backups. Its nature is therefore suitable for machines operating 24/7 – like servers.


Cron jobs are mostly used by system administrators, but it is also useful for webmasters. For example, use cron to deactivate expired accounts, check for broken links, or send newsletters to target users.


Cron job basics


You can create and edit cron jobs using different methods. In this tutorial, we will show you how to do it using Linux Shell Prompt (Terminal).


If you have a VPS on Hostinger, go to the VPS admin page to get information about accessing the server via SSH. If you have a problem, you can review it PuTTY instructions for SSH connection.


Here are some of the tasks that cron jobs can do:


If you want to edit the current user's crontab file, type crontab -e trong terminal:


crontab


It will return results like this:


cron tab results


If using you editor, you can learn more vi commands Basic to do it right.


If you want to edit another user's crontab, you can type crontab -u username -e. You need to run this command using superuser, which means you need to type: sudo su before executing this command.


crontab -u username -e outcome cron job


Another operation that allows you to see cron files if it was created. You just need to enter crobtab -I. If there are no files, you will see this output:


crontab -l result cron job


Alternatively, if you want to see a list of other users' crontab files, you can type crontab -u username -I under superuser:


cron job crontab -u username -l results


Except for having to know about basic tasks, basic syntax is also very important for you to remember.


Basically a crontab file that contains two parts: the scheduler and the command. Here's how to write the command:


* * * * * /bin/sh backup.sh


  • ***** /bin/sh backup.sh cronjob means it will run backup every minute

  • 30 18 * * * rm /home/sydtesting/tmp/* means it deletes files tmp from /home/sydtesting/tmp every day at 6:30 PM.


Let's find out more details about Cron Job


How to write correct Cron syntax


As mentioned earlier, the crontab file has 5 fields – each represented by an asterisk. They are used to specify the date and time a certain task is set up to operate repeatedly.


5 schools of Crontab



  • Minute – minute of the hour the command will run, between 0 and 59

  • Hour – based on the hour the command will run, between 0 and 23

  • Day of the month – based on the day of the month in which you want to run the command, between 1 and 31

  • Month – based on the month in which a particular command runs, between 1 and 12

  • Day of the week – based on the day of the week you want to run the command, between 0 and 7


In addition, you need to use the correct character for each crontab file.



  • Asterisk

  • – to define all scheduled parameters Comma

  • (,) – to maintain 2 or more executions of an instruction Hyphen

  • (-) – to specify the execution interval of an instruction slash

  • (/) – to create a specific rest period Final (L) – for the specific purpose of specifying the last day of the week of the month. Eg, 3L

  • means last Wednesday. Day of the week (W) – to specify the nearest weekday. For example, 1W means if day 1is saturday, command will run on monday (Day 3

  • ) Hash (#) – to specify the day of the week, followed by a number running from 1 to 5. For example, 1#2

  • means the second Monday. Question mark


(?) – leave space


11 examples of Cronjob's syntax


Now you know how to write the correct syntax. We will give you specific examples to understand the above rules better. Before continuing, make sure the output of the command is automatically sent to your email account. So if you want to stop receiving these emails, you can add >/dev/null 2>&1


0 5 * * * /root/backup.sh >/dev/null 2>&1

into the syntax like the example below: If you want to email output to a specific account, add MAILTO


MAILTO="myname@hostinger.com"
0 3 * * * /root/backup.sh >/dev/null 2>&1

and then the email address. Here is an example:

























































Here are more syntax examples:Expression
Meaningful0 0 * * * /bin/sh backup.sh
To run the backup database in the middle of the night, and run it after 1 day.0 6,18 * * * /bin/sh backup.sh
To run database backup 2 times a day at 6AM and 6PM.0 */6 * * * /scripts/monitor.sh
To enforce monitoring every 6 hours.*/10 * * * * /home/user/script.sh
To run cron job for script file placed in home directory every 10 minutes.0 * 20 7 * /bin/sh backup.sh
To run backup database every hour every day 20/07.0 0 * * 2 * /bin/sh
To run the database backup in the middle of the night every Tuesday.* * * 1,2,5 *  /script/script.sh
To run the command in January, February and May.10-59/5 5 * * * /home/user/script.sh
To run command every 5 minutes at 5AM, start at 5:10 AM.0 8 1 */3 * /home/user/script.sh
To run a quarterly order on the 1st at 8AM.* * * * * /scripts/script.sh; /scripts/scrit2.sh
To schedule multiple jobs on a standalone cron job.@reboot /scripts/script.sh
To run certain task every time you start the system.0 0 1 * *  /home/user/script.sh

To run the command on the first day of each month.


Epilogue


Setting up automatic scheduling jobs is not only convenient, but also helps you execute timely, timely actions.


Cronjob is a good way to manage tasks for system administrators or other professions like web developers. You need to use the right commands and choose at the right time.



  • Here are some basic commands: $ crontab e

  • – to create and edit crontab files $ crontab -u username -e

  • – to edit the crontab file of another user with superuser access $ crontab -l

  • – to see the list of crontab files of the current user. $ crontab -u username -l


– to see a list of other users' crontab files.





Now try Cron Job and let it run automatically a command you like to see.






Author





Hai G. is an expert in managing and operating website services. He has many years of experience in VPS, Hosting, technical SEO, CMS. Especially love WordPress and have been using it for over 5 years now. His hobbies are reading, blogging, traveling and mentoring young people to start a business.
Related Posts
Diệp Quân
Nguyen Manh Cuong is the author and founder of the vmwareplayerfree blog. With over 14 years of experience in Online Marketing, he now runs a number of successful websites, and occasionally shares his experience & knowledge on this blog.
SHARE

Related Posts

Subscribe to get free updates

Post a Comment

Sticky