Friday, October 23, 2009

PHP Cron Jobs - HowTo, Tips and Trick

How to set up PHP cron jobs?

To set up PHP cron jobs, there's two way to do:
- Using the /usr/bin/php /path/to/file.php command in your crontab.
If you have access to CronJobs, you can simply enter that command into the "Command to execute" field.

But this way is not recommended, as your script may not run as expected. It's difficult to parse  variables in the script, too.

- Set cron job to call its URL.
This is more convenient, and you can test whether your script executes as expected or not just by entering its URL into a browser.
You can use the wget or lynx command to call that URL, and lynx is recommended, as it's a browser.

To set up PHP cron jobs with lynx, you can enter lynx http://www.example.com/cron.php with http://www.example.com/cron.php is PHP script's URL. You can also add some GET variables into the URL using ?var1=value[&var2=value2&....].
Or, you can try some free cron jobs services to set up your PHP cron jobs :) Most of them work perfectly and have their own simple WebCron interface.

PHP Cron Jobs - Tips and Trick.
- To make sure your cron jobs run until finishing its jobs, you can try tips at Set cron jobs with PHP scripts.
- To prevent your cron script from running when you create / save / enable your cron at SetCronJob, read this tips.
- If you wanna set cron job every minute to do some small jobs, you can buy a cron every minute at price of $10 / year from SetCronJob.

Thursday, September 24, 2009

Missing jobs will be executed.

As I cannot control the server downtime caused by my hosting provider, from now on, all missing jobs after a downtime will be executed later.

There are 3 solutions:

- Execute all missing jobs when my server is up.
This is a big bang :D and of course I don't use it :)

- Execute all missing jobs along with new jobs.
E.g, my server is down at 22:01, and it's up again at 23:01, so at 23:05, it will execute all cron jobs that should be executed at 22:05 (missing jobs) and 23:05 (new jobs).

But this may be a bit terrible if your heavy script (e.g backing up) run at the same time: if you set cron job every 30 minutes, at 23:30, it will run twice a minute :(

- Execute all cron jobs 3 times faster than normal.
With the example above, at 23:05, it will execute all cron jobs that should execute at 22:05, 22:10, 22:15.
At 23:05, it will execute all cron jobs that should execute at 22:20, 22:25, 22:30.
....

This will affect all normal cron jobs, but it the best way that I can do :)

After the downtime length / 2, every thing will go normally.

Sunday, September 6, 2009

Basic HTTP Authentication

A bug on Basic HTTP Authentication has been fixed. 

If users try to create a cron job with URL containing username:password@, the URL becomes scheme://1domain.name/path.....

Thanks Fred for informing me :)

Saturday, September 5, 2009

Set cron jobs with PHP scripts

Here are some tips to make your PHP script work perfectly:

Official tips

Add

<?php
set_time_limit(0);
ignore_user_abort(1);
?>

at beginning of your php file(s). This will let your script run until it finishes all jobs and doesn't care about users' stop button :)

Thanks Francesco for this tip :)

Personal tip

As most shared hosting disable functions set_time_limit and/or ignore_user_abort, you'd better don't output anything, or your output should be less than 4KB. Remember not to force your script to output (i.e using flush).

I tried it with shared hosting and my current server (LAMP), and my script which requires 150 seconds to complete works perfectly when set cron job at SetCronJob.

Wednesday, September 2, 2009

Connection time and response time

I've split Execution time into Connection time and Response time.

From now all, when viewing cron jobs' logs, you will see these two values.

For example, here's part of my logs for URL http://www.setcronjob.com/

Last execution time : Wed, 02 Sep 2009 11:45:08 UTC
Connection time : 0.0144
Response time : 0.000835
Output:

HTTP/1.1 200 OK
Date: Wed, 02 Sep 2009 11:45:08 GMT
Server: Apache
X-Powered-By: PHP/5.1.6
Content-Length: 6195
Connection: close
Content-Type: text/html; charset=UTF-8
Content-Language: en

.........

At SetCronJob, the max connection time is 3 seconds, and max response time is 2 seconds.

Saturday, August 22, 2009

Change email address

You can now change your email address: Go to Control Panel > My account > Change email, enter new email, click Change.

An email will be sent to new email address. You must follow that link to complete.

If you didnot click on that link, your email will not be changed.

Sunday, August 2, 2009

Cron jobs unreachable!

As I said, cron jobs unreachable will be deleted. However, this made a big problem: many users complained about deleting their cron jobs, which is ok when checked with SetCronJob.

