Need to edit root crontab jobs from webinterface?? - php

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

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

Running a PHP script with crontab

I understand SO is for questions but no matter how many tutorials I have looked up, I cannot get my crontab to work and I am building an website that will rely on crontab to reset a particular setting in my database every night.
Here is my crontab file:
# 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
* * * * * /usr/bin/php -q /var/www/html/cron/index.php
If I try to cd to /usr/bin/php i get
-bash: cd: /usr/bin/php: Not a directory
So I cd'd to just /usr/bin/ and this is what i found:
-rwxr-xr-x 1 root root 27216 Feb 10 15:08 pgrep*
lrwxrwxrwx 1 root root 21 Jun 13 09:36 php -> /etc/alternatives/php*
-rwxr-xr-x 1 root root 9049256 Jul 2 11:57 php5*
If I cd to /etc/alternatives I find:
lrwxrwxrwx 1 root root 13 Jun 13 09:36 php -> /usr/bin/php5*
I go back to the bin file, php5 has the * symbol and is green.
-rwxr-xr-x 1 root root 9049256 Jul 2 11:57 php5*
My PHP script. Very simple. Checks for a cookie and if it exists increments it by one. I then check the results on another page. Manually this works. With crontab, cannot get it to work.
if (!empty($_COOKIE['cronTest'])) {
$int = $_COOKIE['cronTest'];
$int++;
setcookie("cronTest", $int, time()+3600);
}
Most likely, your script inside /var/www/html/cron is owned by www-data user. Depending on your setup the user executing the cronjob doesn't have permissions to run this file.
There are no $_COOKIEs on the command line. A cookie is sent by the users browser. As cli isnt a browser, so you can't read the cookies value. Though you could access a users $_SESSION, but that's another story. Have a look here Is it possible to read cookie/session value while executing PHP5 script through command prompt? for further details.
Your cronjob line looks valid, so the problem will be one of above points. To verify the first, try something without the use of $_COOKIE in your file, like simply
mkdir('/var/www/html/cron/testdir');
just to see if the file can be accessed and a directory is created inside above dir. If it can't be accessed, create a new group and add both the current user ( find out with
ls -al
in /var/www/html/cron ) and the user running the cronjob to the group, then make that group own the file you want to run. See the accepted answer on this question: Set user permissions | Artisan command will not run in code but works fine on command line I posted some time back on how to do that.
For the $_COOKIE problem, you will have to find another solution. For example use Redis or Memcached as a caching service that can be accessed both by online and cli configurations.
Consider to add
1>> /dev/null 2>&1
to the end of your cronjob line, if you don't do so and let cron run this every minute, you will get masses of logfiles.
For the sake of completenes on possible pitfalls when working with php and the cli, allways make sure to provide full paths to your files.

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

Run PHP script with CRON

I try to run PHP scripts on my debian server, everyday at midnight & 1 minute.
So I created the crontab file in admin, with
crontab -e
who seems to be in
/tmp/crontab.ky3Q3F/crontab
And I put the line
01 00 * * * /usr/bin/php5 /var/www/MySite/cronTB.php
Bellow the comments.
The goal is to run cronTB.php (with PHP) everyday at 0h01.
Is my syntax correct ?
Is the correct file to edit, to the correct path ?
Is the CRON daemon still runing after I run it with:
/etc/init.d/cron start
even if I close the ssh connexion ?
Thanks for help

Yii and cron jobs

I have created a Yii command that needs to be run every month. If I go to my protected folder and run the command manually:
protected/yiic ganadores
It works fine. I have tried to add the following command line to etc/cron.hourly and etc/crontab with no success:
/usr/bin/php5 /var/www/path/to/project/protected/yiic ganadores (etc/cron.hourly/ganadores)
0 0 1 * * root /usr/bin/php5 /var/www/path/to/project/protected/yiic ganadores
(etc/crontab)
If I run the file ganadores inside etc/cron.hourly manually, it's working also.
What am I missing here?
Edit: Finally got it solved. I had some extra spaces in the cron line. Used tab instead spaces and it started working..
This is how I run my Yii cron jobs (in the root crontab file):
45 23 * * * sudo -u www-data php /path/to/yii/app/protected/console.php mycommand
Basically just regular crontab syntax, but I am running console.php instead of yiic, and I am setting the user to Apache (www-data) so the permissions are correct for my script. I am not sure why yours isn't working, but hopefully looking at mine will help you out. :)

Categories