Schedule Crontab

2

I have a problem when I want to schedule a crontab task that allows executing /programas/backups.sh all penultimate days of the all months of the year (in a single line). At 23.00 hours Sending an email at the end of the execution to the [email protected] account

I do not know if anyone has any reference to be able to program this crontab

    
asked by Renan Matias Maturana Diaz 18.11.2018 в 22:40
source

2 answers

2

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.

    
answered by 25.11.2018 / 12:05
source
1

I found this link: link

What it does on this page is to run a script in which it looks if tomorrow is a lower number than today (that is, if today is 31, tomorrow will be day 1, so there is a change of month), if so, return "true" and if not false.

I think you could adapt it to your problem by taking instead of the morning number, the number within 2 days.

I hope it serves you!

    
answered by 21.11.2018 в 21:23