0

I want to run a script on the 1st of every month.
If the computer was powered off, I'd like to execute it the next time it turns on.

Anacron fits in regard to the "powered off" use case but it only provides daily, weekly, monthly intervals. Monthly is too late and weekly way too early.

I checked fcron but that package clashes with Timeshift so that's not an option.

I was thinking if cron can run the task one time, at any time between the 1st & the 4th of each month, that would be ok too. I had a look at the cron syntax and think that's actually not possible.

Does anybody know how to solve this?

I'm on Arch Linux (Manjaro).

0
1

Something like this (untested)

#!/bin/bash
# run this via crontab on days 1-4 and @reboot
#
# Store the run_month here, or somewhere writable on disk not /tmp
runfile="$HOME/run_month"
# make sure $runfile exists, initalize to a non-month if 1st run ever 
[[ ! -f "$runfile" ]] && echo "init" >"$runfile"
#
# get the last month we ran
rf="$(cat "$runfile")"
# get the current month
cm="$(date "+%b")"
# if $rf is the same as $cm, quit
if [[ "$cm" = "$rf" ]] ; then
  exit
fi
# Remember we ran this month
echo "$cm" >"$runfile"
#
# Left  as an exercise for the student 
1
  • I coded my solution too now but yours is more elegant. I was subtracting the seconds (+%s) of $last_run from $today and comparing the result with the seconds of 25 days xD
    – progonkpa
    Oct 30 '21 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.