How can I set cron job through PHP script

前端 未结 3 367
闹比i
闹比i 2020-11-30 13:14

How can I set cron job through PHP script.

相关标签:
3条回答
  • 2020-11-30 13:29

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

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

    You may run into problems with permissions if you are running 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 that need to be 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 a cronjob). Your script will need to be smart enough to decide when to run the list of scripts and when to simply exit.

    0 讨论(0)
  • 2020-11-30 13:41

    You can't set a CRON job through PHP script, you have to set it one the server side. Unless you want to do it via system function, you can't set a CRON through php.

    If you're not running on your own server and use hosting service, ask your hosting provider how to set up a CRON script (if the provider allows it).

    0 讨论(0)
  • 2020-11-30 13:48

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

    If so, you just need to use the php function exec to issue the same commands you would have to create the cron job at the command line. One caveat is that there may be permission issues and you have to be really careful about what you put in that exec function (you don't want to pass input from the end user to that function).

    0 讨论(0)
提交回复
热议问题