waddledoo Posted December 12, 2011 Share Posted December 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/ Share on other sites More sharing options...
requinix Posted December 12, 2011 Share Posted December 12, 2011 Put in the path to the script, then make sure the very first line in the file is #!/usr/bin/php (check that path before using it - could easily be something else) Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297321 Share on other sites More sharing options...
waddledoo Posted December 13, 2011 Author Share Posted December 13, 2011 Is the line #!/usr/bin/php placed before the <?php //code ?> in the script? Also, I put the filename in the command line, and got the following error: /bin/sh: (file).php: command not found Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297328 Share on other sites More sharing options...
requinix Posted December 13, 2011 Share Posted December 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297332 Share on other sites More sharing options...
waddledoo Posted December 13, 2011 Author Share Posted December 13, 2011 I'm trying to run from cronjobs through cPanel on my webhost. I'll include a screenshot Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297335 Share on other sites More sharing options...
requinix Posted December 13, 2011 Share Posted December 13, 2011 The command would be something like /path/to/your/stuff/file.php If that doesn't work, find out where they put the PHP binary (likely /usr/bin/php) and enter /usr/bin/php -f /path/to/your/stuff/file.php Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297338 Share on other sites More sharing options...
waddledoo Posted December 13, 2011 Author Share Posted December 13, 2011 So I would enter either /public_html/(file).php OR /usr/bin/php -f /public_html/(file).php (^^or w/e the path is) EDIT: Before you replied, I tried entering '$ chmod +x file.php' and received the error /bin/sh: $: command not found Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297339 Share on other sites More sharing options...
requinix Posted December 13, 2011 Share Posted December 13, 2011 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 ~/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). Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297342 Share on other sites More sharing options...
waddledoo Posted December 13, 2011 Author Share Posted December 13, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297348 Share on other sites More sharing options...
requinix Posted December 13, 2011 Share Posted December 13, 2011 Use /usr/bin/php -f (filepath)/public_html/(filename).php But you shouldn't be putting these scheduled scripts in a web-accessible location. Somebody could go to filename.php on your site and trigger the script. Put it outside the public_html. Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297351 Share on other sites More sharing options...
waddledoo Posted December 13, 2011 Author Share Posted December 13, 2011 The file was in public_html so I could test run the script myself. Regardless, I believe the script is running correctly now. However, it isn't sending me any messages, error or otherwise, so I will have to further test. Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297353 Share on other sites More sharing options...
requinix Posted December 13, 2011 Share Posted December 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253035-php-file-run-with-cronjobs/#findComment-1297571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.