1
0

index.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. .. _jobs:
  2. =======================
  3. :index:`Job Management`
  4. =======================
  5. .. versionadded:: 0.9.7
  6. Since Salt executes jobs running on many systems, Salt needs to be able to
  7. manage jobs running on many systems.
  8. The :index:`Minion proc System`
  9. ===============================
  10. Salt Minions maintain a *proc* directory in the Salt ``cachedir``. The *proc*
  11. directory maintains files named after the executed job ID. These files contain
  12. the information about the current running jobs on the minion and allow for
  13. jobs to be looked up. This is located in the *proc* directory under the
  14. cachedir, with a default configuration it is under ``/var/cache/salt/{master|minion}/proc``.
  15. Functions in the saltutil Module
  16. ================================
  17. Salt 0.9.7 introduced a few new functions to the
  18. :mod:`saltutil<salt.modules.saltutil>` module for managing
  19. jobs. These functions are:
  20. 1. ``running``
  21. Returns the data of all running jobs that are found in the *proc* directory.
  22. 2. ``find_job``
  23. Returns specific data about a certain job based on job id.
  24. 3. ``signal_job``
  25. Allows for a given jid to be sent a signal.
  26. 4. ``term_job``
  27. Sends a termination signal (SIGTERM, 15) to the process controlling the
  28. specified job.
  29. 5. ``kill_job``
  30. Sends a kill signal (SIGKILL, 9) to the process controlling the
  31. specified job.
  32. These functions make up the core of the back end used to manage jobs at the
  33. minion level.
  34. The jobs Runner
  35. ===============
  36. A convenience runner front end and reporting system has been added as well.
  37. The jobs runner contains functions to make viewing data easier and cleaner.
  38. The jobs runner contains a number of functions...
  39. active
  40. ------
  41. The active function runs saltutil.running on all minions and formats the
  42. return data about all running jobs in a much more usable and compact format.
  43. The active function will also compare jobs that have returned and jobs that
  44. are still running, making it easier to see what systems have completed a job
  45. and what systems are still being waited on.
  46. .. code-block:: bash
  47. # salt-run jobs.active
  48. lookup_jid
  49. ----------
  50. When jobs are executed the return data is sent back to the master and cached.
  51. By default it is cached for 24 hours, but this can be configured via the
  52. ``keep_jobs`` option in the master configuration.
  53. Using the lookup_jid runner will display the same return data that the initial
  54. job invocation with the salt command would display.
  55. .. code-block:: bash
  56. # salt-run jobs.lookup_jid <job id number>
  57. list_jobs
  58. ---------
  59. Before finding a historic job, it may be required to find the job id. ``list_jobs``
  60. will parse the cached execution data and display all of the job data for jobs
  61. that have already, or partially returned.
  62. .. code-block:: bash
  63. # salt-run jobs.list_jobs
  64. .. _scheduling-jobs:
  65. Scheduling Jobs
  66. ===============
  67. Salt's scheduling system allows incremental executions on minions or the
  68. master. The schedule system exposes the execution of any execution function on
  69. minions or any runner on the master.
  70. Scheduling can be enabled by multiple methods:
  71. - ``schedule`` option in either the master or minion config files. These
  72. require the master or minion application to be restarted in order for the
  73. schedule to be implemented.
  74. - Minion pillar data. Schedule is implemented by refreshing the minion's pillar data,
  75. for example by using ``saltutil.refresh_pillar``.
  76. - The :mod:`schedule state<salt.states.schedule>` or
  77. :mod:`schedule module<salt.modules.schedule>`
  78. .. note::
  79. The scheduler executes different functions on the master and minions. When
  80. running on the master the functions reference runner functions, when
  81. running on the minion the functions specify execution functions.
  82. A scheduled run has no output on the minion unless the config is set to info level
  83. or higher. Refer to :conf_minion:`minion-logging-settings`.
  84. States are executed on the minion, as all states are. You can pass positional
  85. arguments and provide a YAML dict of named arguments.
  86. .. code-block:: yaml
  87. schedule:
  88. job1:
  89. function: state.sls
  90. seconds: 3600
  91. args:
  92. - httpd
  93. kwargs:
  94. test: True
  95. This will schedule the command: ``state.sls httpd test=True`` every 3600 seconds
  96. (every hour).
  97. .. code-block:: yaml
  98. schedule:
  99. job1:
  100. function: state.sls
  101. seconds: 3600
  102. args:
  103. - httpd
  104. kwargs:
  105. test: True
  106. splay: 15
  107. This will schedule the command: ``state.sls httpd test=True`` every 3600 seconds
  108. (every hour) splaying the time between 0 and 15 seconds.
  109. .. code-block:: yaml
  110. schedule:
  111. job1:
  112. function: state.sls
  113. seconds: 3600
  114. args:
  115. - httpd
  116. kwargs:
  117. test: True
  118. splay:
  119. start: 10
  120. end: 15
  121. This will schedule the command: ``state.sls httpd test=True`` every 3600 seconds
  122. (every hour) splaying the time between 10 and 15 seconds.
  123. Schedule by Date and Time
  124. -------------------------
  125. .. versionadded:: 2014.7.0
  126. Frequency of jobs can also be specified using date strings supported by
  127. the Python ``dateutil`` library. This requires the Python ``dateutil`` library
  128. to be installed.
  129. .. code-block:: yaml
  130. schedule:
  131. job1:
  132. function: state.sls
  133. args:
  134. - httpd
  135. kwargs:
  136. test: True
  137. when: 5:00pm
  138. This will schedule the command: ``state.sls httpd test=True`` at 5:00 PM minion
  139. localtime.
  140. .. code-block:: yaml
  141. schedule:
  142. job1:
  143. function: state.sls
  144. args:
  145. - httpd
  146. kwargs:
  147. test: True
  148. when:
  149. - Monday 5:00pm
  150. - Tuesday 3:00pm
  151. - Wednesday 5:00pm
  152. - Thursday 3:00pm
  153. - Friday 5:00pm
  154. This will schedule the command: ``state.sls httpd test=True`` at 5:00 PM on
  155. Monday, Wednesday and Friday, and 3:00 PM on Tuesday and Thursday.
  156. .. code-block:: yaml
  157. schedule:
  158. job1:
  159. function: state.sls
  160. args:
  161. - httpd
  162. kwargs:
  163. test: True
  164. when:
  165. - 'tea time'
  166. .. code-block:: yaml
  167. whens:
  168. tea time: 1:40pm
  169. deployment time: Friday 5:00pm
  170. The Salt scheduler also allows custom phrases to be used for the `when`
  171. parameter. These `whens` can be stored as either pillar values or
  172. grain values.
  173. .. code-block:: yaml
  174. schedule:
  175. job1:
  176. function: state.sls
  177. seconds: 3600
  178. args:
  179. - httpd
  180. kwargs:
  181. test: True
  182. range:
  183. start: 8:00am
  184. end: 5:00pm
  185. This will schedule the command: ``state.sls httpd test=True`` every 3600 seconds
  186. (every hour) between the hours of 8:00 AM and 5:00 PM. The range parameter must
  187. be a dictionary with the date strings using the ``dateutil`` format.
  188. .. code-block:: yaml
  189. schedule:
  190. job1:
  191. function: state.sls
  192. seconds: 3600
  193. args:
  194. - httpd
  195. kwargs:
  196. test: True
  197. range:
  198. invert: True
  199. start: 8:00am
  200. end: 5:00pm
  201. Using the invert option for range, this will schedule the command
  202. ``state.sls httpd test=True`` every 3600 seconds (every hour) until the current
  203. time is between the hours of 8:00 AM and 5:00 PM. The range parameter must be
  204. a dictionary with the date strings using the ``dateutil`` format.
  205. .. code-block:: yaml
  206. schedule:
  207. job1:
  208. function: pkg.install
  209. kwargs:
  210. pkgs: [{'bar': '>1.2.3'}]
  211. refresh: true
  212. once: '2016-01-07T14:30:00'
  213. This will schedule the function ``pkg.install`` to be executed once at the
  214. specified time. The schedule entry ``job1`` will not be removed after the job
  215. completes, therefore use ``schedule.delete`` to manually remove it afterwards.
  216. The default date format is ISO 8601 but can be overridden by also specifying the
  217. ``once_fmt`` option, like this:
  218. .. code-block:: yaml
  219. schedule:
  220. job1:
  221. function: test.ping
  222. once: 2015-04-22T20:21:00
  223. once_fmt: '%Y-%m-%dT%H:%M:%S'
  224. Maximum Parallel Jobs Running
  225. -----------------------------
  226. .. versionadded:: 2014.7.0
  227. The scheduler also supports ensuring that there are no more than N copies of
  228. a particular routine running. Use this for jobs that may be long-running
  229. and could step on each other or pile up in case of infrastructure outage.
  230. The default for ``maxrunning`` is 1.
  231. .. code-block:: yaml
  232. schedule:
  233. long_running_job:
  234. function: big_file_transfer
  235. jid_include: True
  236. maxrunning: 1
  237. Cron-like Schedule
  238. ------------------
  239. .. versionadded:: 2014.7.0
  240. .. code-block:: yaml
  241. schedule:
  242. job1:
  243. function: state.sls
  244. cron: '*/15 * * * *'
  245. args:
  246. - httpd
  247. kwargs:
  248. test: True
  249. The scheduler also supports scheduling jobs using a cron like format.
  250. This requires the Python ``croniter`` library.
  251. Job Data Return
  252. ---------------
  253. .. versionadded:: 2015.5.0
  254. By default, data about jobs runs from the Salt scheduler is returned to the
  255. master. Setting the ``return_job`` parameter to False will prevent the data
  256. from being sent back to the Salt master.
  257. .. code-block:: yaml
  258. schedule:
  259. job1:
  260. function: scheduled_job_function
  261. return_job: False
  262. Job Metadata
  263. ------------
  264. .. versionadded:: 2015.5.0
  265. It can be useful to include specific data to differentiate a job from other
  266. jobs. Using the metadata parameter special values can be associated with
  267. a scheduled job. These values are not used in the execution of the job,
  268. but can be used to search for specific jobs later if combined with the
  269. ``return_job`` parameter. The metadata parameter must be specified as a
  270. dictionary, othewise it will be ignored.
  271. .. code-block:: yaml
  272. schedule:
  273. job1:
  274. function: scheduled_job_function
  275. metadata:
  276. foo: bar
  277. Run on Start
  278. ------------
  279. .. versionadded:: 2015.5.0
  280. By default, any job scheduled based on the startup time of the minion will run
  281. the scheduled job when the minion starts up. Sometimes this is not the desired
  282. situation. Using the ``run_on_start`` parameter set to ``False`` will cause the
  283. scheduler to skip this first run and wait until the next scheduled run:
  284. .. code-block:: yaml
  285. schedule:
  286. job1:
  287. function: state.sls
  288. seconds: 3600
  289. run_on_start: False
  290. args:
  291. - httpd
  292. kwargs:
  293. test: True
  294. Until and After
  295. ---------------
  296. .. versionadded:: 2015.8.0
  297. .. code-block:: yaml
  298. schedule:
  299. job1:
  300. function: state.sls
  301. seconds: 15
  302. until: '12/31/2015 11:59pm'
  303. args:
  304. - httpd
  305. kwargs:
  306. test: True
  307. Using the until argument, the Salt scheduler allows you to specify
  308. an end time for a scheduled job. If this argument is specified, jobs
  309. will not run once the specified time has passed. Time should be specified
  310. in a format supported by the ``dateutil`` library.
  311. This requires the Python ``dateutil`` library to be installed.
  312. .. versionadded:: 2015.8.0
  313. .. code-block:: yaml
  314. schedule:
  315. job1:
  316. function: state.sls
  317. seconds: 15
  318. after: '12/31/2015 11:59pm'
  319. args:
  320. - httpd
  321. kwargs:
  322. test: True
  323. Using the after argument, the Salt scheduler allows you to specify
  324. an start time for a scheduled job. If this argument is specified, jobs
  325. will not run until the specified time has passed. Time should be specified
  326. in a format supported by the ``dateutil`` library.
  327. This requires the Python ``dateutil`` library to be installed.
  328. Scheduling States
  329. -----------------
  330. .. code-block:: yaml
  331. schedule:
  332. log-loadavg:
  333. function: cmd.run
  334. seconds: 3660
  335. args:
  336. - 'logger -t salt < /proc/loadavg'
  337. kwargs:
  338. stateful: False
  339. shell: /bin/sh
  340. Scheduling Highstates
  341. ---------------------
  342. To set up a highstate to run on a minion every 60 minutes set this in the
  343. minion config or pillar:
  344. .. code-block:: yaml
  345. schedule:
  346. highstate:
  347. function: state.highstate
  348. minutes: 60
  349. Time intervals can be specified as seconds, minutes, hours, or days.
  350. Scheduling Runners
  351. ------------------
  352. Runner executions can also be specified on the master within the master
  353. configuration file:
  354. .. code-block:: yaml
  355. schedule:
  356. run_my_orch:
  357. function: state.orchestrate
  358. hours: 6
  359. splay: 600
  360. args:
  361. - orchestration.my_orch
  362. The above configuration is analogous to running
  363. ``salt-run state.orch orchestration.my_orch`` every 6 hours.
  364. Scheduler With Returner
  365. -----------------------
  366. The scheduler is also useful for tasks like gathering monitoring data about
  367. a minion, this schedule option will gather status data and send it to a MySQL
  368. returner database:
  369. .. code-block:: yaml
  370. schedule:
  371. uptime:
  372. function: status.uptime
  373. seconds: 60
  374. returner: mysql
  375. meminfo:
  376. function: status.meminfo
  377. minutes: 5
  378. returner: mysql
  379. Since specifying the returner repeatedly can be tiresome, the
  380. ``schedule_returner`` option is available to specify one or a list of global
  381. returners to be used by the minions when scheduling.