Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Talk Cron
- [unedited notes dump. later Ill do properly with pictures]
- 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.
- Please note, this info may not be relevant to PCs because CmdPrompt is different. Blame Microsoft.
- .
- Cron is a venerable old program from the Unix days so there's guidance available online
- https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
- Or more authentic guide from 1999
- http://www.unixgeeks.org/security/newbie/unix/cron-1.html
- Or the man page for truly studious people
- `man crontab`
- Cron is a can way to schedule commands or scripts. and you set the interval e.g on 9th hr daily.
- Cron's “arguments” for specifying time intervals has odd syntax but luckily there are diagrams.
- [Img]
- There are 5 ‘slots’ separate by spaces.
- 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.
- A range of numbers is also doable.
- And list numbers with comma, so long as no spaces.
- .
- 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.
- [Alt] Along with these are folders for “weekly”, just stick a script here if u don't need granular ‘5-slot’ precision.
- 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,
- 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.
- But first Cron uses the [system’s?] default editor specified in the environment variable EDITOR, if curious check it with
- `echo $EDITOR=nano`
- Add change it with
- `export EDITOR=nano`
- Unless of course you're comfortable in Vim.
- Now to create a cron job
- `crontab -e
- Edit+ save. That's it. Now the hard part is understanding the special syntax for intervals. [bring printout]
- To list all active cron jobs:
- `crontab -l`
- Or even by user of you're root:
- $ crontab -u username -l
- To remove all crontab jobs:
- $ crontab -r
- .
- Instead of typing the cron rules you can write them all in a text file and load that, for convenience.
- $ crontab <file>
- .
- GitHub this guide write up? Stick a commented example file in Slack during talk?
- .
- [8/5 from? tutsplus?]
- asterisk (*) operator specifies all possible values for a field. E.g. asterisk in the hour field is every hour.
- comma (,) is a list “1,5,20”
- dash (-) is a range “5-15” days
- 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”
- .
- By default it emails you the output (cool, do other commands? Wheres it get my email, admin like git config?). To disable do
- 0 3 * * * /root/backup.sh >/dev/null 2>&1 [std error explnd in a resource?]
- .
- It can take special reserved string arguments instead of the first five fields:
- @reboot Run once at startup.
- @yearly Run yearly, equivalent to “0 0 1 1 *”
- @annually
- @monthly Run monthly “0 0 1 * *” [whys this need a 1 when other need 0?]
- @weekly Run weekly “0 0 * * 0”
- @daily Run daily “0 0 * * *”
- @midnight daily
- @hourly Run hourly “0 * * * *”
- More examples
- Daily at 3am:
- 0 3 * * * /script.sh
- Daily at five past midnight:
- 5 0 * * * /script.sh
- At 10 pm on weekdays:
- 0 22 * * 1-5 /script.sh
- At 2:15pm on the first of every month:
- 15 14 1 * * /script.sh
- Daily at 2 hour intervals, so 23 minutes after 24h, 2am, 4am…:
- 23 0-23/2 * * * /script.sh
- Every Sunday at 4:05 :
- 5 4 * * sun /path/to/unixcommand
- .
- A 15 part series on cron jobs from nixcraft, first how to add one cyberciti.biz
- https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
- 2do Hex: check unedited /etc/crontab file
- /etc/cron.*/
- None on Hex
- /var/spool/cron/
- Not on Hex
- Exit Vim wo save. Esc, type “:q!”
- .
- Comment “i want to email people every 15 minutes”
- Wrong: “15 * * * * /script.php” is on the 15th min hourly
- Better: “0,15,30,45 * * * * /script.php”
- Best: “*/15 * * * * /script.php”
- ,
- stOv. If doing every 15s (but avoid cron for sub-minute jobs)
- * * * * * ./script
- * * * * * sleep 15; ./script
- * * * * * sleep 30; /script
- * * * * * sleep 45; /script
- .
- Quite a good tut tutsplus
- https://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800
- Cron jobs are scheduled commands and scripts [preferred].
- .
- Old tut 1999 unixgeeks
- http://www.unixgeeks.org/security/newbie/unix/cron-1.html
- [needs reworded. from unixgeeks
- Cron is a daemon which means it’s as if the cron daemon, or ‘crond’,
- Oh so crontab is like a permanent ‘text file’, .
- Oh. I thought crontab calling my scripts wasnt working but the “sleep trick” only starts after the first minute.
- I should make a text list of commands or scripts to cron, save in Home folder.
- ...
- ooh
- Uses the [system’s?] default editor specified in the environment variables EDITOR or
- VISUAL, to change the editor invoked on Bourne-compliant shells, try:
- cog@pingu $ export EDITOR=nano
- On C shells:
- cog@pingu $ setenv EDITOR nano
- .
- [my eg. For TermShellScripts doc]
- # this runs script every 10s (after first min). ty.sh just appends text to a file.
- * * * * * sleep 10; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
- * * * * * sleep 20; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
- * * * * * sleep 30; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
- * * * * * sleep 40; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
- * * * * * sleep 50; sh /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/ty.sh
- .
- [and update XAMPP trick]
- * * * * * cp /Users/Ed/Documents/Projects/Tutorials/OU/Block2/04/zs11Arrrray.php /Users/Ed/.bitnami/stackman/machines/xampp/volumes/root/htdocs/files-block2-part4\ -\ 18J
- .
- [plan for tut or demo]
- # This is 60 sleep tricks, for every second. Staggered executes after 1st min.
- Semi ; ends a cmd statement. Also && logical operator.
- Wipes file with “a” then appends “b”+”c” but then wipes again, so looks like a 3 second cycle. [for-loop can generate it?]
- Finder isn't meant for animations lol.
- * * * * * sleep 1; echo ‘a’ > f.txt
- * * * * * sleep 2; echo ‘b’ >> f.txt
- * * * * * sleep 3; echo ‘c’ >> f.txt
- * * * * * sleep 4; echo ‘a’ > f.txt
- * * * * * sleep 5; echo ‘b’ >> f.txt
- * * * * * sleep 6; echo ‘c’ >> f.txt
- Etc
- Bonus! Here's the text tricks for how I spawned the chain of sleeps.
Add Comment
Please, Sign In to add comment