Eray
Eray

Reputation: 7128

Are There Any Cron Jobs Alternative?

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 ?

Upvotes: 20

Views: 38775

Answers (5)

Sartaj
Sartaj

Reputation: 169

Best Solution:- Use serialise file to save status or DB. Check this file on every request and run script when need. If you need to manage these jobs more widely and run shell commands and you have permission to install services. Use SartajPHP NativeApp as service. You can also control your jobs via browser and see live errors and fix them.

Upvotes: 0

Kesara Wimal
Kesara Wimal

Reputation: 729

There are several Cron alternatives for Linux. Like,

  1. Anacron - Anacron is a periodic command scheduler just like cron. The only difference is that it does not need your computer to be always running.

  2. fcron - Fcron is the best of both cron and anacron. It doesn’t require your computer to be running 24×7, and it can work with tasks on an hourly or minute basis.

  3. Hcron

  4. Jobber etc.

For future reading refer to this article.

Upvotes: 2

Vaibs
Vaibs

Reputation: 2096

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 .

Upvotes: 0

KJdev
KJdev

Reputation: 723

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)

Upvotes: 17

Kissaki
Kissaki

Reputation: 9217

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.

Upvotes: 2

Related Questions