Add cronjob to crontab
Follow this one: http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
Here is a good Cron Job tutorial: How to Create a Cron Job (Scheduled Task) for Your Website or Blog
Use below command to create/edit crontab:
Use below command to list crontab:
Check if cronjob is running
After add a crontab, if it is not working. You can check the log, in my server, it is like this:
Ubuntu may has different log location:
You can see just cron jobs in that logfile by running
grep CRON /var/log/syslog
Different server may have different path. Check this answer to find more: http://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it
If there is no MTA installed, cron just throws the job output away. The above log will only record the cron running history.
Also, check log you may will see this error:
CRON: (CRON) info (No MTA installed, discarding output)
Linux uses mail for sending notifications to the user. Most Linux distributions have an mail service (including an MTA) installed. Ubuntu doesn’t though. You can install a mail service, postfix for example, to solve this problem.
sudo apt-get install postfix
Check more: http://askubuntu.com/questions/222512/cron-info-no-mta-installed-discarding-output-error-in-the-syslog
Or you can not send mail by add
2>&1 to not send mail like below. Log them into log file.
Below cron job will redirect all standard output and errors that may be produced by the script that is run to the log file specified:
01 14 * * * /home/joe/myscript >> /home/log/myscript.log 2>&1
Drush and cronjob
If you are using Drupal, you may need Drush. Using drush in the cronjob is same as other script. But you may will get “Can’t run drush from cron” error.
My simple solution is like this:
0 11 * * * cd /var/www/html;/home/ubuntu/.composer/vendor/bin/drush mydrush >> /var/log/mycron.log 2>&1
Replace “/var/www/html” to your Drupal site root path.
Replace “/home/ubuntu/.composer/vendor/bin” to your Drush install location.
Create any log file to replace “/var/log/mycron.log”.
For the reason and other solutions, check following links: