Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[documentation] - Configuring cron jobs #2747

Closed
lonalore opened this issue Jul 26, 2017 · 9 comments

Comments

@lonalore
Copy link
Member

commented Jul 26, 2017

Configuring cron jobs using the cron command

Cron is a daemon that executes commands at specified intervals. These commands are called "cron jobs." Cron is available on Unix, Linux and Mac servers. Windows servers use a Scheduled Task to execute commands.

For a modest personal site, you might set up this cron job to run once a day. For a more active site, you might want to run that job more often - perhaps every few hours or every hour.

Cron URL

Each e107 version has a different URL, you must request for cron to trigger. For e107, the URL contains a token, which is a "secret key" to prevent external abuse.

URL format:

http://www.example.com/cron.php?token=TOKEN

The cron command

In the following example, the crontab command shown below will activate the cron tasks automatically on the hour:

0 * * * * wget -O - -q -t 1 http://CRON_URL

Where http://CRON_URL is replaced with a URL which is different for each e107 version. See above.

In the above sample, the 0 * * * * represents when the task should happen. The first figure represents minutes - in this case, on the "zero" minute, or top of the hour. The other figures represent, respectively, hour, day, month and day of the week. A * is a wildcard, meaning "every time." The minimum is every one minute * * * * *.

The rest of the line wget -O - -q -t 1 basically tells the server to request a url, so that the server executes the cron script.

Examples

When Setting
Every 1 minute * * * * *
Every 15 minutes */15 * * * *
Every 30 minutes */30 * * * *
Every 1 hour 0 * * * *
Every 6 hours 0 */6 * * *
Every 12 hours 0 */12 * * *
Once a day 0 0 * * *
Once a week 0 0 * * 0
Once a month 0 0 1 * *

How to set up a cron job

Cron jobs are scheduled by setting up a "crontab." A crontab is a text file that contains the commands to be run. This file can be created and edited either through the command line interface, or, if you manage your website through a web-based control panel such as cPanel, IspConfig, Plesk, or Virtualmin and Webmin, you will use the web interface.

To edit a crontab through the command line, type:

crontab -e

Add ONE of the following lines:

wget

45 * * * * /usr/bin/wget -O - -q -t 1 http://example.com/cron.php?token=TOKEN > /dev/null 2>&1

curl

45 * * * * /usr/bin/curl --silent --compressed http://example.com/cron.php?token=TOKEN > /dev/null 2>&1

lynx

45 * * * * /usr/bin/lynx -source http://example.com/cron.php?token=TOKEN > /dev/null 2>&1

shell script

45 * * * * /var/www/example.com/cron.php TOKEN

or

45 * * * * /var/www/example.com/cron.php token=TOKEN

This would have a lynx, wget, or curl visit your cron page 45 minutes after every hour.

Three options are provided in case either wget, lynx or curl are not installed on the server. Any will do the job well.

Learn more about the crontab file syntax here to set up the cron job to run more or less often.

There are many ways to configure a cron job. If you have full access to crontab features, you should be able to simply paste in one of the above example commands – be sure to replace "example.com" with your own web domain or docroot.

If you're on shared hosting, you should be able to find cron job configuration somewhere in your hosting control panel. Some hosts even have cron "wizards" that walk you through the cron configuration, making it much easier if cron is new to you. On a Windows system you can accomplish the same thing with scheduled tasks to launch Internet Explorer pointed to the URL.

Some hosting companies do not permit local loopback, so using wget, curl or lynx will not work. If this is the case, and they run PHP as a CGI (check with your hosting company to see if this is the case), the following will run the cron locally:

PHP

45 * * * * /usr/bin/php /var/www/example.com/web/cron.php token=TOKEN

Some hosting companies don’t allow access to cron

If your hosting company restricts access to cron, you have many options.

  • Ask the company to give you access, or to set up a cron job for you
  • Ask someone else with access to a server to set up a cron job for you. Any Unix, Linux, or Mac server with access to the internet can have a cron job to regularly visit your site. There are also some companies that offer cron services.
  • Use webcron services like EasyCron. You may find other providers of cron job services. Many are free,but with restrictions.

