crontab not running php script - php

crontab was at one point running properly but one day after running it deleted its file in /var/spool/cron/crontabs
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.DYqvRY/crontab installed on Thu Mar 17 14:50:32 2016)
# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 0 1 * * /var/www/html/mail.php
0 0 16 * * /var/www/html/mail.php
0 13 2 * * /var/www/html/mailcheck.php
0 13 17 * * /var/www/html/mailcheck.php
0 13 2 * * /var/www/html/mailcheckadmin.php
0 13 17 * * /var/www/html/mailcheckadmin.php
0 0 1 * * /var/www/html/PaymentPeriod_Create.php
0 0 16 * * /var/www/html/PaymentPeriod_Create.php
* * * * * /var/www/html/testsession.php > /var/www/html/log
I am using # crontab -e to edit this file then when i'm done I press ^X Y ENTER
is there any extra step that i am missing * * * * * /var/www/html/testsession.php > /var/www/html/log this should run every minute right?

The syntax * * * * * /var/www/html/testsession.php > /var/www/html/log is valid.
Most likely since it is the final line in the crontab it is missing the newline. Cron requires a newline at the end of every entry; in other words your crontab must finish with an empty line.
From the "Diagnostics" section of man crontab:
cron requires that each entry in a crontab end in a newline character. If the last entry in a crontab is missing the newline, cron will consider the crontab (at least partially) broken and refuse to install it.
You may want to replace the > with a >> so that new content is appended to the log file rather than overwriting it every minute, ie * * * * * /var/www/html/testsession.php >> /var/www/html/log. This will still create the log file if it is not already present.
Your PHP file will also need the execute bit set and will need to start with #!/usr/bin/php (or the path to PHP on your system) on the first line. Alternatively you could replace the cron line with * * * * * /usr/bin/php /var/www/html/testsession.php >> /var/www/html/log to explicitly use the PHP interpreter to execute the script.

Related

Cron job not working using crontab linux

I try to run my laravel command through cron job. But when I put my laravel command into cron job using crontab after adding in cron tab i did not see the working on cron job because my database not updated. Below is my cron tab file
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/java-8-oracle/bi$
SHELL=/bin/bash
MAILTP=isnap#gmail.com
* * * * * php /home/isnap/test/api/local artisan command:trending_posts_view && /tmp/myscript.sh
Help to solve this issue
I think it should be:
* * * * * php /home/isnap/test/api/local/artisan command:trending_posts_view && /tmp/myscript.sh
(space between local and artisan replaced with slash)

Schedule Crontab for running PHP script

I am trying to run PHP file on path /var/www/html/rss_feed/mirror.php through cron tabs. For this I performed the below steps.
sudo crontab -e
Then I edited the file by inserting the following code
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# avt 5 a.m every week with:
# 0 5 * * 1 tar -zcf /ar/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php
#
# m h dom mon dow command
As you can see below is the line of code inserted to schedule cron tab after every 2 minutes to run a php file.
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php
I even tried following
2 * * * * lynx -dump http://192.168.0.232/rss_feed/mirror.php > /dev/null 2>&1
and even this
2 * * * * /your/path/to/php /var/www/html/rss_feed/nbt_times.php
and many more formats from different articles.
The php script is not running. Do I need to put the code somewhere else? Or I am on the wrong way. Can you please help me find out what is the issue.
It should work but need to get some clarifications here, file name seems to be different one time its mirror.php and another time its nbt_times.php
which php file has to run on the specific time that has to be specified. As you specified you're running /var/www/html/rss_feed/mirror.php then, then specify mirrror.php on your first command .
The 2nd and third command will run the job for 2nd minute of every hour. like (4:02,5:02,6:02, etc)
Try this crontab
*/2 * * * * /usr/bin/php /var/www/html/rss_feed/mirror.php
This will run for every 2 mins hope this works.
Try with php5-cli:
*/2 * * * * /usr/bin/php5-cli -f /var/www/html/rss_feed/nbt_times.php
Also add write to logs and see them after minute:
* * * * * /usr/bin/php /var/www/html/rss_feed/nbt_times.php >>log.log 2>>log.err
Check permissions of your php-file. Make 0777 for checking. Try create the simplest php file with one row and run it via cron.

Shell via PHP: Trouble adding new cron job to crontab

