Saltstack 简明教程
SaltStack - Using Cron with Salt
Salt 可与 Cron 应用程序一起使用。同时使用这两个应用程序可为 Salt 的自动化提供极好的机会。Salt 提供了远程执行命令的选项,而 Cron 允许以预先调度或自动的方式运行。让我们在本章中学习如何同时使用 Cron 和 Salt。
Salt can be used along with the Cron application. Using both applications together provides a great opportunity to automate Salt. While Salt provides an option to execute commands remotely, Cron enables it to run in a pre-scheduled or automated manner. Let us learn how to use Cron and Salt together in this chapter.
What is Cron?
Cron 是 Linux 环境中非常有用的应用程序。它允许预设一个命令或脚本,以便在特定日期和时间运行。它还允许以规则的时间间隔运行一个应用程序,比如每天、每周或每个月的第一天。
Cron is very useful application in the Linux Environment. It enables to preset a command or script to run in a specific date and time. It also enables to run an application in a regular interval, say daily, weekly or every first day of the month.
Cron 在系统启动时启动,并检查 /etc/crontab 文件以获取配置详细信息。/etc/crontab 在单独的行中列示每个应用程序及其时间表,如下所示。
Cron starts when the system starts and check the /etc/crontab file for configuration details. The /etc/crontab has every application and its schedule in a separate line as shown below.
15 * * * * root echo "This command runs at 15 minutes past every hour"
15 10 * * * root echo "This command is run daily at 10:15 am"
每行都有以下七个入口点,它们以空格分隔,如下所示:
Every line has the following seven entry points, which are separated by space and they are as follows −
-
minute − minute of the hour and is between ‘0’ and ‘59’.
-
hour − hour and is specified in the 24-hour clock.
-
day_of_month − Day of the Month and is between 1 and 31. For example, the 10th of each month is 10.
-
month − A month specified and is specified numerically (0-12), or as the name of the month (e.g. May).
-
day_of_week − Day of the week is specified numerically (0-7) or as the name of the day (e.g. Sun).
-
user − User account under which the command runs.
-
cmd − The actual command and its arguments.
在这里,如果没有分配任何内容,* 会替换。
Here, * replaces, if nothing is assigned.
Salt Caller (salt-call)
Salt 提供了一个 CLI (命令行接口) salt-call 来运行本地 minion 系统本身中的模块,而不是使用 salt 命令从主服务器运行。salt call CLI 支持 salt 命令支持的所有选项,但仅在本地运行。
Salt provides a CLI (Command Line Interface), salt-call to run the modules in the local minion system itself instead of from the master server using the salt command. The salt call CLI supports all the options supported by salt command, but run locally.
Salt caller 最初设计为支持调试,但现在可将其用作独立应用程序。
Salt Caller was initially designed to support debugging, but now, it can be used as a standalone application.
salt-call test.ping
Using salt-call in cron
salt-call CLI 可用于使用 Cron 安排 Salt 操作。例如,要在每天午夜检查 minion 的状态,我们可以使用 salt-call 以及选项 --state.apply,如下所示。
The salt-call CLI is useful to schedule salt operation using Cron. For example, to check the state of the minion every day at midnight, we can use salt-call along with the option – state.apply as shown below.
/etc/crontab
/etc/crontab
PATH = /bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/bin
0 0 * * * salt-call state.apply
在此,
Here,
-
The state.apply function will check the salt configuration file for the minion and check whether all action defined for the minion is properly configured.
-
Setting the path is a good practice because sometimes the salt command may not be available in the system path.
在下一章中,我们将学习远程执行,这是 Salt 的一个核心概念。
In the next chapter, we will learn Remote Execution, which is a core concept of Salt.