Troubleshooting cron jobs

If you receive a permission denied error after starting crontab -e, you may need to use sudo:

sudo crontab -e

You may need to adjust the path to wget, lynx or curl in your crontab. For example, the cron example listed above contains the line:

45 * * * * /usr/bin/lynx -source http://example.com/cron.php?token=TOKEN

However, Lynx may be in a different location on your server, or not installed at all. To find out where Lynx is installed, enter:

whereis lynx

or

which lynx

If it is not located at /usr/bin/lynx, adjust the path as needed. The same applies for wget and curl. If none are installed, ask a server administrator for help.

Notes

SSL

When using SSL, add one additional argument when calling wget: --no-check-certificate. Do not put --no-check-certificate between the -0 and the -.

45 * * * * /usr/bin/wget --no-check-certificate --quiet -O - https://example.com/cron.php?token=TOKEN

Proxy

If your server is behind a proxy, you might need to add --no-proxy to the command.

45 * * * * /usr/bin/wget --no-proxy -O - -q -t 1 https://example.com/cron.php?token=TOKEN

Configuring cron jobs with Windows

To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.

Note: These instructions were written for Windows XP but should be similar in other versions of Windows.

Creating a Scheduled Task

  • Open Scheduler
  • Go to Start > Programs > Accessories > System Tools > Scheduled Tasks
  • Double-click Add Scheduled Task
  • The Scheduled Task Wizard will appear. Click Next.
  • Select the program to run. Choose your browser from the list (for example, Internet Explorer or Mozilla Firefox). Click Next.
  • Give the task a Name, such as E107 Cron Job, and choose the Frequency with which to perform the task (for example, Daily)). Click Next.
  • Choose specific date and time options (this step will vary, depending on the option selected in the previous step). When finished, click Next.
  • Enter your password if prompted. Change the username if required (for example, you'd like the task to run under a user with fewer privileges security reasons). Click Next.
  • On the final page, select the checkbox Open advanced properties for this task when I click Finish and click Finish.

Configuring the task

  • Go to the task's setting page either by checking the checkbox at the end of the last step, or by double-clicking on the task.
  • In the Run box, after the text that is there now (for example, C:\PROGRA1\MOZILL1\firefox.exe), enter a space and then type the address of your website's cron.php page in double quotations (for example, C:\PROGRA1\MOZILL1\firefox.exe http://www.example.com/cron.php?token=TOKEN
  • To set a frequency more often than Daily (for example, hourly), click the Schedule tab, then click Advanced. Here you can set options such as Repeat task, every 1 hour for 23 hours. Click Ok, when finished.
  • Change the start time on the task to one minute of the current time. This will allow you to test the task and make sure that it is working.
  • When all settings have been configured to your liking, click Apply and OK (note: you may be prompted for your password)

Command-line version

Another way to perform the above commands is by using the schtasks (or at in Windows 2000) command from the command line. To duplicate the example above, which runs Firefox hourly to execute http://www.example.com/cron.php?token=TOKEN, open a command prompt (Start > Programs > Accessories > Command Prompt) and enter:

schtasks /create /tn "E107 Cron Job" /tr "C:\PROGRA~1\MOZILL~1\firefox.exe http://www.example.com/cron.php?token=TOKEN" /sc hourly

Enter your password if prompted.

Using wget for Windows to run cron

If for whatever reason you'd rather not deal with a browser window popping up on the machine, you can use wget, the Windows port of which works more or less the same as it does in UNIX. curl and lynx also has windows ports, but wget is probably the easiest to set up and use.

Grab a copy of wget from your choice of either the author's site or from SourceForge. Install it to the location of your choice.

Follow the steps for Creating A Scheduled Task above, except select wget.exe as the program to run (you may need to click the Browse button to locate it if you installed from a .zip file, for example). When you get to the Advanced Properties dialog, paste in the following after the program path:

-O - -q -t 1 http://www.example.com/cron.php?token=TOKEN

Adjust the rest of the options as described above and test it.

Using curl with cmd.exe in Windows 7 (or Vista) with Task Scheduler

  • Start Computer Management (Windows Key, right-click Computer, select Manage...)
  • In the right column, select Create Job...
  • Fill in General tab.
  • Under Triggers tab, select New..., then set the desired schedule, with a repeat set in Advanced Options.
  • Under Actions tab, select Start a program, the program is CMD.EXE. For the optional argument, enter /C "curl http://www.example.com/cron.php?token=TOKEN".

If you have already installed mysysgit, curl is already present and in the path.

Under Windows 7 (or Vista), using schtasks and curl

Assuming you have already installed mysysgit as described above...

Run cmd.exe as Administrator, then enter the command:

C:\Windows\system32>schtasks /create /sc HOURLY /tn DevCron /tr "cmd.exe 'curl - -silent --compressed http://www.example.com/cron.php?token=TOKEN'" /ru SYSTEM

Where /tn is the task name desired (here it's DevCron).

The above command yields a job that will run every hour, indefinitely.

@arunshekher

This comment has been minimized.

Copy link
Member

commented Jul 26, 2017

Thanks for the detailed write-up was thinking of incorporating e_cron addon to a project.

@Jimmi08

This comment has been minimized.

Copy link
Contributor

commented Jul 27, 2017

Thank you, you are awesome.

@Moc Moc added the documentation label Jul 27, 2017

@Moc

This comment has been minimized.

Copy link
Member

commented Jul 27, 2017

Great job! Very detailed.

Maybr you can add one more line for non technical people, explaining that they can also set up the cronjob in their hosting control panel (cPanel, DirectAdmin, Plesk, etc.)

@tgtje

This comment has been minimized.

Copy link
Contributor

commented Jul 27, 2017

Nice... is it maybe something to add to the (still present) Docs part of e107 ? So it always is at hand... ? (does not have to be ala q&a)

@lonalore

This comment has been minimized.

Copy link
Member Author

commented Jul 27, 2017

@Moc

This file can be created and edited either through the command line interface, or, if you manage your website through a web-based control panel such as cPanel, IspConfig, Plesk, or Virtualmin and Webmin, you will use the web interface.

...

If you're on shared hosting, you should be able to find cron job configuration somewhere in your hosting control panel. Some hosts even have cron "wizards" that walk you through the cron configuration, making it much easier if cron is new to you.

@tgtje Do you mean uploading it to Developer Manual? I thought about it... but I hate WYSIWYG editors. Markdown is much more faster. :D Please feel free to upload it! :)

@Moc

This comment has been minimized.

Copy link
Member

commented Jul 27, 2017

Oops! Did you just add this or did I miss it in the first place? Great job nonetheless! :)

@Moc

This comment has been minimized.

Copy link
Member

commented Jul 27, 2017

As for the developer manual.. yeah I think it should be added but I agree with the WYSIWYG argument.

I would love to see a github markdown editor integration.

@lonalore

This comment has been minimized.

Copy link
Member Author

commented Jul 27, 2017

You missed it, but no problem. Btw, I do not want to promote it, but I have an integration plugin for SimpleMDE Markdown Editor ;-)

@tgtje

This comment has been minimized.

Copy link
Contributor

commented Jul 27, 2017

@lonalore ..would be a good one yes;
but i meant in the system incorporated (there still is a help ala.Q&A >topnav Docs) piece present.. That one is of course not really up to the latest, but it is there.. and from point of view as user: nice if i could read it 'at hand' instead of surfing to where it would be located. peaking and applying... (little manual).

@Moc Moc added this to TODO in Documentation update Jun 23, 2018

@Moc Moc closed this Jun 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5 participants
You can’t perform that action at this time.