From f721b03f18685a66720974430fbb770a17f08ed9 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Wed, 11 Dec 2019 11:45:05 -0500 Subject: [PATCH] add new post --- _posts/2019-12-11-Cron.md | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 _posts/2019-12-11-Cron.md diff --git a/_posts/2019-12-11-Cron.md b/_posts/2019-12-11-Cron.md new file mode 100644 index 0000000..ba7986a --- /dev/null +++ b/_posts/2019-12-11-Cron.md @@ -0,0 +1,56 @@ +--- +layout: post +title: "I used cron for the first time" +description: "And I didn't die" +date: 2019-12-11 11:38:00 +categories: random +redirect_from: + - /post/cd9dj84kf0/ + - /cd9dj84kf0/ +--- + + +[Cron](https://en.wikipedia.org/wiki/Cron) has always been one of those "scary sysadmin things" in my head. But today, I finally used it! + +## My need +I have access to a private API that happens to clear it's users if they are inactive for too long. To solve this, I decided to add a small cron job to make an API call once per month. Basically a [keepalive](https://en.wikipedia.org/wiki/Keepalive). + +## How I set it up + +Adding a cron job to my laptop was very easy. First, I made a bash script for my api call (not needed, but I felt like doing it). + +```sh +#! /bin/bash +curl --include --header "Accept: application/xml" '' --user $1:$2 +``` + +Then, by running `crontab -e` in my terminal, I just added a new line at the bottom of the file, discribing the task, and when it should be run. +```cron +# Edit this file to introduce tasks to be run by cron. +# +# Each task to run has to be defined through a single line +# indicating with different fields when the task will be run +# and what command to run for the task +# +# To define the time you can provide concrete values for +# minute (m), hour (h), day of month (dom), month (mon), +# and day of week (dow) or use '*' in these fields (for 'any').# +# Notice that tasks will be started based on the cron's system +# daemon's notion of time and timezones. +# +# Output of the crontab jobs (including errors) is sent through +# email to the user the crontab file belongs to (unless redirected). +# +# For example, you can run a backup of all your user accounts +# at 5 a.m every week with: +# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ +# +# For more information see the manual pages of crontab(5) and cron(8) +# +# m h dom mon dow command +00 11 1 * * /usr/local/bin/api-keepalive.sh +``` + +This will run once per month, on the 1st, at 11:00. + +That's it! Stupidly simple, and I am no longer scared of cron \ No newline at end of file