PHP cron job from MySQL query - php

Thanks for all the help in advance!
I'm just starting with PHP and currently have to work with existing code; which is over my head.
I have a file filled with functions, which includes this MySQL query:
function search_late_orders(){
$this->assert_connected();
$this->assert_table_exists(REGION_PREFIX."Order");
$date = date('Y-m-d H:i:s');
$sql = "UPDATE `".REGION_PREFIX."Order`
SET `".REGION_PREFIX."Order`.`orderfulfilled_ts` = `".REGION_PREFIX."Order`.`orderread_ts`
WHERE `".REGION_PREFIX."Order`.`orderfulfilled_ts` = '0000-00-00 00:00:00'";
}
What i'm attempting to accomplish is to reiterate this function every 90 minutes from a different file. The only caveat is that my DB is roughly 60K rows and growing rapidly. What's the best way to accomplish something like this with an eye on performance.

0 0,3,6,9,12,15,18,21 * * * /usr/bin/php -q /yourpath/search_late_orders.php
you can use crontab to run this task every 90 min

first install cronie.i dont knw which linux flavour u are using.i am using centos
yum install cronie
as a root.if u know the risk of being a root.if not do it as sudo user
first add apache user
useradd apache-Apache
type which php to see path
/usr/bin/php
give necessary permission and ownership
chown apache /path/to/file
chmod 755 /path/to/file
type crontab -l to list cronjobs for that user.
now crontab -e to edit or create new jobs as it will open vi editor.
* * * * * php /location/of/yourfile/scriptto call
add your time in place of star to match your requirement

Related

PHP - MySQL - Backup database daily

I have developed an agenda which works (server-side) with PHP and MySQL. The last thing I need to do is to make an authomatic daily backup of the database.
This is what I have thought may be the easiest: Write a php script which once every 24hs saves a file(Whose name would be a timestamp) in certain folder in the server. Combining this with time machine or some other backup software to backup this folder into an external disk should be enough.
So now, the questions are: How do I make a php script to run automatically once per day? How do I save a file with sql backup from a php script?(Similar to phpMyAdmin export as sql)
Thanks!
If you are running apache server:
sudo nano /etc/crontab
Add below line:
0 0 * * * root mysqldump -u root -proot db_name > /home/username/db_backup/$( date +"\%Y_\%m_\%d" ).sql
0 0 * * * => every 24 hours
If your server is a linux, you can make a cron using this script : cronsql.sh .
#!/bin/bash
path=/home/backup/
projet=(databasename1 databasename2)
now=$(date +"%Y-%m-%d")
for arg in ${projet[*]}
do
if [ ! -d $path$arg ]; then
mkdir $path$arg
fi
cd $path$arg
mkdir dump$now
cd dump$now
mysqldump -dBR --triggers -p"dbpassword" $arg > structure.sql
mysqldump --no-create-info -p"dbpassword" $arg > data.sql
done
It ll generate a folder with database name.
Inside, a folder with date
Inside the folder, a structure.sql file and a data.sql file
>databasename1
---->20151009
---->data.sql
---->structure.sql
---->20151008
---->data.sql
---->structure.sql
---->20151007
---->data.sql
---->structure.sql
for the cron
use crontab -e
then add 59 23 * * * /home/backup/cronsql.sh if cronsql.sh is in /home/backup/
And last but not least,
you can add in crontab
01 01 * * * find /home/backup/ -mindepth 2 -ctime +30 -exec rm -fr {} +
It ll delete every file / folder older than 30 days.
So you have a backup for every last 30days.
And no need to clean it manually
I found out, that you can do it with a *bat, if you are running windows.
This will safe your databases as zip, you also can upload them into ftp by changing this script a little bit.
Follow this link:
http://www.redolive.com/utah-web-designers-blog/automated-mysql-backup-for-windows/
It's easy to do, if you have any questions just ask

Crontab suddenly stop working on server?

I have some crontab set on server on linux platform.Before that 2 days all the cron was running.I dont know what happen with crontab that they are not working now.
All the cron was running before and i have added a new crontab after that they are not running may be this is the problem or is there other problem with that.
I have check ther permission but that is ok with.
New cron i have add look like that:
*/15 * * * * php myproject/sendmail.php
30 5 * * * php myproject/sendmailOnDiscount.php
* */1 * * * php myproject/sendInvitaion.php
The last one have added and before that other was running well.
After adding crontab sendInvitation.php crontab has stop working.
Could any one tell me why crontab is not working now.(All the crontab has stop working)
Maybe it is off, you can turn it on with this command
service crond start
Mostly this problem occurs due to script file permission and ownership of script files. The same problem was faced by me. I found that my script owner was not a super user e.g. root.
So, you have to set the permission and ownership of your scrip as super user. Find below.
First of all edit your crontab as super user.(in RHEL like below)
[abc#host] crontab -e
and save crontab :wq!
Now set permission for script
[abc#host] chmod +x script.sh
[abc#host] chown root:root script.sh
Now restart your crontab.(in RHEL like below)
[abc#host] /etc/init.d/crond restart

Set cronjob script on server

I'm trying to set up a cronjob script on my server. I've followed this tutorial and I now have a folder "scripts" with "cronjob.php":
<?php
define("_CRONJOB_",true);
require(APPLICATION_PATH . '../public/index.php');
// my executions
?>
In my "index.php" file:
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
But how can I set this on my server?
I've done the following as a start: chmod 755 cronjob.php, but what's next?
Use crontab, make sure you have both crontab and php-cli installed.
First edit the cron by doing
$ crontab -e
Then insert something like this
*/10 * * * * /usr/bin/php /path/to/scripts/cronjob.php
This examples does execute the script every 10th minute.
For more on the syntax see https://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions
Alternate method for setting Cron job on Linux Server
Step 1: Open Terminal
Step 2: Type
$ sudo crontab -e
Enter System Password
Step 3: Put Cron URL to be executed for every 5 minutes interval
*/5 * * * * curl http://testwebsite.com/hitcronscript
Reference: https://crontab.guru/every-5-minutes
*Note: For test purpose one may set mail or file/Db entry.
Hope it will help developers.

Need to edit root crontab jobs from webinterface??

I want to edit the crontab of root from the web interface. I have Apache and PHP installed on my Ubuntu. If there is any permissions required please also mention that.
I want to edit the crontab of root. I have some jobs running in that crontab. One of my projects wants me to change the running jobs Time from web interface.
So please tell me how can I access the crontab of root and edit it.
permission problems: solve those by allowing www-data to run the appropriate commands via sudo.
run crontab -u root -l to get the current crontab, edit it with php string manipulation code of your liking and intall it with crontab -u root $FILE. i recommend to place markers in the file to make it easier to find the correct places to edit like so:
# Edit this file to introduce tasks to be run by cron.
# ...
# m h dom mon dow command
0 14 27 * * backupmails-monthly.sh
0 14 * * 5 backupmails-weekly.sh
# MARKER_EDIT_HERE_START
0 14 27 * * job_to_edit.sh
# MARKER_EDIT_HERE_END
a better solution if jobs run regularly: don't put them in the crontab but in /etc/cron/cron.{hourly,daily}.
if you have access to the console, you should install webmin for web management interface of the server
http://www.webmin.com/
These days there are tools like
chronos (depends on apache, has a dockerfile)
luigi from spotify
minicron
More options found at the github topic: cron

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.

Categories