Jump to content

PHP file run with Cronjobs


waddledoo

Recommended Posts

I am not sure where else I could post this, so for now it's going here.

 

I am trying to run a PHP script once every 40 minutes using cron, accessed through the cPanel of my webhost.

 

The interface allows me to input the time and a command.

 

The time is easy, I know how to use that part.

 

However, I am not sure what to enter for the command exactly.

Link to comment
Share on other sites

Yes, the very first line. PHP will ignore it.

 

You also need to chmod +x (executable) the file, forgot to mention that. On a terminal,

$ chmod +x file.php

or use whatever you have available.

 

If you're trying to run from a terminal directly you need the full path or precede the filename with a ./

$ ./file.php

Link to comment
Share on other sites

1. You have to use the full path. Including the stuff before the public_html. Do a pwd to find out where you are. For example,

~/public_html$ pwd
/home/waddledoo/public_html

Then use /home/waddledoo/public_html/file.php as the command.

 

2. The $ is a placeholder. It represents the prompt from the terminal. You aren't supposed to type it :D

~/public_html$ chmod +x file.php
~/public_html$ ls file.php
-rwxr-xr-x ... file.php

In the above you're in the ~/public_html folder and you just ran the "chmod +x file.php" command. Then ls showed you that it was executable (the three Xs in the output).

Link to comment
Share on other sites

I can't exactly use commands to find out where I am, as I don't have any access to a typical command line.

I'm running windows, and using my web browser to access cPanel for my website online.

 

So, I need to type

~/public_html chmod +x (filepath)/public_html/(filename).php

OR is it

(filepath)/public_html chmod +x (filepath)/public_html/(filename).php

or am I still way off

 

Link to comment
Share on other sites

cron will only capture output on stdout, and will only email it to you if configured to do so (which it surely is).

 

So

1. Make sure your script outputs any messages it needs to, preferably to stderr

fwrite(STDERR, "Error message");

2. Modify the command to be

/usr/bin/php -f (filepath)/(filename).php 2>&1

This will redirect stderr to stdout.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.