Execute php script with crontab doesn't work - php

I'm trying to execute a php script every day to send an automatic email to the clients that have contacted me. To try if it works I'm trying to execute it every minute. I have followed these steps:
whereis php prompts
php: /usr/bin/php /usr/share/php /usr/share/man/man1/php.1.gz
2. Then I make crontab -e and add a line:
* * * * * /usr/bin/php /var/www2/www/centros-sbc.com/public_html/mail/mail_automatico.php &>> /dev/null
Then I wait a minute and nothing happens. Have any idea?

Every minute is "* * * * *"
You've done "every hour at one minute past"

Have your tried
/usr/bin/php -f /var/www2/www/centros-sbc.com/public_html/mail/mail_automatico.php &>> /dev/null
-f option is here to execute the file (your file) mail_automatico.php

1) check if your cron job is working (service crond status | systemctl status crond)
2) &>/dev/null is normally equal to > /dev/null 2>&1.
But some shells doesn't support it.
have you tried > /dev/null 2>&1 instead &>/dev/null ?

Finally I succeed by making it more simple:
/usr/bin/php /var/www2/www/centros-sbc.com/public_html/mail/mail_automatico.php
Thanks to all

Related

How can I prevent creating log files by CronJob?

I have a Cronjob. My command is that: wget mysite.com/bot.php But this command will automatically creates log files that names are like:
bot.php
bot.php.1
bot.php.2
bot.php.3
etc...
How can I prevent this?
Thanks!
I assume you're trying to run your script without storing any information locally.
Try adding &> /dev/null to the end of your cron command, this will dump the output to /dev/null which is basically a black hole ( anything that ends up here is irretrievable ).
An example cron command would be:
*/5 * * * * wget -qO- http://example.com/check &> /dev/null

How set cron job - PHP in digital ocean for daily bases?

I have set cron job command on my digital ocean server.
0 1 * * * php /var/www/html/domain/cron/index.php
is it right coded??
Because its not running daily.
Had check of 5mint and hourly, its working fine but not for each day.
Please help me to find out the solution.
Thanks in Advance.
0 1 * * * means your php file will execute on 01:00:00, and this depend on your where your server is located (server's time zone).
Check this site. it will help you to set time for your cron job.
and if your cron is running for 5 minute, it should work for daily basis, you have to just figure out at what time it should run.
Check if your cron commands are OK by typing:
$ crontab -l
or right in
/var/spool/cron/crontab
Your command seems to be OK for a script that will be executed every day at 01AM.
Login to your droplet and then follow this steps
crontab -e
* * * * * cd project_path/ && php artisan schedule:run 1>> /dev/null 2>&1
Example:
* * * * * cd /var/www/html/blog/ && php artisan schedule:run 1>> /dev/null 2>&1

Run PHP script in crontab only works when exporting to a file

On CentOS release 6.5 (Final):
I know that usually I just need to use the following command in crontab to run a php script.
0 * * * * /usr/local/bin/php absolute_path_file_to_the_script.php
But, recently, it stopped working. The only work around is to use the following command
0 * * * * /usr/local/bin/php absolute_path_file_to_the_script.php > log
But I would rather not to output anything to log for now.
So, I even tried
0 * * * * /bin/sh -c "/usr/local/bin/php absolute_path_file_to_the_script.php"
But the above commend is again not working in crontab (it works if I type in the shell directly).
And I am sure that the above command did run in crontab for a second with
ps ux, and then it stopped executing.
Any ideas on how to run the command properly without logging?
Try run that command (only php, without cron settings) from terminal and show result
Both answers from Marc and Greg work:
> /dev/null
or
> /dev/null 2>&1

Problem running a small script as cron job

I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.

storing crontab php outputs in a log file

* * * * * php /home/admin/public_html/domain.com/public/cron/route.php &>> /home/admin/public_html/domain.com/log/cron.log
I have that cron running every minute.
I want to store the errors that occur in route.php in cron.log
This works wonderfully when I run :
php /home/admin/public_html/domain.com/public/cron/route.php &>> /home/admin/public_html/domain.com/log/cron.log
through the command line manually. But when crontab runs it no errors gets stored in cron.log
the cron.log is owned by admin:admin and the permissions are set to 777 just to be sure.
anyone?
Something like this:
* * * * * /usr/bin/php /home/admin/public_html/domain.com/public/cron/route.php >> /home/admin/public_html/domain.com/log/cron.log 2>&1
Where the standard error is redirected to the same destination as standard output (with 2>&1), this will collect any errors outside of the script and add them to the cron.log file.
This can be simply called with > symbol on cron or php -q or -f command.
On your case
php -q /home/admin/public_html/domain.com/public/cron/route.php > /home/admin/log.txt 2> /dev/null
Blockquote php -q /home/admin/public_html/domain.com/public/cron/route.php > /home/admin/log.txt 2> /dev/null

Categories