Cron is running but for some reason it's not executing the script. I have checked to see if the script works and it does. I am trying to make the PHP script execute every minute.
* * * * * /etc/php5 /var/www/cron/automatedScript.php
The server is Linux, Ubuntu distro.
You are most likely giving it the wrong php executable path :)
On MY system it would be:
* * * * * /usr/bin/php /var/www/cron/automatedScript.php
To confirm the correct path to use execute:
nm#vp:~$ which php
And this will return you the path similar to:
/usr/bin/php
I think this may solve your problem
* * * * * /usr/bin/php -q /var/www/cron/automatedScript.php
Info : PHP path may be vary in different os. So you have to know right installtion path of PHP.
I've created a cron job in AWS EC2 but it is not working.
I followed below steps to create cron tab:
Step 1: I logged in to AWS EC2 Instace
step 2: crontab -e
Step 3: Insert mode
Step 4: I entered * * * * * php/var/www/html/welcome.php (To run every min.)
Step 5: :wq
Cron tab is created but not running.
Please can you any one help me if is there any PHP script means please provide me. Do I need to give spaces between every star?
First of all, you need to put an space between php and /var:
From
* * * * * php/var/www/html/welcome.php
to
* * * * * php /var/www/html/welcome.php
^
Then, you'd better use /bin/php instead of just php. To determine where the php executable is located, type which php in your console, it will give you the file path. So it will become something like this:
* * * * * /bin/php /var/www/html/welcome.php
^^^^^^^^
More things:
check if crontab is saved properly? Type crontab -l. Your new crontab line should be there.
is the script exactly in this dir? Try ls -l /var/www/html/welcome.php.
is the script executing if you do from console? Try /bin/php var/www/html/welcome.php to see if it is a script or crontab problem.
does the script have executing mode? Try chmod 755 /var/www/html/welcome.php
Keep us updated so we can find what can be causing the error.
May be a too late but anyways if you intend to run the script every minute the command should probably be
* * * * * php /var/www/html/welcome.php
Running cron on EC2 is not any different from running on any *nix server - as far as I know.
I would check of the system messages for any errors. You can also redirect stderr/stdout to a file as in
* * * * * <your script> >> /var/tmp/out.log 2>&1
and check for any issues for starters.
I have 2 php scripts which need to run every few minutes, the php itself is fine as I can trigger it manually by typing http://myfakesite.com/myphpfile.php into a browser. Both work as they should.
I don't have much experience with cron but I have found the crontab file (I am using Parallels Power Panel on a VPN), and it already had jobs to run-parts cron.hourly, daily, weekly and monthly. I added my email address to the MAILTO field, and get a message telling me that cron.hourly is not a directory - even though it is (although it is currently empty).
I also added my own 2 jobs to this file as follows:
*/1 * * * * wget http://myfakesite.com/script1.php
*/5 * * * * wget http://myfakesite.com/script2.php
Neither of these ever get called. In the php script I also have a line to email me once the script is called, so that I know it is working, and have never received an email via the cronjob. If I ssh into the server and use wget the php works and emails me to confirm.
I have also tried
*/1 * * * * php http://myfakesite.com/script1.php
and
*/1 * * * * wget -O /dev/null http://myfakesite.com/script1.php>/dev/null 2>&1
with no luck. The MAILTO in the crontab file never sends me a message to say there was a problem running the script, but clearly it is not doing it.
Can anyone help at all? I have no idea what to try next.
Edit: Found some info that said that the 'not a directory' error can be down to a corrupt crontab file. So I downloaded it copied the info to a new file and uploaded that. The 'not a directory' error disappeared...COMPLETELY. Even if I tell it to look for a folder which I know doesn't exist like cron.myfakefolder I don't get a Not a directory: /etc/cron.myfakefolder email. Replaced my new crontab file with the original, and I'm still not getting any feedback from the cronjob.
Edit 2: As dAm2K and prodigitalson suggested, I tried using the absolute paths to both wget and php. Neither have worked, and I've double checked the locations of usr/bin/wget and usr/bin/php just to make sure they were actually there.
I also checked var/log/cron and var/log/messages, both contained this:
2002Can't perform "download" operation: Requested file "/var/log/cron" is to big to be sent at once. Try to request file in pieces of 512KB
No idea what is happening with this. As I mentioned in edit 1, I'm also no longer receiving error emails even when I purposely add a false location. Is this related to the error in the log files?
Also, I have checked that crond is running and according to both 'System Services' and 'System Processes' it is running.
Try using absolute paths to executables with cron: eg
*/5 * * * * /usr/bin/wget 'http://myfakesite.com/script1.php'
instead of
*/5 * * * * wget http://myfakesite.com/script1.php
Double check /var/log/messages or /var/log/syslog log files for details on cron execution, and what happens to the system in general.
If your script is on the same server, Try execute it with php -q:
*/1 * * * * php -q pathToTheFile/script1.php
I have built one php file to check some result, so that I need to setup a cronjob.
I set one to run every 30 minute, so that the results will be send. However, I don't know why my crontab did not run after every 30 minute.
Here is how I set the crontab:
*/30 * * * * php /var/www/html/result.php
I have confirmed my file directory is correct. What I not sure is about the timing part: isn't it possible to use */30 * * * * or 30 * * * * ? I set */30 * * * * and did not work.
Given
*/30 * * * * php /var/www/html/result.php
There are multiple possibilities why it is not working:
First of all it is important to check if the simple execution of php /var/www/html/result.php. This is required. But unfortunately, accomplishing this does not mean that the problem is solved.
The path of the php binary has to be added.
*/30 * * * * php /var/www/html/result.php
to be changed to
*/30 * * * * /usr/bin/php /var/www/html/result.php
or whatever coming from which php.
Check the permission of the script to the user running the crontab.
Give execution permission to the file: chmod +x file. And make sure the crontab is launched by a user having rights to execute the script. Also check if the user can access the directory in which the file is located.
To be safer, you can also add the php path in the top of the script, such as:
#!/usr/bin/php -q
<?php
...
?>
Make sure the user has rights to use crontab. Check if he is in the /etc/cron.d/deny file. Also, make a basic test to see if it is a crontanb or php problem.
* * * * * touch /tmp/hello
Output the result of the script to a log file, as William Niu suggested.
*/30 * * * * /usr/bin/php /var/www/html/result.php > /tmp/result
Use the -f option to execute the script:
*/30 * * * * /usr/bin/php -f /var/www/html/result.php > /tmp/result
Make sure the format in crontab is correct. You can do so for example using the site Crontab.guru.
To sum up, there are many possible reasons. One of them should solve the problem.
It may be because php is not in the path. crontab has a very minimal path. So, include the full path for your php program.
you can test your cron commands by piping the output to a file, e.g.
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
From this reference page, under "Crontab Environment":
cron invokes the command from the user’s HOME directory with the
shell, (/usr/bin/sh). cron supplies a default environment for every
shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Also, /30 syntax might not be supported by all platforms, so, try to change it to 0,30 instead.
Had a similar issue; from command line, it worked, but from cron, no go.
had a "include ("./connect.php"); in my php code for the db stuff.
Removed that, and added the connect.php code directly into the php script, and it worked from cron.
I had a similar issue on Ubuntu 14.04.1 and the problem turned out to be the way I was modifying the crontab:
I was using sudo crontab -e instead of just crontab -e and this caused my changes to be ignored.
I had a funny one regarding this. Although my scripts would run manually, they wouldn't run from crontab.
Turns out that because the script was being run from /usr/bin/php rather that the location of the file (as it does when I run it manually) my php require wasn't finding the files I wanted. Changing that to reflect the full address fixed it.
troubleshooting by running the script as /usr/bin/php -f /var/www/myfile.php helped me find the issue
You can use */30 * * * * wget http://my.domain.com/path/to/php/result.php
But Crontab executes the task using the current user that ran crontab -e. When you use wget it’s handled by Apache using the www-data user/group pair
First, make sure the script works as expected.
$ php /var/www/html/result.php
Second, edit the crontab for the Apache user account
$ sudo crontab -u www-data -e
or
$ sudo crontab -u root -e
Now add the crontab and output to a log file.
*/30 * * * * php /var/www/html/result.php > /tmp/result.log
After a day of puzzling why my script would work directly (to send data in an email to a gmail account) I discovered that all the deliberate sends worked when I clicked the url and all the cron sends went into spam. No idea why but I thought I'd share it.
Willem's answer showed me the way. In my case, I have a "include("connection.php")" inside my code. I changed connection.php to /my/full/path/connection.php. I have some rename() calls with the relative path, and I changed to the absolute path. That worked for me. I hope it can help someone else.
Easy and logical way:
Checking the cron logs at /var/log/cron will give you very useful info
less /var/log/cron
Eg.,
My cron entry is * * * * * /usr/bin/php /cat.php <== Run cat.php every minute
The log file will contain an entry similar to the one below every time a cron entry is run
Jan 24 08:06:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Jan 24 08:07:01 OlaTower CROND[13641]: (root) CMD (/usr/bin/php /cat.php)
Here, the php command will be executed every minute and there will be an entry in the log file every minute
If the entry is not there then crond is not even picking that cronjob. If the log entry is there and still you are not getting the desired output then there is something wrong with the command/application logic
Are you sure it is not running? If you use exec, realize that you are running from cron and the full path for everything is required, so instead of cp, you'll need to use /bin/cp.
Centos 7
For the record (and it could work for other distros)
I had the next script
* * * * * /usr/bin/php -f /var/www/html/cron.php >/tmp/result.txt
But it failed to execute.
In the /var/log/cron log file, I found the next line
crond[2213]: (/usr/bin/php) ERROR (getpwnam() failed)
What is that?
It's simple, the syntax of corn is * * * * * user command (check user)
* * * * * someuser /usr/bin/php -f /var/www/html/cron.php >/tmp/result.txt
Using Ubuntu w/ Vesta :
The following command works perfect for me,
/usr/bin/php /home/admin/web/mydomain.com/public_html/mycode.php
Feel free to comment if you have any question, have a nice day :)
I was stuck too. I am using centos 7 and had to run few php scripts. I initially tried this
$crontab -e
& inserted the scripts to be executed at 12 midnight.
0 0 * * * usr/bin/php /var/www/html/cronjob/myscript.php
However in var /var/spool/mail/centos, it gave me an error in the mail there
/bin/sh: usr/bin/php: No such file or directory
So then I used wget like this,
$ crontab -e
0 0 * * * wget https://myapplicationurl/var/www/html/cronjob/myscript.php
This also gave me an error.
ERROR 404: Not Found.
Then I realized my mistake of specifying the folder, since the url will already be pointing to html folder, the folder from there i to be specified, like this
0 0 * * * wget http://myapplicationurl/cronjob/myscript.php
and it worked !!!
Hope this helps any newbie like me :)
if you php script has an include or require, you must provide the full path yours includes
wrong way
// relative path
require_once("../library/PHPMailer/PHPMailerAutoload.php");
Correct Way
// full path
require_once("/home/bitnami/library/PHPMailer/PHPMailerAutoload.php");
I had same problem with my php. Then I test execute php from root dir:
php -f /var/www/html/my_proj_folder/test.php
and got some errors regarding path to lib (included files), such as parse_ini with argument 'config.ini' <- has been taken from my global lib and lose path when it has been started from root.
So,
try to run your php-file (php -f your.file)
check relative path and try to run with absolute path
check permissions to your.php - it has to have executable flag x (you can see it ls -l your.php and set by chmod +x your.php)
put #!/usr/bin/php -q before <?php in your main/executable file
This is driving me a little insane, and I've gone through a hundered different things without touching on the solution; so I may miss out on some details on what I've done so far.
I'm trying to get a Cron job to run on my linux server ive got running in a datacentre. All I'm trying to get to run is a simple php script in the format:
* * * * * php -q /path/to/script/file.php
The php part runs fine if I type it in manually, but nothing happens when the cron runs; it also appears to run in the logs just fine, with no errors.
If i go back and edit with crontab -e, and put in the line
* * * * * echo "test" > /tmp/test.txt
That seems to work ok, it creates the text file.
Has anyone had any problems running a php script in this format?
(Btw I'm just testing with the run every minute, it doesnt work at any time.)
Any help is appreciated.
try invoking php with it's full path, for example /usr/bin/php
the cron will not have the same environment variables as your user profile have, so it might not find the executable.
Try to put full path to php binary (/usr/bin/php or similar)
I also don't have '-q' flag in my distribution. Check it.
Might just be some path craziness: I'd run which php and then copy the full path into cron. On one of my boxes it is /usr/bin/php and so you'd have:
* * * * * /usr/bin/php -q /path/to/script/file.php
Try that and see if it helps.
Perhaps your PHP binary is not in your PATH. Try using the full path:
* * * * * /path/to/php -q /path/to/script/file.php
It's possible that you may need to provide the full path to your php binary
* * * * * /usr/bin/php -q /path/to/script/file.php