Skip to content Skip to sidebar Skip to footer

Simple PHP code to create crontab

PHP logo

A crontab, or more commonly called a cronjob, is a tool that is executed at a certain point in time. In the previous article I shared several websites that share the cronjob feature without buying a server / control panel. But now, if you have your own server, I’ll make it easier for you to use PHP to create a cron job without going to the control panel page. Perhaps there are still many who use cron jobs through the control panel page, but if you use the command system on the server it can be easier and very secure to create.

If you do Cron job from the control panel side, absolutely need information Username and password. And it is classified as very insecure because if there are other loopholes in the website then other people can see the username and password from the control panel and can go deeper into the server than anyone can imagine. However, if you use the original features of the server it is safe to do so because it does not require a username and password, just that this method does not apply to all servers, for example Shared hosting whose functions are not fully available to every user and require permission to do so.

So this method can be performed on websites that enable the feature shell_exec (), exec (), or System() and already available crontab in binnary. If the user Virtual server surely you can enable and disable this feature as you wish, but for shared hosting users, not all hosting admins offer this feature as it compromises security. Please look at the code below and the procedure is below.

  1. First, create a file called cronjob.php

  2. Then copy and paste the code above into the cronjob.php file that you created earlier.

  3. Now please set how much time you want to spend on the file. The time above is marked by a star code, above it is set to 10 minutes. You can find the timecode of the cron job here.

  4. Now please create a file called cron.php. This file that is being executed, please do what has to do with the execution there, e.g. B. automatic deletion, publishing or others.

  5. The last step is to see the results of the crontab.php earlier. And if you see the text Install new crontab, it means it worked.

  6. To see the ongoing crontab process, please go to the URL http://ihresite.com/crontab.php?see and if you want to delete it please go to http://yoursite.com/crontab.php?delete. Don’t forget to change your Site.com to your site address

The above code is the simplest and for crontab.txt is the trigger to be able to run / install a new crontab. You could better develop the above code so that it can use multiple aliases, not just one crontab. And this crontab consumes a lot of resources, which is unfriendly for low servers. So it would be better to use a VPS to run crontab at the same time (several). ️

Hopefully useful and good luck