I have several scripts scheduled to run in my crontab, but can only see them as root (or using sudo). I need to have a PHP script (which is being run as nginx) be able to add a new line to the crontab file. To do this I created a shell script (owned by root) and gave the nginx user permission to sudo it via the /etc/sudoers file.
The last line of the /etc/sudoers file:
nginx ALL=NOPASSWD: /etc/nginx/addcron.sh
The PHP call to execute the script:
chdir("/etc/nginx/");
echo exec("2>&1 ./addcron.sh $custname", $output);
echo "<pre>".print_r($output, true)."</pre>";
My current crontab:
[ec2-user#ip-172-31-xx-xxx nginx]$ sudo crontab -l
* * * * * env > /tmp/env.output
* * * * * /usr/bin/php -f /var/www/html/example/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/demo/cron/cron.php
0 23 * * * rm /tmp/cachegrind.out.*
Meta information about my addcron.sh file:
[ec2-user#ip-172-31-xx-xxx nginx]$ pwd
/etc/nginx
[ec2-user#ip-172-31-xx-xxx nginx]$ ls -al addcron.sh
-rwxr-xr-x 1 root root 129 Nov 24 22:16 addcron.sh
The contents of addcron.sh:
#!/bin/bash
custname="$1"
(crontab -l; echo \"* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php\" ) | crontab -
When I try to run this though, I get the following error:
errors in crontab file, can't install.
Array
(
[0] => "-":102: bad minute
[1] => errors in crontab file, can't install.
)
It seems like it doesn't like the - mark in addcron.sh, but my Google searches suggest this is correct. Also, I have tried adding sudo to the PHP's exec command, but then I just get the following error:
sorry, you must have a tty to run sudo
What am I doing wrong or missing, and why?
Side note: running cron jobs unrelated to the system admin under the root's account is a BAD IDEA from the security prospective. Install your crons under the nginx user.
The issue is caused by the escaping of the quotes in your bash script (which you can check directly in bash, piece by piece, btw):
> (crontab -l; echo \"* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php\" )
no crontab for username
"* ... <all kinds of filenames in here> ... /usr/bin/php -f /var/www/html/the_customer/cron/cron.php"
Without the quote escaping things work a bit better:
> (crontab -l; echo "* * * * * /usr/bin/php -f /var/www/html/$custname/cron/cron.php" )
no crontab for username
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
You might want to add some protection against duplicating the entries, here's how crontab looks after a few invocations:
> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:10 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:09 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:09 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:08 2015)
# (Cronie version 4.2)
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (- installed on Tue Nov 24 20:54:07 2015)
# (Cronie version 4.2)
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
* * * * * /usr/bin/php -f /var/www/html/the_customer/cron/cron.php
I recently published a project that allows PHP to obtain and interact with a real Bash shell. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$custname= "someone";
$strCmd = "echo \"* * * * * /usr/bin/php -f /var/www/html/".$custname."\" >> /cron/cron.php";
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1 = $shell->exeCmd($strCmd);
//assuming your distribution reloads / restarts using 'service'
$return2 = $shell->exeCmd('service crond restart');
this will append your line to the cron file.

Cron job not working in Raspberry

I want to add a cron job in my Raspberry to execute a task every five minutes.
So I do in the terminal:
crontab -e
and then add at the file:
*/1 * * * * /usr/bin/php myscript path.
The script is something very simple. just to try if it works:
<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = date("l jS \of F Y h:i:s A") . "<br>";;
fwrite($myfile, $txt);
fclose($myfile);
?>
The problem is that the date isn't updated, so the cron job doesn't work. Any idea of the problem ?
UPDATE
This what I got, when I execute crobtab -e
GNU nano 2.2.6 File: /tmp/crontab.3IXg0z/crontab
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * /usr/bin/php /full/path/myscript.php
First, make sure the script is executable:
chmod +x /path/to/some/script.php
Second, make sure your script has an appropriate #! (or "shebang") on the very first line:
#!/usr/bin/php
Then make sure your cron job is configured correctly. The format for cron is typically m h dom mon dow command
sudo crontab -e
*/5 * * * * /path/to/some/script.php
If you wanted to run a script every 5 minutes you should add this entry.
*/5 * * * * /usr/bin/php /full/path/to/php/script.php
Make sure you have PATH variable set correctly on crontab, so it can locate your file.
You can simple put following line in top of crontab
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/path/to/newfile.txt
Try, following test
* * * * * touch /tmp/hello
Do following to redirect result
*/5 * * * * /usr/bin/php /full/path/to/php/script.php > /tmp/out.txt
Make sure your script running on command line.
/usr/bin/php /full/path/to/php/script.php
Use the -f option to execute the script:
*/5 * * * * /usr/bin/php -f /full/path/to/php/script.php
Tail logs file to see its executing every 5 min
tail -f /var/log/cron

how to run a php script daily with the cron job on Ubuntu os

command to runI am using ubuntu 12 and lamp server . I want to run a php script after every 1 hour . i have create a crontab to execute this and if i check my cron list with command crontab -l it is showing like this
# Edit this file to introduce tasks to be run by cron.
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
this is my php script
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
but it is not executing
how can i check why it is not working , please help
You can use crontab to add/remove/edit cronjobs.
Hit Alt+Ctrl+T to open terminal.
First make sure the script is executable by running:
chmod +x YOURSCRIPT
Then run the following command to add your cronjob:
crontab -e
Add your cronjob like this:
0 * * * * /usr/local/bin/php path/of/php/file
That's it!
Your can check the current user's crontab entries by running:
crontab -l
For more information about crontab run:
crontab --help
OR
man crontab
To find out what is wrong with your cron you can type the following command in your terminal:
grep -i "cron1.php" /var/log/syslog
The syslog contains all log of crons.
Try run the code /usr/bin/php5 -q /var/www/cronjobs/cron1.php on terminal to check if there are errors.
You can also redirect all errors to a file:
0 * * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php 2> /tmp/errorCron1.txt
Make Script File /etc/scripts/cron.sh with contain
cd /var/www/
/usr/bin/php cron.php
Save it then
chmod +x cron.sh
then go on /etc/crontab
15 15 * * * root /etc/scripts/cron.sh
save it
and wait
This is the description of crontab arguments
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat)
0 2 12 * * /usr/bin/find
To Run your script every hour use below crontab entry.
0 */1 * * * /usr/bin/php5 -q /var/www/cronjobs/cron1.php
This way your script will start executing every hour.

Categories