Miles M.
Miles M.

Reputation: 4169

How to call a specific url at a specific hour with Cron?

I need a function to be triggered by the user when simply clicking on a button. This will set a job to be executed at a specified time (let's say 9am) and will eventually be repeated periodically. That would be handled by PHP

So i've though about using CronJobs alongside PHP. I've followed this pretty good tutorial but I don't really know how to create the triggerred file (home/path/to/command/the_command.sh).

Could anyone advise me please ?

EDIT: In fact What I am looking to do is to call a url, let's say www.google.com at 9am if the user pushed the button on some admin website. The admin part of the job would be handled by the system set up on the tut page I've linked. And I am wondering how should be the file that is called by this php script when the author says :

$crontab->append_cronjob('30 8 * * 6 home/path/to/command/the_command.sh >/dev/null 2>&1');

what should I put into the_command.sh to call a url ?

I don't want this url to be open in any kind of browser. This url will basically trigger a function on the server side.

Upvotes: 0

Views: 870

Answers (2)

We Are All Monica
We Are All Monica

Reputation: 13354

If you have a PHP script you want to run at 9am, then your command in the crontab file should be something like:

/usr/bin/php /var/www/mysite/script.php

where /usr/bin/php is the path to your PHP executable, and /var/www/mysite/script.php is the path to the PHP script you want to execute. Run this command in a terminal first to make sure it works.

This will work with any command. You are supposed to replace home/path/to/command/the_command.sh with the command you want to execute, not use that command literally. Here is a command that will retrieve the page at http://www.google.com/:

/usr/bin/curl http://www.google.com/

Upvotes: 1

arheops
arheops

Reputation: 15259

there are 3 posibilities

1) add line in crontab every time you have task. this one is BAD idea

2) add into crontab:

* * * * *  /usr/bin/php /path_to_your_script/script.php

and check in script current time/trigger all action for this time. Also bad, but better then 1

3) create deamon which will do your tasks. For example one which will start every hour and wait next hour for events schedulered, check every minute.

If you just need call url from bash, use following:

/usr/bin/curl http://www.google.com >/dev/null 2>/dev/null

Upvotes: 1

Related Questions