Cron job related question


#1

Well guys

I’m trying to set up a cron job. After some googling and having gone thro’ DH’s wiki, here’s what I’m having problem.

DH wiki : To execute a PHP script using crontabs, you must add this line to the top of the PHP script you want to execute for it to work: #!/usr/local/bin/php -q

Now say I want my file cron.php to run every five minutes. This means I should add #!/usr/local/bin/php -q at the top of cron.php first, am I right? Then, where it should be exactly, even before the the beginning <?php or after? Like

#!/usr/local/bin/php -q

<?php // rest goes here or <?php #!/usr/local/bin/php -q //rest goes here They also tells, alternatively, I can set up the cron like: */5 * * * * /dh/cgi-system/php.cgi /home/myuser/mydomain.com/cron.php Since they have used the term alternatively, I can use any of these two, logically. But, where I am telling when the cron to be executed in the first example? If I go for the second example, they tells that I should save the line in a text file and upload it. Now, where should I upload it? In the same directory as my domain name or before that? I am also confused between */5 * * * * /dh/cgi-system/php.cgi /home/myuser/mydomain.com/cron.php and */5 * * * * wget -q -O /dev/null http://mydomain.com/cron.php Can anyone help? Thanks. [url=http://www.sharpdebate.com]Web's ultimate debate center - www.SharpDebate.com[/url]

#2

It should be that both adding the line to the very top of your php file and the ‘alternative’ setup through cron would work in exactly the same way. Personally i’ve always just done it through cron, and I do it differently than the wiki. :slight_smile:

For the first example, you add that line to the top of your php script, and I believe it has to go n the very first line above anyting else, then you have a crontab entry like this:
*/5 * * * * /home/username/path/to/cron.php
Cron opens up the cron.php file, and ‘sees’ that it’s a php file, and then uses php to excute the file.

The way I like to do things is the ‘alternative’ example. We leave the php file all alone and just tell cron to use php for the file, like this:
*/5 * * * * /usr/local/bin/php /home/username/path/to/cron.php

I’m not quite sure what you’re talking about uploading. Are you talking about uploading a text file that has your cron command in it, and typing “crontab cron.txt” ? If that’s the case, then you would just upload that file to your home directory - the directory you’re in when you first FTP in.
You can also edit your cron file by simply typing “crontab -e”. What’s the difference between these two? When you enter a command for cron to run, it must all appear on one line, it’s can’t wrap onto a 2nd line. A funny qurk of the default setup is that the text editor you use when you type crontab -e has wrapping turned on my default. Becuase of this it is simpler for many people to use notpad to write the command, upload it to their home direcotry, and import that file into crontab.

One work of caution about importing the text file, when you do that it overwrites anythign else in your crontab. So if late you decided to add another command, make sure you don’t delete your first one.

What’s the difference between between my above example and usign wget? wget is a " non-interactive network downloader". Wget will apparently download and excute the file. While you could use this in your situation, I believe it’s mainly used for grabbing a file from an other server.

I think that’s all of your questions, please feel free to ask for cliarification or more questions.

–Matttail
art.googlies.net - personal website


#3

Hi

Thanks a lot for your detailed answer. I really appreciate your effort. Well, sorry to ask such a basic question, but I think I need a bit more clarification.

Well, from your reply I think this is what I need.

Open a new notepad / DW file and type
*/5 * * * * /usr/local/bin/php /home/username/path/to/cron.php

say, my username is bush and domain is www.bush.com and path to cron is www.bush.com/cron.php, the above example will be

*/5 * * * * /usr/local/bin/php /home/bush/http://www.bush.com/cron.php

Is that correct?

Now,

  1. Is that the only thing I need in the file that I am creating?
  2. How should I save the file? .php, .txt …?
  3. DH server space where I do the ftp is like -> xxx.dreamhost.com, where I find folders like bush.com, logs, maildir, etc. and I upload my files for the site www.bush.com inside the folder bush.com. Now where should I save that .txt or .php (number 2) file? At the same level as bush.com, logs, etc. or inside the folder bush.com.

I will greatly appreciate if you can help me out for this.
Thanks.

Web’s ultimate debate center - www.SharpDebate.com


#4

[quote]*/5 * * * * /usr/local/bin/php /home/bush/http://www.bush.com/cron.php

[/quote]

Almost, but not quite. this is just showing the directory structure to your file, so it would be
*/5 * * * * /usr/local/bin/php /home/bush/bush.com/cron.php

  1. Save it as a txt file. I’d reccomend notepad. You don’t want anything fancy for this file, just plain text.
  2. you’ll want to upload it to your home directory. That’s the same one “where I find folders like bush.com, logs, maildir, etc.”

Once you’ve uploaded that file you’ll log into SSH, and type “crontab cron.txt”. With luck you’ll get a success message about installing the new crontab.

–Matttail
art.googlies.net - personal website


#5

thanks for the post. I’m trying this …

Web’s ultimate debate center - www.SharpDebate.com


#6

Very strange !!! I followed exactly what u told, but when I tried crontab cron.txt, shell told me :

“cron.txt”:1: bad minute
errors in crontab file, can’t install.

But when I did crontab -l, I got the whole

*/5 * * * * /usr/local/bin/php /home/bush/bush.com/cron.php

what does it mean !!!

Web’s ultimate debate center - www.SharpDebate.com


#7

It means that for some reason Cron can’t understand what you’re telling it to do. It still took the text over so when you type crontab -l it shows.

Let’s try this, Copy your line from your text file (on your home computer) then switch over to SSH. Type crontab -e delete everying there, then paste in your text. Press Ctrl and o at the same time to save. Press enter to accecpt the file name/location. Press Ctrl and x at the same time to exit. What happens now?

–Matttail
art.googlies.net - personal website


#8

Now it gives:

crontab -l
// the crontab line
crontab -e
crontab: installing new crontab
// cursor here

does it mean that everything is OK
now?

Web’s ultimate debate center - www.SharpDebate.com


#9

Hey man!

It seems things are working … that’s great … I was trying for this since this morning !!!

Thanks a lot buddy !!!

Web’s ultimate debate center - www.SharpDebate.com