Background/delayed execution of tasks?

888 views
Skip to first unread message

Steve Bennett

unread,
Feb 13, 2014, 9:39:08 PM2/13/14
to salt-...@googlegroups.com
Hi all,
  At the end of my deployment process is a long step of downloading a dataset and loading it into a database. It takes around 30 minutes. I don't really want it on the main execution path of the salt deployment, because I don't seem to get any feedback on how the rest of the deployment went until it's all done.

As others have noticed, simply including a "./process.sh &" type shell command doesn't work.

I see the issue was addressed 2 years ago, but I don't really understand the resolution: https://github.com/saltstack/salt/issues/1402

Similarly, UtahDave developed a "custom module" (https://github.com/saltstack/salt/issues/6691) but I don't know how to apply it - or if that's really the best solution.

Is there some other way to achieve this? (Open to creative workarounds too...)

Thanks,
Steve

Mike Place

unread,
Feb 13, 2014, 9:45:29 PM2/13/14
to salt-...@googlegroups.com
Take a look at the 'at' execution module: http://docs.saltstack.com/ref/modules/all/salt.modules.at.html#module-salt.modules.at

Obviously, you wouldn't get Salt to return the data for you but it's one fairly bombproof technique to fire off set-it-and-forget-it commands at a point in time in the future.

-mp




--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

David Anderson

unread,
Feb 13, 2014, 10:13:53 PM2/13/14
to salt-...@googlegroups.com
Hi Steve,

Have you made sure you are redirecting the command's stdout/stderr?

(salt-dev)root@web1:/# cat /tmp/sleep.sh
#!/bin/sh

sleep 30
echo awake


(salt-dev)root@salt:/srv/salt/base# cat sleeper.sls
run_cmd:
cmd.run:
- name: '/tmp/sleep.sh &'


(salt-dev)root@web1:/# time salt-call state.sls sleeper
local:
----------
ID: run_cmd
Function: cmd.run
Name: /tmp/sleep.sh &
Result: True
Comment: Command "/tmp/sleep.sh &" run
Changes:
----------
pid:
8562
retcode:
0
stderr:

stdout:
awake

Summary
------------
Succeeded: 1
Failed: 0
------------
Total: 1

real 0m32.514s
user 0m0.918s
sys 0m0.202s




If you make sure you redirect stdout/stderr, you should be able to
background the process:

(salt-dev)root@salt:/srv/salt/base# cat sleeper.sls
run_cmd:
cmd.run:
- name: '/tmp/sleep.sh >/dev/null 2>&1 &'


(salt-dev)root@web1:/# time salt-call state.sls sleeper
local:
----------
ID: run_cmd
Function: cmd.run
Name: /tmp/sleep.sh >/dev/null 2>&1 &
Result: True
Comment: Command "/tmp/sleep.sh >/dev/null 2>&1 &" run
Changes:
----------
pid:
8634
retcode:
0
stderr:

stdout:


Summary
------------
Succeeded: 1
Failed: 0
------------
Total: 1

real 0m2.362s
user 0m0.928s
sys 0m0.214s


(salt-dev)root@web1:/# ps wwauxf | grep sleep
root 8635 0.0 0.0 9396 1296 pts/0 S 04:07 0:00 /bin/sh /tmp/sleep.sh
root 8638 0.0 0.0 4048 540 pts/0 S 04:07 0:00 \_ sleep 30


--
Dave

Steve Bennett

unread,
Feb 13, 2014, 10:50:14 PM2/13/14
to salt-...@googlegroups.com
On Fri, Feb 14, 2014 at 2:13 PM, David Anderson <da...@dubkat.com> wrote:
Hi Steve,

Have you made sure you are redirecting the command's stdout/stderr?


Thanks, will try. What's the logic behind that rule? 

I've actually been using nohup, which redirects stdout into nohup.out - but maybe I need to redirect it anyway. Will also have a look at the at module.

Steve

Dmitry Golubenko

unread,
Feb 13, 2014, 11:51:56 PM2/13/14
to salt-...@googlegroups.com
В Чтв, 13/02/2014 в 18:39 -0800, Steve Bennett пишет:
> Hi all,
> At the end of my deployment process is a long step of downloading a
> dataset and loading it into a database. It takes around 30 minutes. I
> don't really want it on the main execution path of the salt
> deployment, because I don't seem to get any feedback on how the rest
> of the deployment went until it's all done.
>
>
> As others have noticed, simply including a "./process.sh &" type shell
> command doesn't work.
you can use `at' daemon as already suggested or just wrap ./process in
`screen' like:
screen -d -m ./process.sh
this does not requires at daemon running. as a bonus it gives you
possibility to attach later and debug if somthing goes wrong ;-)




Steve Bennett

unread,
Feb 14, 2014, 12:07:29 AM2/14/14
to salt-users
Thanks, using 'at' worked in the end. I'm just calling it directly from the command line for simplicity. Pity you can't delay by less than 1 minute.

- name: echo './import.sh && ./process.sh' | at now +1 minute

I did try redirect output from nohup, but it still seemed to hold up the whole process. I've been having a few issues with minions getting stuck, so it's possible my experiment wasn't clean. It's hard to do clean experiments when you don't understand the tools very well!

Steve


--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/NVsz_oFLzgI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages