{"action":"opened","issue":{"url":"https://api.github.com/repos/saltstack/salt/issues/29492","labels_url":"https://api.github.com/repos/saltstack/salt/issues/29492/labels{/name}","comments_url":"https://api.github.com/repos/saltstack/salt/issues/29492/comments","events_url":"https://api.github.com/repos/saltstack/salt/issues/29492/events","html_url":"https://github.com/saltstack/salt/issues/29492","id":120850826,"number":29492,"title":"Some clarity on cmd.wait/watch vs. cmd.run/onchanges?","user":{"login":"jefferyharrell","id":8659114,"avatar_url":"https://avatars.githubusercontent.com/u/8659114?v=3","gravatar_id":"","url":"https://api.github.com/users/jefferyharrell","html_url":"https://github.com/jefferyharrell","followers_url":"https://api.github.com/users/jefferyharrell/followers","following_url":"https://api.github.com/users/jefferyharrell/following{/other_user}","gists_url":"https://api.github.com/users/jefferyharrell/gists{/gist_id}","starred_url":"https://api.github.com/users/jefferyharrell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jefferyharrell/subscriptions","organizations_url":"https://api.github.com/users/jefferyharrell/orgs","repos_url":"https://api.github.com/users/jefferyharrell/repos","events_url":"https://api.github.com/users/jefferyharrell/events{/privacy}","received_events_url":"https://api.github.com/users/jefferyharrell/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-12-07T19:40:38Z","updated_at":"2015-12-07T19:40:38Z","closed_at":null,"body":"The documentation on `cmd.run` vs. `cmd.wait` is a little confusing to me. Here's what [it says](https://docs.saltstack.com/en/latest/ref/states/all/salt.states.cmd.html#should-i-use-cmd-run-or-cmd-wait) right now:\r\n\r\n> The important thing to remember about them is that `cmd.run` states are run each time the SLS file that contains them is applied. If it is more desirable to have a command that only runs after some other state changes, then `cmd.wait` does just that. `cmd.wait` is designed to watch other states, and is executed when the state it is watching changes.\r\n\r\nThis seems to me to be only *kind of* true. In fact, I think I'm getting much more satisfactory results using `cmd.run` with `onchanges` than I get from using `cmd.wait` with `watch`. Am I badly misunderstanding the documentation?\r\n\r\nHere's what I mean: Say on your minion you have a file, and if (and only if) that file changes, a local command should be run. (In my case, the file is a FlexLM license file and the command is `lmreread` but I think that's unimportant here.) The trivial example looks like this:\r\n\r\n```salt\r\ntestfile1:\r\n  file.managed:\r\n    - name: /tmp/testfile1\r\n    - contents: \"Bring me the passengers. I want them alive.\"\r\n\r\ntestfile2:\r\n  cmd.wait:\r\n    - name: /bin/touch /tmp/testfile2\r\n    - watch:\r\n      - file: /tmp/testfile1\r\n```\r\n\r\n(I'm using `touch` here so I can easily tell whether the command has actually been run or not.) On first run, the results are exactly what you'd expect:\r\n\r\n```\r\nlocal:\r\n----------\r\n          ID: testfile1\r\n    Function: file.managed\r\n        Name: /tmp/testfile1\r\n      Result: True\r\n     Comment: File /tmp/testfile1 updated\r\n     Started: 13:30:09.592725\r\n    Duration: 10.936 ms\r\n     Changes:   \r\n              ----------\r\n              diff:\r\n                  New file\r\n----------\r\n          ID: testfile2\r\n    Function: cmd.wait\r\n        Name: /bin/touch /tmp/testfile2\r\n      Result: True\r\n     Comment: Command \"/bin/touch /tmp/testfile2\" run\r\n     Started: 13:30:09.606103\r\n    Duration: 9.692 ms\r\n     Changes:   \r\n              ----------\r\n              pid:\r\n                  28544\r\n              retcode:\r\n                  0\r\n              stderr:\r\n              stdout:\r\n\r\nSummary for local\r\n------------\r\nSucceeded: 2 (changed=2)\r\nFailed:    0\r\n------------\r\nTotal states run:     2\r\nTotal run time:  20.628 ms\r\n```\r\n\r\nBoth `testfile1` and `testfile2` are created. If I remove `testfile2` and run the state again, then no `testfile2` appears … but I get this confusing output:\r\n\r\n```\r\nlocal:\r\n----------\r\n          ID: testfile1\r\n    Function: file.managed\r\n        Name: /tmp/testfile1\r\n      Result: True\r\n     Comment: File /tmp/testfile1 is in the correct state\r\n     Started: 13:31:06.431885\r\n    Duration: 10.742 ms\r\n     Changes:   \r\n----------\r\n          ID: testfile2\r\n    Function: cmd.wait\r\n        Name: /bin/touch /tmp/testfile2\r\n      Result: True\r\n     Comment: \r\n     Started: 13:31:06.443919\r\n    Duration: 1.047 ms\r\n     Changes:   \r\n\r\nSummary for local\r\n------------\r\nSucceeded: 2\r\nFailed:    0\r\n------------\r\nTotal states run:     2\r\nTotal run time:  11.789 ms\r\n```\r\n\r\nThere's not much there to indicate that `/bin/touch /tmp/testfile2` didn't actually execute on the minion. Comparing run #1 to run #2 I can see the difference, but you have to know what to look for, you know?\r\n\r\nOn the other hand, if I construct the state like this…\r\n\r\n```salt\r\ntestfile1:\r\n  file.managed:\r\n    - name: /tmp/testfile1\r\n    - contents: \"Bring me the passengers. I want them alive.\"\r\n\r\ntestfile2:\r\n  cmd.run:\r\n    - name: /bin/touch /tmp/testfile2\r\n    - onchanges:\r\n      - file: /tmp/testfile1\r\n```\r\n\r\n…then on first run I get this…\r\n\r\n```\r\nlocal:\r\n----------\r\n          ID: testfile1\r\n    Function: file.managed\r\n        Name: /tmp/testfile1\r\n      Result: True\r\n     Comment: File /tmp/testfile1 updated\r\n     Started: 13:33:45.287159\r\n    Duration: 11.936 ms\r\n     Changes:   \r\n              ----------\r\n              diff:\r\n                  New file\r\n----------\r\n          ID: testfile2\r\n    Function: cmd.run\r\n        Name: /bin/touch /tmp/testfile2\r\n      Result: True\r\n     Comment: Command \"/bin/touch /tmp/testfile2\" run\r\n     Started: 13:33:45.300506\r\n    Duration: 9.3 ms\r\n     Changes:   \r\n              ----------\r\n              pid:\r\n                  29452\r\n              retcode:\r\n                  0\r\n              stderr:\r\n              stdout:\r\n\r\nSummary for local\r\n------------\r\nSucceeded: 2 (changed=2)\r\nFailed:    0\r\n------------\r\nTotal states run:     2\r\nTotal run time:  21.236 ms\r\n```\r\n\r\n…and on second run I get this…\r\n\r\n```\r\nlocal:\r\n----------\r\n          ID: testfile1\r\n    Function: file.managed\r\n        Name: /tmp/testfile1\r\n      Result: True\r\n     Comment: File /tmp/testfile1 is in the correct state\r\n     Started: 13:34:19.418901\r\n    Duration: 10.884 ms\r\n     Changes:   \r\n----------\r\n          ID: testfile2\r\n    Function: cmd.run\r\n        Name: /bin/touch /tmp/testfile2\r\n      Result: True\r\n     Comment: State was not run because none of the onchanges reqs changed\r\n     Started: \r\n    Duration: \r\n     Changes:   \r\n\r\nSummary for local\r\n------------\r\nSucceeded: 2\r\nFailed:    0\r\n------------\r\nTotal states run:     2\r\nTotal run time:  10.884 ms\r\n```\r\n\r\n…which is *much* more useful output, at least for me. That tells me specifically that the command did not execute, and it tells me why.\r\n\r\nBut my question is, since the documentation seems so specifically to say that I should be using `cmd.wait` here instead of `cmd.run` … am I shooting myself in the foot?\r\n\r\nThanks."}}