My host doesn't allow cron

My host doesn't allow cron

by Ryan Niebur -
Number of replies: 16
My host doesn't allow cron jobs unless you pay $3 more a month. Can I set up a cron job on my local machine that will run the following script:

#!/bin/sh

wget http://example.com/moodle/admin/cron.php
rm cron.php

Will that work?
Average of ratings: -
In reply to Ryan Niebur

Re: My host doesn't allow cron

by Richard Enison -

RN,

I don't think so. Assuming you are running Unix or a Unix-like system such as Linux on your local machine, the script will run as a shell script on your local machine (if it's a windows box, it won't run). The first command will download cron.php from your server (I'm assuming example.com represents your server URL) to your local machine, but it won't run it (see http://www.gnu.org/software/wget/manual/wget.html#Overview). You could add a command to run it, but it would only run on your local machine, which I don't think was your intension. Finally, the rm command will delete from your local machine the file it just downloaded from your server.

Theoretically, I suppose, you could create a cron job on your local machine that would cause cron.php to be run as a command-line utility on the server, but it would have to be much more complicated, because it would have to shut down the web server (which would make it hard for it to do anything else after that, so it would have to upload itself to the server first and cause it to be run there) so that cron.php could be run without interfering with the normal operation of Moodle. Also, it would have to contain the passwords that would give it permission to cause these things to happen on the server. Otherwise, anyone on Earth could cause your server to do whatever they wanted it to do!

RLE

In reply to Ryan Niebur

Re: My host doesn't allow cron

by Dennis Flynn -
you should be able to run cron jobs on your local computer that call php pages on your hosting provider. might not be as seamless as running cron locally, but you should be able to do what you need using this method
In reply to Dennis Flynn

Re: My host doesn't allow cron

by Richard Enison -

DS,

I stand corrected. I thought I remembered seeing some code in cron.php that would exit if it was run from the web instead of on a command line, but I must have been mistaken. But you have to admit that some of what I wrote about RN's script was true. Am I right?

RLE

In reply to Richard Enison

Re: My host doesn't allow cron

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
Richard,

The other suggestions in this thread are correct - by running the wget command you are in effect executing the script on the remote server, not your local PC.

Ryan: assuming you are running a Linux machine at home, you can easily create a new cron entry to run cron.php in your Moodle install.

At a terminal type 'crontab -e' and add the following line:

*/5 * * * * wget -q -O /dev/null http://example.com/moodle/admin/cron.php >/dev/null 2>&1

This will run the cron script every 5 minutes on your server, it will write the downloaded file to /dev/null (basically discard it) and will suppress all errors. To check that you've entered it correctly you can type 'crontab -l' at the terminal.

Paul

In reply to Paul Holden

Re: My host doesn't allow cron

by Ryan Niebur -
Thanks!

(I don't know very much about linux scripting and commands; I'm new to linux)
In reply to Richard Enison

Re: My host doesn't allow cron

by Richard Enison -

DF,

I meant DF, not DS, in the earlier post. It was a typo. blush

RLE

In reply to Ryan Niebur

Re: My host doesn't allow cron

by Ralf Hilgenstock -
Picture of Core developers Picture of Translators
Hi Ryan,

i think you can also use a free cronjob service. In Germany is this http://cronjob.de/. I think there are other free cron services available.

Ralf
In reply to Ralf Hilgenstock

Re: My host doesn't allow cron

by Richard Enison -

RH (& DF),

OK. Apparently you both are ignoring me for some reason. But don't you think RN deserves to know, one way or the other, whether he can expect his script, as written, to work, and if not, what changes he needs to make???

RLE

In reply to Richard Enison

Re: My host doesn't allow cron

by Ralf Hilgenstock -
Picture of Core developers Picture of Translators
Hi Richard,

sorry. I can't say something about the script. Its not my buisness to write and discuss scripts.

Ralf
In reply to Ralf Hilgenstock

Re: My host doesn't allow cron

by Richard Enison -

RH,

Well, this must be a disappointment to RN, since his main question was whether the script listed in his post would do what he intended; so I will. It now appears that I was dead wrong on just about every point blush, largely due to a misunderstanding of a highly misleading statement in the GNU Wget manual, and also because I mis-remembered something I thought I had seen in the code of cron.php. I'm pretty sure I saw it somewhere, but not there. I'm still a little puzzled about the "rm cron.php" line. It seems that it would delete a file of that name on his local machine, which probably doesn't exist, and if it did, why delete it? Especially since if it ever existed, it would be gone after the first time the script was run.

RLE

In reply to Richard Enison

Re: My host doesn't allow cron

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers
When you invoke wget with just the url, it downloads the contents of that url and saves it in a local file. The name of the file is, by default, the last component of the url. So in this case, it creates a file called cron.php. If you don't want the file, you need to remove it. Hence the 'rm cron.php' wink

Saludos. Iñaki.
In reply to Iñaki Arenaza

Re: My host doesn't allow cron

by Richard Enison -

IA,

Either you are making the same mistake I did in misinterpreting the Wget manual, or you are using the word download in the same sense as the manual, in which case many newbies reading this forum will misunderstand your post as well. The manual says what you said, that wget with just a URL will download the URL. That's why I stupidly told RN that his wget command will simply download cron.php rather than execute it. What I should have realized is that in the context of a PHP script file in an http URL, download means execute! surprise In an ftp URL, that's a different story.

EDIT: Ordinarily, I suppose, when you "download" via http a PHP script, the output of the script is what actually gets downloaded, and if you use wget it will be saved in a local file with the same name as the script. But in the case of cron.php, I don't think there is any output (I could be mistaken), so I don't know if this means no file would be saved, or that an empty file is saved. And if you don't delete it each time, will each subsequent download replace the previous file, append to it, save it under a different name, or will it display a message box asking the user if (s)he wants to replace the file, in which case the wget command will hang for hours if the user isn't around overnight?

RLE

In reply to Richard Enison

Re: My host doesn't allow cron

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Peer reviewers Picture of Plugin developers

[...] you are using the word download in the same sense as the manual

I'm using the word in the standard canonical sense: ask for this url to the web server and get whatever the server sends us.

How this output is created can vary from url to url, but this is irrelevant to the definition of download smile

But in the case of cron.php, I don't think there is any output (I could be mistaken)

Yes you are mistaken smile cron.php outputs quite a few lines of plain text, telling you what's doing and so on. So yes, you get a file saved on your local disk.

If you don't delete the file, each subsequent download will create a new file (by default, unless you use '-nc' or '--no-clobber') with a different name by appending the suffix .1, .2, .3 and so on. This is in the manual page too wink

Saludos. Iñaki.

In reply to Iñaki Arenaza

Re: My host doesn't allow cron

by Richard Enison -

IA,

Thank you for answering my questions. I had a hunch it might save the file under a different name each time. That's what Windows does if you use it to copy a file to a folder that already contains one with the same name.

As for the manual, I was in such a hurry to respond to RN that I didn't read the whole thing. I read enough to satisfy me that I knew what the command did, which was a big mistake. sad

RLE