Possible to configure anacron for a specific day/schedule?

Questions about other topics - please check if your question fits better in another category before posting here
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
Avatar1
Level 2
Level 2
Posts: 66
Joined: Mon Jun 19, 2017 7:55 pm

Possible to configure anacron for a specific day/schedule?

Post by Avatar1 »

Hi all,

I have a desktop computer:
Specs: Desktop: Cinnamon 5.6.8
Distro: Linux Mint 21.1 Vera base: Ubuntu 22.04 jammy

I'm trying to schedule a job to run on a specific day of the week (let's say Wednesday), or if the scheduled run is missed, then n minutes after next bootup, to resume on the specific day of the week (Wednesday) next time.

I currently have the job running from cron.weekly so it runs once a week, but not on a specific day. For example, if I originally set up the job on Wednesday, it will run next Wednesday unless the computer is off. If so, then it will run the next time the computer is on, say Friday. After this it will run every Friday unless the computer is off, at which point it will run the next available day and so on...

I know that cron allows for setting a specific day and time, but if that time is missed, it won't run at all until the next schedule comes up.

I've been researching for hours but haven't been able to find a way to do this. :?:

Is there a way to do what I want to do? (Which basically seems to be to combine cron and anacron together lol)
Last edited by LockBot on Mon Dec 04, 2023 11:00 pm, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
linux-rox
Level 10
Level 10
Posts: 3334
Joined: Sun Jul 19, 2020 9:17 pm

Re: Possible to configure anacron for a specific day/schedule?

Post by linux-rox »

I've never used any of these, but have seen several sources recommend the systemd timer for these complex schedule problems.
Avatar1
Level 2
Level 2
Posts: 66
Joined: Mon Jun 19, 2017 7:55 pm

Re: Possible to configure anacron for a specific day/schedule?

Post by Avatar1 »

Thanks for the reply and sorry for the delay. I tried to come back a couple times and check, but the forums were down, and then I forgot haha

Anyway, I will look into this. Seems a bit complicated but that's Linux :) Thank you!
User avatar
AndyMH
Level 21
Level 21
Posts: 13757
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Possible to configure anacron for a specific day/schedule?

Post by AndyMH »

I do something similar - run a backup job once a week to my NAS.

I use cron not anacron with

Code: Select all

#rsync home to synology
0 */4 * * * /home/andy/.config/synbackup/synologyback >> /home/andy/.config/synbackup/cronerrors.txt 2>&1
So my script is running every 4 hours. I save the time for the next backup as unix time in a file, the script checks if now > backup time, if so does the backup and saves the next backup time to the time file. If now < backup time it just exits. That way, if the computer is not on at the alloted hour it doesn't matter, it will do the backup next time it is on.

Relevant snippets of code:

Code: Select all

oneweek=604800 #in seconds, used to set date for next home backup
You could always be a bit more sophisticated and work out when the next Wednesday is.

Code: Select all

#get time now in unix time
today=`date +"%Y-%m-%d %H:%M:%S"`
todayepoch=$(date -d "${today}" +"%s")
Convert time now into unix time.

Code: Select all

#load time for next backup from file and check
backtime=`cat $configfolder/$timefile`
if (( todayepoch < backtime )); then #not time for backup
    echo not time for a backup
    exit 1
fi
Is it time for backup?

If it is then good to go, when finished set time for next backup.

Code: Select all

nextbackup=$(( $todayepoch + $oneweek ))
echo $nextbackup > $configfolder/$timefile
Other things I learnt along the way:
  • The PATH cron sees is not the PATH you see, specify the full pathname to your script.
  • I use notify-send to send a notification when it has done a backup, this does not work without:

Code: Select all

#this is needed for notify-send to work
export XDG_RUNTIME_DIR=/run/user/$(id -u) 
  • I use yad to give me a simple GUI showing backup progress when the backup runs, cron has no idea what display you are using.

Code: Select all

#this is needed for yad to work
export DISPLAY=:0 
Note there is nothing elegant about my bash scripts, I just kludge my way through with a lot of help from google.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
Locked

Return to “Other topics”