Schedule Automatic Server Maintenance [Ubuntu] - php

I'm running an Ubuntu 12.10 server as a LAMP stack. I access this through a SSH terminal (as I don't have access to the physical box).
My question is, is there a way to setup ubuntu to automatically execute a script at chosen times. Like to schedule automatic maintenance scripts that have been built in PHP or really any language (possibly Bash Scripts?). I'm sure this has to be possible.
Since I am a bit newer to linux/ubuntu, any help would be appreciated.
EDIT:
This is one solution I found if anybody else stumbles across this...
sudo crontab -e
...
#daily /usr/bin/wget -q -O /var/log/maintenence.txt /var/www/admin/script.php

You can use the crontab in linux.. That wil suit your purpose.
Cron is a daemon that executes scheduled commands
eg:
# 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 * 0,6 /usr/bin/find

Okay for example.. if you need to run the script file /home/krizna/backup.sh every sunday 12 PM .. just issue the below command
sudo crontab -e
and add this line at the end of the file
# Minute Hour Day of Month Month Day of Week Command
00 12 * * 7 /bin/sh /home/krizna/backup.sh

Related

Script runned by crontab will no produce data

Using Cacti, I'm trying to run the following command:
php poller.php > test.cfg
As expected I'm getting the following output :
root#SLINUX01:[/usr/local/cacti]$tail -f test.cfg
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
05/12/2016 01:23:50 PM - POLLER: Poller[0] CACTI2RRD: /usr/bin/rrdtool update /usr/local/cacti/rra/stse01_traffic_in_14.rrd --template traffic_out:traffic_in 1463052230:2773678063:3900704413
05/12/2016 01:23:50 PM - POLLER: Poller[0] CACTI2RRD: /usr/bin/rrdtool update /usr/local/cacti/rra/stse01_traffic_in_15.rrd --template traffic_in:traffic_out 1463052230:1071486:1088742
05/12/2016 01:23:50 PM - SYSTEM STATS: Time:5.4128 Method:cmd.php Processes:1 Threads:N/A Hosts:3 HostsPerProcess:3 DataSources:9 RRDsProcessed:7
Loop Time is: 5.41
Sleep Time is: 294.58
Total Time is: 5.42
^C
And my manipulated has updated.
Now using the crontab, I made the following :
*/5 * * * * cacti php /usr/local/cacti/poller.php > /home/cacti/test.cfg &>/home/cacti/test.cfg
But when i go in the /home/cacti/test.cfg after cron execution , I got nothing, but the cron runned the script :
May 12 15:25:01 SLINUX01 CROND[19871]: (root) CMD (cacti php /usr/local/cacti/poller.php > /home/cacti/test.cfg &>/home/cacti/test.cfg)
What is the source of this problem?
Thanks in advance.
Your cron log entry only says that the command was tried. It does not guarantee that the command has been successfully started (permission problems? environment variables unset? unmounted filesystem?).
Do you use the cron table for the same user that can successfully run the command from command-line?
What are the exact paths when you run from command line?
Note that in crontab you try to invoke php without giving its full path - that may well be the culprit.

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

Seems Cron job working even after Stopped

