pau*_*llb 6 cron vagrant vagrantfile
如何配置Vagrant配置,以便在配置计算机时自动配置其crontab?(根据Chef(?)文件配置vagrant)
举个例子,我想设置以下cron:
5 * * * * curl http://www.google.com
Run Code Online (Sandbox Code Playgroud)
没有Chef/Puppet/Ansible而是使用shell,可以很容易地完成这样的事情的基本配置.
该流浪文档覆盖这个基本配置相当不错了具有盒从boostrap.sh下载Apache的例子.
同样,您可以按照相同的步骤编辑Vagrantfile,以便在配置时调用bootstrap.sh文件:
Vagrant.configure("2") do |config|
...
config.vm.provision :shell, path: "bootstrap.sh"
...
end
Run Code Online (Sandbox Code Playgroud)
然后,您可以在与Vagrantfile相同的目录中创建一个bootstrap.sh文件,其中包含以下内容:
#!/bin/bash
# Adds a crontab entry to curl google.com every hour on the 5th minute
# Cron expression
cron="5 * * * * curl http://www.google.com"
# ? ? ? ? ?
# ? ? ? ? ?
# ? ? ? ? ?????? day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# ? ? ? ??????????? month (1 - 12)
# ? ? ???????????????? day of month (1 - 31)
# ? ????????????????????? hour (0 - 23)
# ?????????????????????????? min (0 - 59)
# Escape all the asterisks so we can grep for it
cron_escaped=$(echo "$cron" | sed s/\*/\\\\*/g)
# Check if cron job already in crontab
crontab -l | grep "${cron_escaped}"
if [[ $? -eq 0 ]] ;
then
echo "Crontab already exists. Exiting..."
exit
else
# Write out current crontab into temp file
crontab -l > mycron
# Append new cron into cron file
echo "$cron" >> mycron
# Install new cron file
crontab mycron
# Remove temp file
rm mycron
fi
Run Code Online (Sandbox Code Playgroud)
默认情况下,Vagrant配置程序以root身份运行,因此这将向root用户的crontab附加一个cron作业,假设它尚不存在.如果要将其添加到vagrant用户的crontab,则需要运行配置程序,并将privileged
标志设置为false
:
config.vm.provision :shell, path: "bootstrap.sh", privileged: false
Run Code Online (Sandbox Code Playgroud)