4

I've got a PHP script that updates an entry's status depending on certain criteria being met. I was planning on running this script as a cron job on my web server every hour.

It'd also be nice if I could run it on certain templates on the website too (like on a success/confirmation page after an entry was submitted), so that entries can be updated right away should they need to be.

How would I go about running this script silently from a template though, without it outputting anything to screen? I almost want to 'ping the cron job' (pardon the lack of technical terms here!) from the EE template. It's not something I've done before so I'd appreciate any advice on the best way of going about this, thanks.

Edit:

As a final follow-up, here's the code I ended up using inside my EE template. I followed Anna's first recommendation below and used the PHP exec() function, and I ran this PHP using CE String's php method:

{exp:ce_str:ing php}
  <?php
    exec("php path/to/my/php/script.php > /dev/null &");
  ?>
{/exp:ce_str:ing}
6

Here are 3 quick/easy options:

USING PHP
This question on Stack Overflow sounds like what you're wanting to do:

Use php to trigger another php script, then ignore


USING CURL
Another method would be to use CURL. Here's a Stack Overflow answer with an example.


USING JAVASCRIPT
Another method is using JS/jQuery. This answer on Stack Overflow shows example code:

2
  • Thanks for the options Anna. Looks like the PHP option will be my best bet there. – Stephen Dec 17 '12 at 12:51
  • Great! Glad one of them worked for you. I always like quick/easy too because I'm not an addon developer. Have to work with my limited skills. – Anna_MediaGirl Dec 17 '12 at 21:09
7

Sorry for the super simple answer, but I would just abstract your logic so you could execute the same function with the cron job as you would with the template tag. By creating an EE module you could create an ACT id to execute the cron job script, and use the plugin portion for the template. A common library would be the two that links it all together. Pretty simple really.

Note, I don't ever put PHP in templates. This is one black and white rule I don't think ever needs to be broken provided you have the skill to write a plugin. IMO, there is no proper instance where PHP in the template is better than a plugin.

2
  • Hi Justin. Although I'm sure you're right about that being the best way of doing it, I think our definitions of what is pretty simple differ somewhat! :) I'm just trying to do something quick and dirty, relatively speaking, in 5 minutes. Also, this PHP script is outside of EE with no EE tags, in a php file that I can run via a cron job. I just want to know if I can trigger it to run via a 'ping' from an EE template instead or as well. – Stephen Dec 17 '12 at 5:07
  • 1
    Totally. Good have to different answers available. Mine is definitely tailored to folks like myself. – Justin Kimbrell Dec 17 '12 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.