one of my cronjobs send an email daily
35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1
I stopped cron using
sudo service cron stop
when i check the cron status
sudo service cron status
it is cron stop/waiting
the problem is i stopped my crons yesterday , but today also the email was sent again
am i doing this wrong
1.how to stop all cron jobs properly
2.how to view last ran cronjob list .
i tried to get last ran cronob details by
grep cron /var/log/syslog
but it is empty , but when i go to the syslog file i can see the word CRON in some places .
Aug 12 09:10:01 64177 CRON[6388]: (root) CMD (cd /var/www && /usr/bin/php runcronjobs.p......
also when i tries typing
ps -ax|grep cron
5696 pts/3 S+ 0:00 grep --color=auto cron
13011 ? Ss 0:00 cron l
is it possble to run crons after i stopped the cron jobs ??
Thanks in advnace
The proper way to do it is to put every job or every group of jobs in their own 'namespace' / file:
/etc/cron.d/production-job-mail-on-delivery
/etc/cron.d/production-aggregate-account-data
/etc/cron.d/devel-job-mail-on-delivery
/etc/cron.d/system-cleanup-sundy
/etc/cron.d/common-mailings
When you want to stop, you just move those files out of the /etc/cron.d/ folder.
You should not stop the cron service and you shouldn't use crontab / /etc/crontab on a bigger system.
Why?
For example, I have 200 cronjobs on a system. /etc/crontab file gets very messy. Some jobs need to be stopped for a week for example, others need to run permanently. Using cron.d folder this is a very simple task.
Normally you would access your crontab with:
crontab -l (lists cronjobs)
and to edit:
crontab -e
to stop a cronjob temporarily you can usually just comment out the first digit:
# 35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1
to no longer run a cronjob permanently just delete the particular job. crontab usually restarts itself automatically after you edit it. if not trying using:
sudo restart cron
more information: http://man7.org/linux/man-pages/man1/crontab.1.html
The simple way to do it is commenting the cronjob trough a crontab -e:
#35 6 * * * cd $EZPUBLISHROOT && $PHP runcronjobs.php -q 2>&1
Other than that you should configure the time the frequency of the cronjob to run.
To stop processes you can
ps|aux grep runcronjobs
and after you get a list of the processes runing you can just kill a process using its number
kill *number

Upstart - Job starts but then stops immediately

I am trying to set up an upstart job but for some reason it stops immediately after starting
I'm on Cents 6.5 btw
here are my files:
/ect/init/test-daemon.conf
start on startup
stop on shutdown
respawn
script
sudo -u root php -f /usr/share/test_daemon.php
end script
/usr/share/test-daemon.php
<?php
// The worker will execute every X seconds:
$seconds = 2;
// We work out the micro seconds ready to be used by the 'usleep' function.
$micro = $seconds * 1000000;
while(true){
// Now before we 'cycle' again, we'll sleep for a bit...
usleep($micro);
}
I have then got this file: (which I found on a forum that logs events)
/tmp/log.file
debug/ (/dev/fd/10):19735:Fri Jul 25 11:52:40 AST 2014:Job
test-daemon/ starting. Environment was: TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PWD=/ JOB=test-daemon
SHLVL=1
UPSTART_INSTANCE=
UPSTART_EVENTS=starting
UPSTART_JOB=debug
INSTANCE=
_=/usr/bin/env
debug/ (/dev/fd/9):19775:Fri Jul 25 11:52:41 AST 2014:Job test-daemon/ stopping. Environment was:
TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PWD=/
JOB=test-daemon
RESULT=ok
SHLVL=1
UPSTART_INSTANCE=
UPSTART_EVENTS=stopping
UPSTART_JOB=debug
INSTANCE=
_=/usr/bin/env
debug/ (/dev/fd/9):19779:Fri Jul 25 11:52:41 AST 2014:Job test-daemon/ stopping. Environment was:
TERM=linux
PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
PWD=/
JOB=test-daemon
RESULT=failed
SHLVL=1
PROCESS=respawn
UPSTART_INSTANCE=
UPSTART_EVENTS=stopping
UPSTART_JOB=debug
INSTANCE=
_=/usr/bin/env
in the log file, I get more debug information but its basically the above repeated a few times.
I get this from running start test-daemon which outputs test-daemon start/running, process 20600
I know the test-daemon.php doesn't actually do anything...at the moment I just need to get the actual job running, once thats fixed ill drop in my code
So from the above...is there anything I am doing wrong? as the job should only stop if I run stop test-daemon right?
Any suggestions would be much appreciated :)
Thanks,
Dave
I'd rather post a comment but with the low reputation limit I can not.
I am not familiar with this distro flavor but did you try to use nohup ?
Like this:
nohup php -f /usr/share/test_daemon.php &
It could be possible that detaching your command from the service process kills it.

How can I run same command in a shell every 3h

I have a bunch of Behat test scenarios, about 3000, these take about 3h to run, and I have deducated machine to run these, so looking for a command which would trigger this command:
./behat --tags oxi --profile staging --format custom
Every 3h. Is there any way to do this?
I looked at "watch" command but even if I do run it:
watch -n 10800 ./behat...
the watch for some reason is running it every 4294 seconds. Not 10800 which I stated in command. So is there any other way of doing this?
Thank you.
First move the command to it's own script vi /usr/local/bin/dowatch:
#!/bin/bash
./behat --tags oxi --profile staging --format custom
Make it executable
chmod +x /usr/local/bin/dowatch
Cron seems like a good choice for scheduling... in the shell:
crontab -e
Then edit the source to:
* */3 * * * /usr/local/bin/dowatch
Which says any minute (first) every three hours (second) any day of the month (third) any month (fourth) any day of the week (fifth) execute /usr/local/bin/dowatch

Categories