How to install cron job via php script

How to install cron job via php script.

+5
source share
3 answers

This will add a script that runs every day at 9:30 a.m.

exec('echo -e "`crontab -l`\n30 9 * * * /path/to/script" | crontab -'); 

You may have permission problems if you use this script from a web server. To get around this, I would suggest a different approach.

Here is one possible solution. Create a list of scripts to run. You can save this in a text file or in a database. Create a script to read this list and run it every minute or every 5 minutes (using cronjob). Your script should be smart enough to decide when to start the list of scripts and when to just exit.

+11
source

Do you know how to normally set cron to work? (outside of PHP, i.e. from a bash script or command line).

If so, you just need to use the php exec function to issue the same commands as for creating the command line cron job. One caveat is that permission problems can occur, and you have to be very careful that you enter exec in this function (you don't want to pass input from the end user to this function).

+2
source

You cannot install a CRON job through a PHP script, you must install it on the server side. If you do not want to do this through system , you cannot install CRON through php.

If you are not working on your own server and are using a hosting service, ask your hosting provider how to set up a CRON script (if the provider allows it).

0
source

All Articles