Run PHP script at regular intervals using CRON - php

I am kinda newbie to this, I have a php script which I want to execute after every 1 hour, for this I updated the crontab file inside the /etc directory but I am not able to see whether its actually being called.
Here's the entry in my crontab file
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
*/5 * * * * /usr/bin/curl -o temp.txt http://myurl.com/postparser.php
I am not even able to see any temp.txt file getting generated
Can someone point me in the right direction

try running
*/5 * * * * /usr/bin/php /path/to/php/file/postparser.php
Change the /usr/bin/php part to your php executable path.

Not sure about this but, changing just the file would have no effect.
Try editing the tasks using the command crontab -e ("e" for editing).
If you want to edit the crontab for a certain user, use the -u parameter
for more, check the man:
http://linux.die.net/man/1/crontab
Good luck

Related

crontab printing script instead of executing it

here my problem width crontab:
I need to run a php script everyday at noon and so on my crontab i set:
# 0 0 * * * /usr/bin/php -c /var/www/fanta-trade/operazioni_pianificate_medaglie.php > /var/www/cron_log
0 12 * * * root /usr/bin/php /var/www/offertegaseluce/operazioni_pianificate.php > /var/www/offertegaseluce/cron_log
*/15 * * * * /usr/bin/php -q /var/www/fanta-trade/operazioni_pianificate_ordini.php > /var/www/cron_log
#20 */6 * * * /root/certs/certbot-auto renew --quiet --no-self-upgrade
the line that i need is actually the second... i tried many debugging:
I checked the timezone and it is properly set to europe-rome
I checked width whereis the position of php and /usr/bin/php is correct
I tryed to run the script as root or just widthout user specification
0 12 * * * /usr/bin/php /var/www/offertegaseluce/operazioni_pianificate.php > /var/www/offertegaseluce/cron_log
I checked if crontab as a sevice is running and restarted it
I checked if the script itself is reacheable and if it works and if i call the script via browser it works
but actually crontab is not executing the script it just copy paste the cript itself to the log file
any suggestion?
thanks

Execute PHP in cron job on centos

I needed help on my cronjob experiment, im unable to accomplish what i wanted. below are the things I did.
Create a php file inside /var/www/html/bob/test/index.php
Set permission rights of the directory and file to executable (0777)
Specify a cronjob
My index.php
<?php
$myfile = fopen("testfile.txt", "w");
?>
My various tested parameters for crontab -e which didn't work
*/1 * * * * root /usr/bin/php /var/www/html/bob/test/index.php
*/1 * * * * root /var/www/html/bob/test/index.php
*/1 * * * * root php -q /var/www/html/bob/test/index.php
*/1 * * * * root /var/www/html/bob/test/index.php
looking at some of my /var/log/cron I dont see any error indication.
Sep 6 15:55:01 localhost CROND[4866]: (root) CMD (root /var/www/html/bob/test/index.php)
Sep 6 15:56:01 localhost CROND[4872]: (root) CMD (root /var/www/html/bob/test/index.php)
Sep 6 15:57:01 localhost CROND[4878]: (root) CMD (root /var/www/html/bob/test/index.php)
Lastly, when i run my php via #php /var/www/html/bob/test/index.php the code executes fine and creates that textfile.
You need to remove root from your cronjob. The log is showing it's trying to execute a program called root with /var/www/html/bob/test/index.php as a parameter.
Instead, use the following as your cronjob:
*/1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
This answer states that if you want to set the user for a cronjob to be run, you have to edit /etc/crontab directly, not by using crontab -e.
1 * * * * /usr/bin/php /var/www/html/bob/test/index.php
if this not working, so you have a probleme of permission , try to use chown and chmod for giving the right permission
and for $myfile = fopen("testfile.txt", "w"); try to use the absolute path for the file testfile.txt like :
$myfile = fopen("/home/xxxx/testfile.txt", "w");

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.

Using Sleep command in Crontab CentOS

I am trying to execute a php script in conjunction with the Sleep command from Crontab. Here is the relevant code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
# run-parts
* * * * * sleep 5 && root /usr/bin/php /path/to/the/php/file.php
But it won't execute at all. Any hints?
Remove root from sleep 5 && root /usr..
If you want to run it as root, the correct format is:
* * * * * root sleep 5 && /usr/bin/php /path/to/the/php/file.php

Cronjob every minute

I have a file in mysite.com/url1/url2/cronjob.php which has to be run every minute. I try every combination, but can't succeed. What should I run in command line? Thank you.
In case you'd set it up in a crontab, this works:
*/1 * * * * /usr/bin/wget -O /dev/null http://example.com/cron.php
Steps your shell
$ crontab -e
* * * * * php -f /path/to/cron.php
~
~
I got confused for the first time where to add all of these and finally found.
Type the following in the linux/ubuntu terminal
crontab -e
select an editor (sometime it asks for the editor) and this to run for every minute
* * * * * /usr/bin/php path/to/cron.php &> /dev/null
The PHP interpreter.
/[path-to-php]/php -f [your-php-script]
Are you looking for help to make an UNIX cronjob?
If so, have you tried to edit /etc/crontab, and add
\1 * * * * user command
where user is either root or your name. Im not exactly sure how to access and URL, but a dirty way could involve downloading the link as a file, e.g. "wget http://url.com/page.php"

Categories