So, I've make a small improvement: If your cron job is unreachable in 03 consecutive days, it will be disabled. To enable it, click Save and correct all error messages. It will not be deleted until expired.

Friday, July 3, 2009

Error messages explanation

When set cron job with your URL, you may see one of these messages:

- No need to set cron job with home page URL.

- This domain name is restricted.

Mentioned in Set cron job with a URL.

- This URL is restricted.

Your URL contains prohibited words. Read my Term of Service.

-Invalid URL. URL must start with http://, length from 12 to 200 characters.

The shortest URL, I think, will like http://a.bc/ (one character domain name with national extension), whose  length is only 12 characters. Normal URL length will be from 50 to 200 characters.

- This URL is unreachable.

This means SetCronJob cannot connect to your server (the connection time out is 2 seconds). You should have a look at your URL and make sure there's no http://http:// at begining of your URL, or some typos in domain name.

- This URL returns "HTTP/1.1 404 Not Found"...

Your server don't found the page you try to set cron job. You may check typos in URL, or go directly to that URL by browser to see whether it works.

- This URL returns "HTTP/1.1 301 Moved Permanently"...

- This URL returns "HTTP/1.1 302 Found"...

The two error messages above mean that your URL redirects to somewhere. You can fix that by enter directly your URL into browser, and when everything is loaded, copy the URL displayed on address bar, then set cron job with that URL.

- This URL returns "HTTP/1.1 403 Forbidden"...

Make sure your directory has index file, or your server / script allows SetCronJob to make request. The user agent of SetCronJob is SetCronJob/1.0 (+http://www.setcronjob.com/), and IP is 95.154.192.29.

- This URL returns "HTTP/1.1 401 Unauthorized"...

This URL require logging in. Make sure you entered correct username:password@ before domain name.

- Nothing else :D

Thursday, July 2, 2009

Set cron job with a URL

In SetCronJob, URL to call must have this structure:

http://user:pass@domain.com:port/path.ext?query=value
things in italic are optional.

- HTTP only.

HTTPS, FTP, FTPS, etc are not allowed.

- User name and password are supported.

If your URL require HTTP Authentication, simply add user:pass@ before domain name.

- All domain extensions are allowed.

You can add URL with domain.com, .net, .org, .co.uk, ..., .co.cc, .uni.cc, etc. There're some domain names restricted, such as google.com, yahoo.com, youtube.com, ...


- All ports are allowed.

Recommended ports are 80 (default), 8080 (HTTP Alternative), ...

- Must have either path or query.

It's often no need to set cron job with home page, e.g http://example.com/.

http://example.com/?q=backup (home page but have query), http://example.com/backup.php (has path), http://example.com/backup.php?mode=all&sendto=webmaster (has path and query) are allowed.

- All file extensions are allowed.

You can set cron job with file.php, .asp, .aspx, .py, .pl, .cgi, .html, .html, etc

- Fragment will be trimmed.

If your URL end with #fragment, e.g http://example.com/backup.php#useless, the #useless will be trimmed. The final URL will be http://example.com/backup.php. As fragment is used only by browser, web server don't know what fragment is inserted, so it's no need to add #fragment to your URL.

Next post: How to fix error messages like This URL is unreachable or This URL returns "HTTP/1.1....

Sunday, June 28, 2009

How to disable a cron job?

Simply click on Disable icon. Your cron job will be disabled, and no longer be executed.

For Unix/Linux users, you can simply edit your crontab file (using crontab -e), and add # at beginning of cron job line you wanna disable.

For example, you have this cron job:
*/5 * * * * lynx http://example.com/backup.php
And you wanna disable it. Change it into:
#*/5 * * * * lynx http://example.com/backup.php

Sunday, June 7, 2009

About SetCronJob

Cron jobs is a good feature in web hosting that allows you to call a URL periodically. It's so useful to back up your files, database, clear temporary files, etc.

Unfortunately, not all web hosting allow you to use cron jobs. Some will suspend your account if it takes too much server load and memory usage.

Go to Set Cron Job, and you'll have a good alternative for cron jobs, include many features:
- You can create 10 - 40 cron jobs
- Minimum interval: 5 minutes
- Simple, fast, easy to use control panel with one click to create, edit & save, delete cron jobs
- One click to change account type and renew cron jobs.
- Cron jobs will expire after one year.

It's also cheap:
- Free account was provided lifetime.
- Upgrade whenever you need more cron jobs.
- Starting price: $5/year

With free account:
- Create up to 5 cron jobs.
- Minimum interval: 10 minutes.
- Cron jobs will expire after 1 months.

Simply goes here: www.setcronjob.com and register an account for free!