How To Use Cron Jobs In Linux And Schedule Tasks With It

Tutorials

March 5, 2020 by Harry Hill

use cron jobs in linux

Cron Jobs is a feature of Linux systems that performs specific operations in a specific timing in a fully automated manner. These operations can be executing a command or a particular program or running a CGI script.

For example, the scheduled execution of a PHP file will send an email to users who will automatically send the email at the specified time, such as 9 am every day. This is done on the Linux Server via the Cron job and on the Windows VPS via the Schedule task.

Using Cron Jobs in Linux, you can schedule and execute routine and repetitive tasks simple and without completely automated interference. The commands that are executed in each Cron are called the Cron Job. An important use of Cron job in websites is for users’ services, which will be sent to your users or customers with any Cron implementation.

For example, email for billing information, service blocking notice, service renewal notice, service information, etc. that are all easily configurable and executable with a PHP command, and if not, all of these and many more things should be done manually, and even if there won’t be enough accuracy.

Note, however, that using the Cron Job and the commands you set up requires technical knowledge with the Linux commands. So if you are a beginner user, you should first get enough information about the Linux commands or set up your own Command with the technical person’s approval.

The values ​​that can be adjusted in CJ are minutes, hours, days, months, and days of the week. (Monday-Wednesday, for example, 0 or 7 represents a Sunday). You can also fill in the field with an asterisk “*” to disable one of the values.

In the Cron Jobs section, you will see the options you have above, and each one has a set of schedules and instructions you want to execute. For example, you can perform Cron for Once Per Minute, every 5 minutes, twice per hour, twice an hour, twice a day, once a day, once per week, on the 1st and 15th of the Month, Once a month, and Once Per Year.

Cron Jobs In Linux Syntax Explanation

Tasks that need to be scheduled are saved in a file called CronTab. CronTab is a text file that contains a command with a format that Cron understands. Each of the commands in this file is called Cron Job.

set Cron Jobs In Linux
A crawler always contains four columns, each representing a time that ultimately leads to a path or command to execute.

Each of the columns represents:

  • Minutes: 0 mins
  • Hours: Hours [0-6]
  • Days: Day [0-7]
  • Months: Month [0-7]
  • Days of the Week: Days of the Week [1-5]

1 Minutes [0-59]
2 | Hours [0-23]
3 | | Days [1-31]
4 | | | Months [1-12]
5 | | | | Days of the Week [Numeric, 0-6]
6 | | | | |
7 * * * * * home / path / to / command / the_command.sh

For example, if you want to set the timing for 1 pm on the first day of every month, you’ll have something like the following:

1 0 0 1 * * home / path / to / command / the_command.sh

If you plan to set the timing for every Saturday at 8:30 pm:

1 30 8 * * 6 home / path / to / command / the_command.sh

There are also other options that you can use in scheduling

The comma (,) to construct a list of values ​​from each krone column
Dash (-) to specify the range of values
Stars (*) to specify the value of all
cronTab sends a notification email to the executed task by default. In some circumstances, we do not need to send this email to move the output to / dev/null. This output transfer is done with Operator&gt.

1 30 8 * * 6 home / path / to / command / the_command.sh> / dev/null

If you plan to move the Errors to Divisional Null, you can also use this command.

1 30 8 * * 6 home/path/to/command/the_command.sh >/dev/null 2>&1

Outline of Cron Job Training in PHP
To manage cronTab in PHP, we need to execute a shell command on the server to edit the CronTab file. This feature is usually available on a few shared servers, and on dedicated and virtual servers, you can easily use it.

Use PHP With The SSH2 Library.

To get started, we wrote a class on Cron Job training in PHP, which outlines its structure below.

We have defined the following four properties at first:

$ connection: Our database connection / connection output source
$ path: the file path
$ handle: Our temporary crown file name
$ cron_file: Complete file path and crawler filename

Our class must connect and authenticate to execute commands and have access to the user’s CronTab. So we establish an SSH2 connection inside the constructor and authenticate.

