As far as I understand it .... anacron is not a daemon and is ran by cron. cron runs at boot and runs anacron. anacron checks to see if something its programmed with has missed its schedule and runs it.
Since this is the way I see it ... it would seem that you would want the daily job set in anacron. As for hourly in anacron .. I also haven't seen anything on how it can be setup so I would put the hourly backup in cron.
I know this doesn't explain it that well but ... I guess it confuses me too. I'm sure someone here can come up with a better, more satisfying answer but I figured I would generalize it.
Cron will run the www.data items. You can verify this with this simple test:
$ sudo crontab -u www-data -e
Add this entry:
* * * * * date >> /tmp/date.out
Now examine the output with:
$ tail -f /tmp/date.out
After you have done that to ensure your crontab is working you can work the actual scripts you want to run.
the user www-data
while not have the same path as you, so lots of commands that work from your account will not work from the www-data
user unless you specifically set the path for that script. www-data's default path contains only: PATH=/usr/bin:/bin
You can do this by exporting your path list, then placing it in a path list for the script.
You can do this with:
$ echo $PATH > ~/mypath.txt
Now append the text of the ~/mypath.txt to the top of your script as:
Your script:
#!/bin/bash
export PATH=$PATH:/home/users/l/j/ljames/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
# rest of your script code here.
You can fine tune the path by removing some obvious items that you won't be using such as in the case of my example:
/home/users/l/j/ljames/bin
/usr/games
/usr/local/games
/snap/bin
That would leave the path line with of the script with:
#!/bin/bash
export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# ... rest of your script code here
PHP script run in cron
The php test script (/home/users/l/j/ljames/test.php
)
#!/usr/bin/php
<?php
$string=`date`;
print "Output from PHP script: $string";
?>
The php crontab entry
* * * * * php /home/users/l/j/ljames/test.php >> /tmp/date.out
You can examine the output with:
$ tail -f /tmp/date.out
Output from PHP script: Fri Feb 3 14:46:01 EST 2017
Output from PHP script: Fri Feb 3 14:47:01 EST 2017
Output from PHP script: Fri Feb 3 14:48:01 EST 2017
The output verifies that crontab will run your php scripts. It shows that the culprit is the actual script, which would have to be debugged to function properly outside of cron first. The likely culprit would the path list of the environment.
Note:
Your sytsem with your web server scripts run as user www-data
will be more secure than if they were run-as you or root. If they were as you, the script would have access to every thing that you have access to and if your web server were compromised it might even have root access with sudo
. Having your web server scripts run-as www-data
, a compromised server will only be able to access your server. Which, if backed up, would be easier to fix than to have to deal with your whole server.
Best Answer
Both
cron
andanacron
are daemons that can schedule execution of recurring tasks to a certain point in time defined by the user.The main difference between
cron
andanacron
is that the former assumes that the system is running continuously. If your system is off and you have a job scheduled during this time, the job never gets executed.On the other hand
anacron
is 'anachronistic' and is designed for systems that are not running 24x7. For it to workanacron
uses time-stamped files to find out when the last time its commands were executed. It also maintains a file/etc/anacrontab
just likecron
does. In addition,cron.daily
runs anacron every day. Hence,anacron
can only run a job once a day, butcron
can run as often as every minute.From
man anacrontab
:This means, if a task is scheduled to be run daily and the computer was turned off during that time, when anacron is run, it can see that the task was last run more than 24 hours ago and execute the task correctly.
For example if you specify the following in
/etc/anacrontab
:and on the day when the
script.sh
job is supposed to executed, if the system is not running,anacron
will execute thescript.sh
15 minutes after the system comes back up.Few Reference: