Something you can also do is run the condition inside your crontab file, that is, something like that.
00 23 27-31 * * [ $(date -d +2day +\%d) -eq 1 ] && /programas/backups.sh
That is, "If the day after tomorrow is a day 1, then today is the penultimate day of this month."
That is similar to what @IamDavid answered, except that in this way you do not worry about deleting that script on another occasion.
Both the @IamDavid option and mine are part of the possibilities set in man cron(5)
.
LIMITATIONS
The crontab syntax does not make it possible to define all possible periods one could image off. For
example, it is not straightforward to define the last weekday of a month. If a task needs to be run in a
The best approach would be to have
the program itself check the date and time information and continue execution only if the period matches
the desired one.
If the program itself can not do the checks then a wrapper script would be required. Useful tools that
could be used for date analysis are ncal or calendar For example, to run a program the last Saturday of
every month you could use the following wrapper code:
0 4 * * Sat [ "$(date +\%e)" = "'ncal | grep $(date +\%a | sed -e 's/.$//') | sed -e 's/^.*\s\([0-9]\+\)\s*$//''" ] && echo "Last Saturday" && program_to_run
Update 1
Thinking about the option of @IamDavid, about using the suggestion of a script to return a 0 or 1 depending on the day, I came up with the idea of doing a script that contains that, that is, including it and so on you could jump that one day, after many, you go back to see that crontab and do not remember what it was doing.
That is, your crontab will be defined in the same way:
00 23 27-31 * * /programas/backup.sh
But your program backup.sh
should have that characteristic at the beginning. Something like that.
#!/bin/bash
PASADO_MANANA="$(date -d +2day +%d)" #No necesitas escapar el caracter %
#Documentacion, etc, etc
#Puedes utilizar comodamente el built-in [[ sin miedo a la portabilidad
[[ "$PASADO_MANANA" != "01" ]] && exit
# Si pasado manana no es "01" entonces hoy no es el penúltimo día,
# entonces se sale del programa.
# Esto se ejecuta cada penúltimo día del mes, etc, etc.
#Resto del código de backups.sh
I have not tried it but I think it would be more comfortable in this way, although perhaps its disadvantage is that within the crontabs file it lacks verbosity regarding its execution time.