Are There Any Cron Jobs Alternative?

2019-01-17 23:40发布

问题:

Cron Jobs are closed on my server and server admin doesn't accept open it. Because , cron jobs slowing server etc. So, i need an alternative.

I have to run a php file (cron.php) every 2 minutes.

So, how can i do this ?

回答1:

Even though the question was posted a while ago, I just had the same problem but found a solution (based on Kissaki's answer, thanks!) and thought I'd post it here for anyone still looking for a possible solution.

Prerequisites:

  • SSH access
  • Python

Code (python):

from subprocess import call
import time
while True:
    call(["php","cron.php"])
    time.sleep(120)


回答2:

Depends on your access on the box.

PHP itself will not be able to run standalone that well. You could do a script which tries to increase it’s execution time constantly, sleeping and checking for new jobs regularly. But that is sub-optimal as you’ll have to access it via browser once, and the script would have to make sure it only runs once.

With shell access you could run the php script on the shell, which would prevent it from being callable from public and having to run it via webbrowser.

With shell access you could also run a program that provides a (cron) service for you. Be it a Java, Python, or other program.

Cron jobs shouldn’t slow the server always. That depends on the job that is run. If it’s your jobs that are so expensive your admin will probably not be okay with working around the closed cron jobs and slowing the server again anyway and may take further action to prevent you from working around.



回答3:

Subscibe to a (free) web site monitoring service, and schedule your [url]/cron.php. You can have your webpage page return something meaningfull and configure the monitoring service to notify you on that response.



回答4:

Seems like this might be your solution - Free & Easy Cronjob Execution.

As I get it, it's a webservice that will evoke your site URL via schedule you setup.

Found mention about this thing here - https://stackoverflow.com/a/6853113/123618



回答5:

Just an addition to the Answers. Rare case scenario.

If your application is having frequent amount of database operations then you can maintain a separate table where the the one column will work as a measure when to run the script. Example as below

Table CRON_RUN

last_run
----------
12-09-2018 11:55:12 (dd-mm-yyyy H:M:S)

Now in your application a check can be performed every time when those frequent db operations occurs and check if the last_run date and the current date is having x difference . X is difference in time you want to run the script. 2 minutes in your case. Also if the difference is more then or equal to 2 minutes then the update statement will run to update the last_run column with the current date time and the next statement will be you cron.php script .



回答6:

If you had a lot of visitors, just write to database last time visit, and if nest time more needed time - run cron. But actually - find new hosting.



标签: php cron jobs