To execute commands, we need to use the exec () method.

Then, we’ll need to write on the cronTab file. We also need a way to delete this file when it’s done. To do this, we define write_to_file () and remove_file ().

Of course, we need a way to create and delete Cron jobs. So we create the append_cronjob () and remove_cornjob () methods, respectively.

To remove all CronTabs, we need to use the remove_crontab () method to manage this operation.

Finally, we need two helper methods: the first method, which returns a boolean value, checks for a temporary Cron file. The next method is used to display bugs. The method names will be crontab_file_exists () and error_message (), respectively.

Save Time with Cron Jobs In Linux

The following is a general class for our work on Cron Job training in PHP:

1 <? PHP
2
3 Class Ssh2_crontab_manager {
4
5 private $connection;
6 private $path;
7 private $handle;
8 private $cron_file;
9
10 function __construct () {...}
11
12 public function exec () {...}
13
14 public function write_to_file () {...}
15
16 public function remove_file () {...}
17
18 public function append_cronjob () {...}
19
20 public function remove_cronjob () {...}
21
22 public function remove_crontab () {...}
23
24 private function crontab_file_exists () {...}
25
26 private function error_message () {...}
27
28 }

 

Run A Cron Job Training Class In PHP

To begin with, we first create an instance of our class. We need to pass the constructor class IP address, port, username, and password first.

1 $crontab = new Ssh2_crontab_manager ('5.5.22.33', '22', 'my_username', 'my_password');

Adding A Cron Jobs In Linux

With an authentication connection, we can build a new crawler.

1 $crontab = new Ssh2_crontab_manager ('5.5.22.33', '22', 'my_username', 'my_password');
2 $crontab-> append_cronjob ('30 8 * * 6 home / path / to / command / the_command.sh> / dev / null 2> & 1 ');

Adding An Array Of Cron Jobs In Linux

Adding several Cron jobs is simply adding a Cron job. We can refer to the array of Job Crawls as the apeend_cronjob () method.

1 $crontab = new Ssh2_crontab_manager ('5.5.22.33', '22', 'my_username', 'my_password');
2
3 $new_cronjobs = array (
4    '0 0 1 * * home / path / to / command / the_command.sh',
5    '30 8 * * 6 home / path / to / command / the_command.sh> / dev / null 2> & 1 '
6 );
7
8 $crontab-> append_cronjob ($new_cronjobs);

Delete A Cron Jobs In Linux

Here you use Regular Expression to find and remove a single crawler. However, depending on the crawler you want to find and delete, by month, your regex expression will be different.

1 $crontab = new Ssh2_crontab_manager ('5.5.22.33', '22', 'my_username', 'my_password');
2
3 $cron_regex = '/home\/path\/to\/command\/the_command\.sh\/';
4
5 $crontab-> remove_cronjob ($cron_regex);

Removing An Array Of Cron Jobs In Linux

As with deleting a Cron job, we use regex here, each of which we place in an array house, and finally run the remove_cronjob method.

1 $crontab = new Ssh2_crontab_manager ('5.5.22.33', '22', 'my_username', 'my_password');
2
3 $cron_regex = array (
4    '/ 0 0 1 \ * \ * /',
5    '/home\/path\/to\/command\/the_command\.sh\/'
6 );
7
8 $crontab-> remove_cronjob ($cron_regex);

Conclusion

As you can see, it is easy and enjoyable to work with Cron Jobs in Linux and PHP, especially since we wrote our own class and controlled different operations without using another class of our own methods.

You have realized how useful and efficient task scheduling can be in managing scripts in web applications. Using Cron Job can generally perform routine and repetitive tasks without the need for completely automated interference.

I hope you get the most out of Cron Job training in PHP.

Suppose you have any questions, post in the comments. Quickly, we will answer your questions.

Helpful

About the Author Harry Hill

I like to learn and to teach to make things easier

Leave a Reply

Your email address will not be published. Required fields are marked *