Cron
Contents
Syntax
# Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed
Scheduling Tricks
every Thursday at 3 p.m
00 15 * * Thu /usr/local/bin/mycronjob.sh
quarterly reports after the end of each quarter (use the first day of the following month)
02 03 1 1,4,7,10 * /usr/local/bin/reports.sh
one minute past every hour between 9:01 a.m. and 5:01 p.m.
01 09-17 * * * /usr/local/bin/hourlyreminder.sh
to run a job every two, three, or four hours. That can be accomplished by dividing the hours by the desired interval, such as */3 for every three hours, or 6-18/3 to run every three hours between 6 a.m. and 6 p.m. Other intervals can be divided similarly; for example, the expression */15 in the minutes position means "run the job every 15 minutes."
*/5 08-18/2 * * * /usr/local/bin/mycronjob.sh
in Docker
dockerfile
FROM node:11.15.0-alpine RUN apk --no-cache add fetchmail ca-certificates openrc COPY scripts/fetchmail/config/cron /usr/src/fetchmail.cron RUN chmod 655 /usr/src/fetchmail.cron COPY scripts/fetchmail/config/docker-entrypoint.sh /usr/src/docker-entrypoint.sh RUN chmod 655 /usr/src/docker-entrypoint.sh ENTRYPOINT ["/usr/src/docker-entrypoint.sh"]
Use apk for installing, upgrading, configuring, and removing apps/programs for an Alpine Linux operating system in a consistent manner.
Install
- fetchmail
- ca-certificates
- openrc
OpenRC is the init system used in alpine. The init system manages the services, startup and shutdown of your computer.
scripts/fetchmail/config/cron
* * * * * node /opt/cxm/app/api/scripts/fetchmail/fetchmailrc.js
scripts/fetchmail/config/docker-entrypoint.sh
#!/usr/bin/env sh set -eu crond -b rc-update add local default crontab /usr/src/fetchmail.cron node /opt/cxm/app/api/scripts/fetchmail/fetchmailrc.js chmod 400 ~/.fetchmailrc fetchmail -N -f ~/.fetchmailrc --sslcertpath /etc/ssl/certs
- crond -b : run in background
- rc-update : add a service to runlevel
- crontab : add cron to daemon
- fetchmail.js : writes ~/.fetchmailrc file
~/.fetchmailrc
poll imap.gmail.com proto imap: uidl # POP3 only user "ali.iybar.inbox@gmail.com" pass "MyPassword" preconnect 'echo "$(date) fetching Email" >> /root/.fetchmail.log' fastuidl 1 # POP3 only keep no rewrite mda "node /opt/cxm/app/api/scripts/fetchmail/fetchmailmda.js --connector-id 0805fc18-be09-4219-bdae-5db919e99f75" ssl
fetchmailmda.js
Script which receives a full Mail object from an IMAP server, parses the headers, content and any attachments and prepares it for the next stage in the process (database/queuing).
This script is called from the `fetchmail` daemon which expects an exit code of `0`. If there are any problems processing an email, a non-zero exit code will mean that the fetchmail daemon will try again in the next pass.
async function main() {
process.stdin.resume();
process.stdin.setEncoding('utf8');
const source = process.stdin;
const mail = await simpleParser(source);
const mailPayload = toEmailPayload(mail);
...
const name = parseFullName(get(mailPayload, 'from[0].name', ''));
it gets the email, parse and use the parsed payloads to save it