Jump to content

PHP as a cron job

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Ok, I want that on the 1st day of every month an email is sent to me to remind me of something about my website. So, I know how to make the PHP script to send the email, BUT I don't know how to make it as a cron job. I have toastedpenguin hosting, so I login the cp, go to cron jobs, select the time and date etc... and then what is the command I have to write to execute the PHP script?

Should I add some other code to the PHP script that send the email?

Thanks.

#2
veridicus

veridicus

    Newbie

  • Members
  • Pip
  • 5 posts
If you'd like to execute the PHP script directly, you can run "php -f /path/to/script.php'.

Or if you'd like it to run as a web request, you can run "curl http: //example.com/script.php > /dev/null".

(Remove the space between "http:" and "//". It won't let me post this comment with a complete URL.)

#3
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
What is the difference between the direct and web request?

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
The web request is a URL that is invoked using "curl". The other one is run usin a different method.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

TcM said:

What is the difference between the direct and web request?

PHP 4.3 included a server application programming interface (SAPI) which was the CLI (command line interface) which allows php to be executed directly from a console (bypassing the need for a webserver [Apache]). Long story short, using a web request will require Apache, the other method wont. I always run my crons directly using php -f.

#6
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
this is a great thread.... I always wanted to know how to do this.

I can just use this in SSH?
Checkout my new forum! http://adminreference.com/

#7
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
After reading John's post, I think it is possible.

So the command should be something like this??

php -f /public_html/script.php


#8
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

phpforfun said:

this is a great thread.... I always wanted to know how to do this.

I can just use this in SSH?
Yes, use the same command below.

TcM said:

After reading John's post, I think it is possible.

So the command should be something like this??


php -f /public_html/script.php


Yes, but you need the full path to your script. Which is /home/<usrname>/public_html/script.php

#9
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts

John said:

Yes, use the same command below.



Yes, but you need the full path to your script. Which is /home/<usrname>/public_html/script.php

THread bookmarked**

ok, what if I want it to execute at a certain time.. do I put that time in the line, or in the script?
Checkout my new forum! http://adminreference.com/

#10
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts

phpforfun said:

THread bookmarked**

ok, what if I want it to execute at a certain time.. do I put that time in the line, or in the script?

In cPanel, there is an interface which allows you specify time and dates for the cron to run.

#11
phpforfun

phpforfun

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,236 posts
What if I didnt have cpaneL? Like if I were on a friends server and wanted to set one up, but he doesnt have cpanel
Checkout my new forum! http://adminreference.com/

#12
Guest_Jordan_*

Guest_Jordan_*
  • Guests
You can login via SSH and execute the command:

# crontab -e

which is what cpanel executes. Also, on top of what John said (and I also learned this from John) you can put the PHP shebang at the top of your PHP script and execute it like a shell script:

# ./myPhpScript.php

I'd also like to point out another method that circumvents the crontab. You can have your PHP script check for a date. For instance, if you want to execute on the 1st at 12:00 you can use the date function to check the date and time. If it is the 1st and 12:00 or after the script will execute. You would include this PHP in your webpage so it is actually activated when users visits (every time they visit it checks the date time). I think that using the crontab is better but I point this out in case some users do not have the ability to access the crontab. vBulletin also uses this method.