-
-
Notifications
You must be signed in to change notification settings - Fork 1
HOWTO devops crontab
The following document talks about crontab
and the custom implementation of the log:log_rotate
task, which
works similarly to the System's logrotate
but with fewer and simpler options.
Alternatively, check out this HOW-TO manage logfiles with logrotate,
which talks exclusively about the System's logrotate
.
How to install a list of Rake tasks as recurrent maintenance cron jobs.
With a small Bash script it is possible to filter-out any password-bearing text so that the cron job itself can send notifications of the resulting output by plain mail.
Typically, on predefined time intervals we launch either a script or a specific rake task to perform daily backups or other recurrent jobs, like log rotation.
From the bash console:
$> man crontab
To get the correct fullpath to launch rake
or bundle
with a command execution by crontab
, use:
$> which rake
/usr/share/rvm/gems/GEMSET_NAME/bin/rake
$> which bundle
/usr/share/rvm/gems/GEMSET_NAME/bin/bundle
(Where GEMSET_NAME will be the name of the Gemset you're using with RVM) To edit the crontab, log to the server w/ ssh and use:
$> crontab -e
(It opens vim)
A typical command line example (that has to be executed by crontab):
$> cd /var/www/b1cus.com/current && /usr/share/rvm/gems/GEMSET_NAME/bin/bundle exec rake RAILS_ENV=production sql:dump
Make sure that the paths used to reach bundle
& rake
are the ones returned by the which
command above (we need to replicate the same context).
To load the bash environment (otherwise the bundle exec command won't work when running as root) use:
$> /bin/bash -l -c "<SHELL_COMMAND_TO_BE_RUN_WITH_PARAMETERS>"
The Cronjob timing is based upon:
minute hour day month weekday
15 * * * *
=> run the 15th minute of every hour of every day, for every month, every weekday
00,15,30 * * * *
=> as above, but on the 0th, 15th and 30th minute of every hour
A sample entry that will run a simple rake task (as a one-liner, with no password protection):
MAILTO=<MAILTO_ADDRESS_FOR_NOTIFICATIONS>
# Main app maintenance:
00 2 * * * /bin/bash -l -c "cd /var/www/<SITE_NAME>/current && /usr/share/rvm/gems/<GEMSET_NAME>/bin/bundle exec rake RAILS_ENV=production sql:dump"
Log in as root
(or with a user able to scale to the correct root privileges) on the remote server:
$> ssh root@<remote_server>
Create a file naming it, for example, /root/crontab_script
. The file will be the script executed by cron.
$> vi /root/crontab_script
Inside the script, we'll be using grep to get the passwords from the database configuration and we'll strip them out from any captured console output.
We shall be able to send via email the resulting text safely, without any sensitive information released in clear.
----8<----
[/root/crontab_script
]
#!/bin/bash
# Launch a backup task (for example), or anything else:
cd <PATH_TO_DEPLOY_ROOT>/current
PSSWD=$(grep -m 1 -Po "(?<=password\:\s)(.+)" <PATH_TO_DEPLOY_ROOT>/current/config/database.yml)
<WHICH_bundle_RESULT> exec rake RAILS_ENV=production <TASK_NAMES_LIST> 2>&1 | sed "s/$PSSWD/***/g"
# Un-set variable:
unset PSSWD
# If you're using monit:
monit status
----8<----
Typically, <PATH_TO_DEPLOY_ROOT>
is /var/www/<SITE_NAME>/current
when deploying with Capistrano.
current
is the symlink to the current deploy version folder.
Check out the notes in the previous section for an explanation on how to get the exact path for <WHICH_bundle_RESULT>
.
$> chmod +x crontab_script
Edit crontab
in order to launch the script, and add also a mail notification, plus anything else may be required.
(Such as, for instance, the Network Time Protocol update to keep the internal clock in synch with the rest of the world.)
$> crontab -e
Do the edits using the example with the MAILTO
field as shown in the previous section, customize it running the /root/crontab_script
instead (created previously) and save:
MAILTO=<MAILTO_ADDRESS_FOR_NOTIFICATIONS>
# Main app maintenance:
00 2 * * * /bin/bash -l /root/crontab_script
# Adjust ntpdate:
0 0 * * * /etc/network/ip-up.d/ntpdate
$> service cron reload
Done!