I'm trying to run my first cron job on MAC OS (my localhost is running MAMP).
To access crontab, to run a cron job, I'm typing the following into the command line:
crontab -e
Then I press i on the keyboard to go into insert mode to insert the cron job.
The cron job inserted is
* * * * * wget http://localhost:8888/project/cron.php
Then I press esc and type :wq in the command line.
I then get a message:
installing new crontab
Then I check what cronjobs are running with crontab -l and it says:
* * * * * wget http://localhost:8888/project/cron.php
But nothing happens...
On a separate note, below is the script I'm executing as a test script in the cron.php file. It is meant to generate a new time stamp in a text file in the root of the project (the cron job is supposed to make this happen every minute):
<?php
$file = dirname(__FILE__) . '/output.txt';
$data = "hello, it's " . date('d/m/Y H:i:s') . "\n";
file_put_contents($file, $data, FILE_APPEND);
?>
If anyone has any advice as to why this isn't working I'd be hugely grateful.
I've tried the steps shown above at both system level (i.e macmini) and also in my 'project' folder.
I'm completely new to all of this so any help would be amazing.
The solution to this was to use the file paths not the url. First I found where the php was located. I did this with:
whereis php
This gave me the following file path:
/usr/bin/php
I then got the FULL file path to the cron.php file in my project:
/Applications/MAMP/htdocs/project/cron.php
I then combined the two in the crontab cron job:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/project/cron.php
The actual steps I used are the same as in my original question.
Related
I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh
I have a PHP script that is being called every minute by crontab like this:
* * * * * /usr/bin/php /var/www/html/cronjobs/watchdog/watchdogExec.php
The script executes a number of other PHP scripts and passes a variable like so:
$ccmd = "nohup sudo /usr/bin/php watchdog.php " . $var . " &";
exec($ccmd);
When called from a browser, watchdogExec.php runs fine and does its job. However when I added the cronjob, the script is not working and is spamming mails with the following error:
No entry for terminal type "unknown";
using dumb terminal settings.
This error appears the same number of times exec() is called, so I figured that must be the issue. I searched and read about cronjob not running commands in a terminal, and I tried setting the environment in the crontask, but it did not help. Apparently PHP is printing that error, but I haven't found anything in their documentation about it.
* * * * * /usr/bin/env TERM=xterm /usr/bin/php /var/www/html/cronjobs/watchdog/watchdogExec.php
I am running CentOS 7.
Edit:
Searched some more and found out about PHP environment variables. Someone fixed a similar problem by adding the following in their PHP script. However this is not fixing my problem, it only stops the mail spam. The script is still not working.
putenv('SHELL=/bin/bash');
putenv('TERM=xterm');
Have you tried using full paths in your exec statement too? Also try adding the TERM environment variable:
$ccmd = "nohup /usr/bin/sudo /usr/bin/env TERM=xterm /usr/bin/php watchdog.php " . $var . " &";
I do this with a 'shell script wrapper'. I add a file phpcron.sh with
#!/bin/bash
php -f filename.php
and add this to crontab.
* * * * * /var/www/phpcron.sh
I hava a php-email (phpmailer 5.2.14) script which work fine when I run it in bash:
pi#schnickschnack: php /var/www/html/email.php
when i run this script with cron (sudo crontab -e):
*/1 * * * * root php /var/www/html/email.php
syslog says...
Jan 22 08:53:01 Schnickschnack CRON[4482]: (root) CMD (root php /var/www/html/email.php)
...but I get no mail.
I have another php-script which works fine with crontab. this script inserts values from phpmodbus into a mysql-db...
does anyone have a hint why the mail-script does not work with cron?
try
* * * * * php /var/www/html/email.php
otherwise, cron tries to execute the command "root", which is not a command.
As you are running by cron, all your usual $PATH and ENV are not available.
So CRON has no idea where to find "php".
Depending on your install - determine location of the PHP bin:
which php
use the resulting path in your cronjob. eg:
*/1 * * * * /bin/php /var/www/html/email.php
** Unless intended, dont leave the email.php script where it could be run "unintentionally" by anyone simple hitting the webserver. email.php is certainly on script kiddies hit list.
I'm trying to set up a cronjob script on my server. I've followed this tutorial and I now have a folder "scripts" with "cronjob.php":
<?php
define("_CRONJOB_",true);
require(APPLICATION_PATH . '../public/index.php');
// my executions
?>
In my "index.php" file:
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
But how can I set this on my server?
I've done the following as a start: chmod 755 cronjob.php, but what's next?
Use crontab, make sure you have both crontab and php-cli installed.
First edit the cron by doing
$ crontab -e
Then insert something like this
*/10 * * * * /usr/bin/php /path/to/scripts/cronjob.php
This examples does execute the script every 10th minute.
For more on the syntax see https://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions
Alternate method for setting Cron job on Linux Server
Step 1: Open Terminal
Step 2: Type
$ sudo crontab -e
Enter System Password
Step 3: Put Cron URL to be executed for every 5 minutes interval
*/5 * * * * curl http://testwebsite.com/hitcronscript
Reference: https://crontab.guru/every-5-minutes
*Note: For test purpose one may set mail or file/Db entry.
Hope it will help developers.
I am trying to set up a cron job using a PHP script. This works fine on my computer running XAMPP but when I try it on my web host (Just Host) it's not adding it to the list of cron jobs. Here is the PHP code:
exec('crontab cronfile.txt');
cronfile.txt Contents:
* * * * * /usr/bin/php -q /home/-username-/public_html/cron/cron.php 1
This does however work when I add it through cPanel and I can view any cron jobs by using shell_exec('crontab -l'). Any ideas how to resolve this?
Most likely Apache is running as a different user than username, so it won't update usernames crontab file. Run the script
<?php phpinfo()
and verify which user Apache is running as.
On my system, I see the following
User/Group apache(48)/48
You should use this exec('crontab /tmp/cronfile.txt')
and it will work.
You can add every script to this file.
example:
touch /tmp/cronfile.txt
vi /tmp/cronfile.txt
* * * * * path-to-script
* * * * * path-to-script2
.
.
finally
crontab /tmp/cronfile.txt
crontab -l you will see your cron list