Legomancer

Talk Cron

Jan 18th, 2019
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. Talk Cron
  2. [unedited notes dump. later Ill do properly with pictures]
  3.  
  4.  
  5. This is a v v brief introduction to cron. It's intended for an audience that has ever opened Terminal, run a command line command or written a shell script.
  6. Please note, this info may not be relevant to PCs because CmdPrompt is different. Blame Microsoft.
  7. .
  8. Cron is a venerable old program from the Unix days so there's guidance available online
  9. https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
  10. Or more authentic guide from 1999
  11. http://www.unixgeeks.org/security/newbie/unix/cron-1.html
  12. Or the man page for truly studious people
  13. `man crontab`
  14. Cron is a can way to schedule commands or scripts. and you set the interval e.g on 9th hr daily.
  15. Cron's “arguments” for specifying time intervals has odd syntax but luckily there are diagrams.
  16. [Img]
  17. There are 5 ‘slots’ separate by spaces.
  18. Some shell syntax applies here. * to “glob” means “every” like every day. When all 5 slots are * it means every minute. A 0 then * means every hour but no minutes.
  19. A range of numbers is also doable.
  20. And list numbers with comma, so long as no spaces.
  21. .
  22. Cron's way of setting up ‘jobs’ is also odd. You are practically editing sys's config files directly. You can even dip into /usr/sbin and find them.
  23. [Alt] Along with these are folders for “weekly”, just stick a script here if u don't need granular ‘5-slot’ precision.
  24. With normal cmds you state cmd then args on one line, but with cron you have to edit a file and it's assumed that cron is running on a config file containing the arguments that you set up and leave running,
  25. So when you do ‘crontab -e’ it drops you into a text editor where you enter only the arguments and save. This is weird to me but ok.
  26.  
  27. But first Cron uses the [system’s?] default editor specified in the environment variable EDITOR, if curious check it with
  28. `echo $EDITOR=nano`
  29. Add change it with
  30. `export EDITOR=nano`
  31. Unless of course you're comfortable in Vim.
  32. Now to create a cron job
  33. `crontab -e
  34. Edit+ save. That's it. Now the hard part is understanding the special syntax for intervals. [bring printout]
  35. To list all active cron jobs:
  36. `crontab -l`
  37. Or even by user of you're root:
  38. $ crontab -u username -l
  39. To remove all crontab jobs:
  40. $ crontab -r
  41. .
  42. Instead of typing the cron rules you can write them all in a text file and load that, for convenience.
  43. $ crontab <file>
  44. .
  45. GitHub this guide write up? Stick a commented example file in Slack during talk?
  46. .
  47. [8/5 from? tutsplus?]
  48. asterisk (*) operator specifies all possible values for a field. E.g. asterisk in the hour field is every hour.
  49. comma (,) is a list “1,5,20”
  50. dash (-) is a range “5-15” days
  51. The separator (/) specifies a step value e.g. “0-23/” hours executes every other hour. Can be after an asterisk, so every two hours is “*/2”
  52. .
  53. By default it emails you the output (cool, do other commands? Wheres it get my email, admin like git config?). To disable do
  54. 0 3 * * * /root/backup.sh >/dev/null 2>&1 [std error explnd in a resource?]
  55. .
  56. It can take special reserved string arguments instead of the first five fields:
  57. @reboot Run once at startup.
  58. @yearly Run yearly, equivalent to “0 0 1 1 *”
  59. @annually
  60. @monthly Run monthly “0 0 1 * *” [whys this need a 1 when other need 0?]
  61. @weekly Run weekly “0 0 * * 0”
  62. @daily Run daily “0 0 * * *”
  63. @midnight daily
  64. @hourly Run hourly “0 * * * *”
  65. More examples
  66. Daily at 3am:
  67. 0 3 * * * /script.sh
  68. Daily at five past midnight:
  69. 5 0 * * * /script.sh
  70. At 10 pm on weekdays:
  71. 0 22 * * 1-5 /script.sh
  72. At 2:15pm on the first of every month:
  73. 15 14 1 * * /script.sh
  74. Daily at 2 hour intervals, so 23 minutes after 24h, 2am, 4am…:
  75. 23 0-23/2 * * * /script.sh
  76. Every Sunday at 4:05 :
  77. 5 4 * * sun /path/to/unixcommand
  78. .
  79. A 15 part series on cron jobs from nixcraft, first how to add one cyberciti.biz
  80. https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
  81. 2do Hex: check unedited /etc/crontab file
  82. /etc/cron.*/
  83. None on Hex
  84. /var/spool/cron/
  85. Not on Hex
  86.  
  87. Exit Vim wo save. Esc, type “:q!”
  88. .
  89. Comment “i want to email people every 15 minutes”
  90. Wrong: “15 * * * * /script.php” is on the 15th min hourly
  91. Better: “0,15,30,45 * * * * /script.php”
  92. Best: “*/15 * * * * /script.php”
  93. ,
  94. stOv. If doing every 15s (but avoid cron for sub-minute jobs)
  95. * * * * * ./script
  96. * * * * * sleep 15; ./script
  97. * * * * * sleep 30; /script
  98. * * * * * sleep 45; /script
  99. .
  100. Quite a good tut tutsplus
  101. https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
  102. Cron jobs are scheduled commands and scripts [preferred].
  103. .
  104. Old tut 1999 unixgeeks
  105. http://www.unixgeeks.org/security/newbie/unix/cron-1.html
  106. [needs reworded. from unixgeeks
  107. Cron is a daemon which means it’s as if the cron daemon, or ‘crond’,
  108. Oh so crontab is like a permanent ‘text file’, .
  109. Oh. I thought crontab calling my scripts wasnt working but the “sleep trick” only starts after the first minute.
  110. I should make a text list of commands or scripts to cron, save in Home folder.
  111. ...
  112. ooh
  113. Uses the [system’s?] default editor specified in the environment variables EDITOR or
  114. VISUAL, to change the editor invoked on Bourne-compliant shells, try:
  115. cog@pingu $ export EDITOR=nano
  116. On C shells:
  117. cog@pingu $ setenv EDITOR nano
  118. .
  119. [my eg. For TermShellScripts doc]
  120. # this runs script every 10s (after first min). ty.sh just appends text to a file.
  121. * * * * * sleep 10; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
  122. * * * * * sleep 20; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
  123. * * * * * sleep 30; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
  124. * * * * * sleep 40; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
  125. * * * * * sleep 50; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
  126. .
  127. [and update XAMPP trick]
  128. * * * * * cp /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/zs11Arrrray.php /Users/Ed/.bitnami/stackman/machines/xampp/volumes/root/htdocs/files-block2-part4\ -\ 18J
  129. .
  130. [plan for tut or demo]
  131. # This is 60 sleep tricks, for every second. Staggered executes after 1st min.
  132. Semi ; ends a cmd statement. Also && logical operator.
  133. Wipes file with “a” then appends “b”+”c” but then wipes again, so looks like a 3 second cycle. [for-loop can generate it?]
  134. Finder isn't meant for animations lol.
  135. * * * * * sleep 1; echo ‘a’ > f.txt
  136. * * * * * sleep 2; echo ‘b’ >> f.txt
  137. * * * * * sleep 3; echo ‘c’ >> f.txt
  138. * * * * * sleep 4; echo ‘a’ > f.txt
  139. * * * * * sleep 5; echo ‘b’ >> f.txt
  140. * * * * * sleep 6; echo ‘c’ >> f.txt
  141. Etc
  142. Bonus! Here's the text tricks for how I spawned the chain of sleeps.
Add Comment
Please, Sign In to add comment