cloudinit how to schedule a periodic task while bootstrapping a vm
Asked Answered
B

2

8

While creating vm using Nova boot I am supplying it a user-data script (cloud-init script).

what I am wondering is, how can I specify in that cloud init script( or any other way to do it) :- to schedule a cron job to run every 2 hours

lets say I want to run "du -s njain/ " to find the size of my directory every 2 hours

I know "runcmd" can be used to do it once..but how do i make it run periodically ? du -s ../njain/

Breannabreanne answered 17/2, 2015 at 21:0 Comment(0)
S
31

it might be too late now, but the right way to do it through cloud-init is just to use the cloud-config write_files module and create a cron entry file in /etc/cron.d

for your example, the cloud-init userdata would look something like:

#cloud-config 

write_files:
  - owner: root:root
    path: /etc/cron.d/your_cronjob
    content: * */2 * * * [USER] du -s njain/

Note: you must replace [USER] with the user you'd like to run the cronjob as.

Sphygmomanometer answered 31/5, 2016 at 8:18 Comment(1)
Note that cron files must contain a trailing newline, otherwise you'll get an error: content: "* */2 * * * [USER] du -s njain/\n"Yamada
B
-14

rather than using cloud-init I used crontab, crontab can be used to schedule tasks periodically. Since i was not sure how to use cloud init to do this...I ended up making a image template with the crontab below

sudo crontab -e 
Breannabreanne answered 1/3, 2015 at 7:18 Comment(1)
The question is how to create a crontab via cloud-init not how to create a crontab.Mandatory

© 2022 - 2024 — McMap. All rights reserved.