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
Related
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
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.
I have a PHP script that I want to run forever; from starting up server until shutdown.
PHP script
<?php
require_once("connection.php"); // I am connecting to MySQL with PDO
while(true) {
//some of my code here
....
....
//code ended
sleep(5);
}
?>
My /etc/init/myscript.conf file
description "Endless PHP loop"
start on startup
stop on shutdown
respawn
chdir /var/www/html/
exec php -f script.php
I also had try
script
exec php -f script.php
end script
When I run: start myscript from terminal script is running without any problem but if server is rebooted myscript wont run again. Also sometimes script is stop running (I don't know why) and wont start running again.
I am googling for two days and I did not find solution. Maybe I do not know what to look after.
Could it be that script fails because apache, mysql or php startin up? Is there option to delay script 30 sec after startup?
Try changing your upstart script to:
description "Endless PHP loop"
start on startup
stop on shutdown
respawn
script
sleep 30
exec php -f /var/www/html/script.php
end script
If you wanted to it with cron you need to remove the endless loop from PHP and add these cron entries:
* * * * * /usr/bin/php -f /var/www/html/script.php &> /dev/null
* * * * * (sleep 5;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 10;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 15;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 20;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 25;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 30;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 35;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 40;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 45;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 50;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 55;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
It is a bad idea to do it like this.
PHP is designed to run on request, and not endless. Maybe it can be done, but who knows what memoryleaks you will introduce?
I strongly suggest you look into cronjobs.
Just crontab your code and run it every 5 seconds.
Here is an example to run each minute. I don't think you can go down to 5 seconds using cron.
* * * * * cd /home/yourdir/public_html/admin/ && php -q /home/yourdir/public_html/admin/updatedb.php
Have a look here for another approach:
Running a cron every 30 seconds
I created a file name 'testCrontab.php' in /var/www/html folder on amazon ec2, ubuntu-based,
$output = shell_exec('crontab -l');
var_dump($output);
The problem is when I invoked this file in browser it shows
string(207) "0 0,6,12,18 * * * /usr/bin/php /var/www/html/testExec.php 0 3 * * * /usr/bin/php /var/www/html/testCrawlback.php 0 4 * * * /usr/bin/php /var/www/html/testInsertCard.php * * * * * NEW_CRON * * * * * NEW_CRON "
Which I guessed those NEW_CRON are from the other time I tested inserting a new cron from php,
but when I invoked this file from command line by issuing /usr/bin/php /var/www/html/testCrontab.php it shows
string(169) "0 0,6,12,18 * * * /usr/bin/php /var/www/html/testExec.php
0 3 * * * /usr/bin/php /var/www/html/testCrawlback.php
0 4 * * * /usr/bin/php /var/www/html/testInsertCard.php"
Also, command crontab -l result in the latter output.
Please help enlighten me what happen here.
Result will be different because when you are executing script via browser the user will be www-data or nobody. It will show the crons of that user. If you execute the script from command line it will show the crons of that particular user.
Edit
Set cron for webuser
sudo crontab -u webuser -e
List crons of webuser
sudo crontab -u webuser -l
webuser will be nobody or www-data or apache
You can find it by executing the following code from web browser.
<?php $output = exec('whoami');
echo $output;
?>
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