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
Related
I am trying to run in mac a php script using cron. I want this php script to run every one minute. I read several sources online and from my understanding is better if I use launchd. In any case, I try to make it work with cron and then if it works fine I might try to use launchd.
So here is what I do:
I wrote this command in a txt file:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
There I have the path to php (I found it with the "which php" command) and the path to my php script.
I named the txt file: crontasks.txt and I run it through terminal with this command:
crontab /Users/dkar/Desktop/crontasks.txt
When I list the crons (crontab -l) I see that this job is listed. But nothing comes as output every one minute. The output is supposed to be an image stored in a specified folder. What am I missing here?
Thanks in advance
D.
You might have a slight miscomprehension how crond processes the "crontab" files. It would suffice to run the command crontab -e, which would let you edit your personal crontab or you edit the system crontab.
If you use crontab -e it will open the default editor (vi/vim) and you can enter the line:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/php_test/main_script.php
Then you simply save your file. If you want to use the system crontab you will have to edit it directly and enter the line. Additionally the system crontab has one more field for the username. Like this:
* * * * * /usr/bin/php root /Applications/MAMP/htdocs/php_test/main_script.php
On the next full minute your script will be executed by crond.
Ok I have been looking into cron jobs for hours, checked every post here, looked in google but I just do not understand how it works.
I have set up a cron job using my path 1 * * * * /home/myuser/domains/mysite/public_html/live.php I have also tried /home/myuser/public_html/live.php
Nothing seems to be working.
Do I have to add something in the php file (live.php)? That is the code that has to be executed. The code itself works.
I know you will all think that I am lazy but I really can't figure this out.
*.php is regular script file which, as any other scripting languages like i.e. perl requires interpreter to run. So if you want to run your script from command line you have either call interpreter and give it your script file as argument, like:
$ /usr/bin/php myscript.php
And that's it - it should run.
Or (if working using linux/bsd) add as very first line of your PHP script file:
#!/usr/bin/php -q
which tells shell, where to look for interpreter for this script file. Please ensure your PHP is in /usr/bin folder as this may vary depending on the distro. You can check this using which, like this:
$ which php
/usr/bin/php
if path is right, you yet need to set executable bit on script file so you'd be able to try to "launch it":
chmod a+x myscript.php
This will make it behave as any other app, so you'd be able to launch it this way:
/full/path/to/myscript.php
or from current folder:
./myscript.php
And that's it for that approach. It should run.
So your crontab line would look (depending on the choosen approach):
1 * * * * /full/path/to/myscript.php
or
1 * * * * /usr/bin/php -q /full/path/to/myscript.php
And you should rather use "0" not "1", as 1st minute in hour is zero, i.e.:
0 * * * * /usr/bin/php -q /full/path/to/myscript.php
EDIT
Please note cron working directory is user's home directory. So you need to put that into consideration, which usually means using absolute pathes. Alternatively you'd prepend your call with cd <script working path> && /usr/bin/php -q /full/....
Maybe
1 * * * * php /home/myuser/domains/mysite/public_html/live.php
You need to make your script executable with chmod on the command line.
Furthermore you need something like this:
#!/usr/bin/php
<?php
// here comes your code
?>
This is required to tell the command line how to execute the file.
Note: this will only work if your php binary is located in /usr/bin/php
I am running Ubunutu Linux server, PHP5, Apache2 and am having trouble getting any sort of cronjob to run through the crontab.
I edit the crontab using
crontab -e
I save the file I want to run:
*/5 * * * * php /home/user/public_html/crx/cronx.php
it saves fine. I can run the file from the console and goes through fine. I can't even find any existing logs for the file. I checked cron was running, stopped and started... no change.
The current php file is just a simple test script that inserts a single line into a database.
I checked the permissions for the file and has read and write. Am absolutely stumped. I can't seem to get ANYTHING to run through cron. Is there something I can run to test permissions?
EDIT
I have also tried the following command
/usr/bin/php /home/user/public_html/crx/cronx.php
I used whereis php and which php to locate and confirm it is all running in the right area
You have too many * values for your times.
Also, cron may not have a PATH set up correctly to use PHP.
Instead try:
*/5 * * * * /usr/bin/php /home/user/public_html/crx/cronx.php
Where /usr/bin/php is the actual path to PHP. From the console you can run which php to see the path to the PHP binary you should use.
EDIT: Here are a couple of more things to try in order to troubleshoot:
# see if cron is running just by having it create a file
*/5 * * * * touch /tmp/crontab-$(date +%s)
Another option:
Set the permissions of your PHP script to 755, and change the beginning to:
#!/usr/bin/php -q
<?php
// rest of script
Then change your cron tab to:
*/5 * * * * /home/user/public_html/crx/cronx.php
I'm still not sure if cron is the issue or the running of the PHP script.
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
I'm trying to run a test script using crontab within Plesk. The php file simply emails me a message
mail('me#somewhere.com','Cron Test','Test');
My path to php is /user/bin/php
I have entered * in every field, to run the script every minute with the following command:
/usr/bin/php -q /usr/httpdocs/crontest.php
However, the script is not being run.
Can anyone help?
I'm probably missing something simple, I've never used cron before.
Any advice appreciated.
Thanks.
I would start by getting it to write to a log file. eg:
* * * * * /usr/bin/php -q /usr/httpdocs/crontest.php >> /a-location/crontest.log 2>&1
This will at least give you any obvious errors like not being able to find php etc.
I found that when using the user based cron in plesk, there are a number of issues:
first I found that you should reference the script from the virtual domain. If your script has an absolute address of /var/www/vhosts/domain.com/httpdocs/email-this.php, you should reference it as httpdocs/email-this.php in the crontab.
Second, the script has to have very particular permissions, but not sure what they "must be." apache:apache is all that ever worked for me. Even with the group write permission set, user still had to be apache... weird.
Third, the easiest way to do the testing was to edit the crontab directly instead of going back into plesk every time I needed to make a change... Edit your crontab like this:
crontab -u [filesystem-username] -e
Fourth, I could never get the crontab to write to a log file outside of httpdocs (I tried statistics/logs/cron_log every way I could think of... lol... no dice). I ended up just adding the MAILTO directive at the top of the crontab file during testing:
eg:
MAILTO=you#domain.com
## * * * * * php -q httpdocs/cron.php
Also see this if you have Plesk 10 or above: http://shaun.net/2011/09/solving-plesk-10-3-1-cron-issues/
I had to do this
/usr/local/psa/bin/server_pref -u -crontab-secure-shell "/bin/sh"
to get this (example) working: wget -O - http://www.yourdomain.com/cron.php