Schedule Linux/PHP Tasks with Cron Job

Schedule Linux/PHP Tasks with Cron Job

Whats is CronJob?

Cron Jobs is a feature of Linux systems that performs the task of performing 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 operating system via the Cron job and on the Windows operating system via the Schedule task.

Using Cron Jobs in general, you can schedule and execute routine and repetitive tasks simple and without completely automated interference. The commands that are executed in each Crone are called the Cron Job. A very important use of Cron job in web sites 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 approval of the technical person.

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 Crone for Once Per Minute: Once every minute, Once Per Five Minute: Every 5 minutes, Twice Per Hour: Twice an hour, Once Per Hour: Once an hour, Twice Per Day: Twice a day, Once Per Day: Once a day, Once Per Week: Once a week, On the 1st and 15th of the Month: Day 1 and 15 every month, Once Per Month: Once a month and Once Per Year: Set once per year. And in the Command field: In this section, type the command you want to run the system.

Cron Job 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 Job
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 so we can 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 be able to execute a shell command on the server in order 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 be able to connect and authenticate in order 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, simply checks for the existence of 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

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 job:

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:

Adding a number of Cron jobs is simply adding a Cron job. We can simply 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 job:

Here you use Regular Expression to find and remove a single crawler. However, depending on the crawler you want to find and delete for example 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);

Remove an array of Cron jobs:

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 how easy and enjoyable it is to work with Cron Job in 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, and using Crone 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.

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

Be successful and victorious.

 

 

 

About the Author
Harry H
I'm Harry, I love my job, I love to help, share information and to always learn.

Leave a Reply

twenty − thirteen =