Cron IP pública para registrar el archivo

Cron IP pública para registrar el archivo

Quiero registrar mi IP pública en un archivo usando cron. Algo como esto:

2021-05-17T01:11:46 99.99.99.99
2021-05-17T01:12:46 99.99.99.99
2021-05-17T01:13:46 99.99.99.99

Esto es lo que he improvisado:

* * * * * { date +%FT%T | tr "\n" " "; curl https://ipinfo.io/ip -s ; echo "" ; } >> /home/mario/logs/pubip.log

Funciona* en el indicador sh, pero una vez que pongo los asteriscos al frente y los pongo en crontab -e, aparece el siguiente error:

/bin/sh: 1: Syntax error: end of file unexpected (expecting "}")

Sistema operativo: Ubuntu 20.04.2 LTS

*Tiene que haber una forma más elegante de manejar el formato. Lo que hice frankenstein se siente bastante incómodo.

Respuesta1

Entonces resultaron ser los símbolos '%'. Debería haber leído más los documentos: crontab(5)

The "sixth" field (the rest of the line) specifies the command to
       be run.  The entire command portion of the line, up to a newline
       or a "%" character, will be executed by /bin/sh or by the shell
       specified in the SHELL variable of the cronfile.  A "%" character
       in the command, unless escaped with a backslash (\), will be
       changed into newline characters, and all data after the first %
       will be sent to the command as standard input.

La línea cron correcta que funcionó al final es

* * * * * { date +\%FT\%T | tr "\n" " "; curl https://ipinfo.io/ip -s ; echo "" ; } >> /home/mario/logs/pubip.log

Dicho esto, trasladaré esto a un script solo para facilitar la lectura, como sugirió @scimerman.

Respuesta2

No pude reproducir el tuyo. Si el comando para cron es demasiado voluminoso, una forma más elegante es envolverlo dentro de un script cron separado y llamarlo desde crontab:

$ cat ~/crontab.ip 
#!/bin/bash
{ date +%FT%T | tr "\n" " "; curl https://ipinfo.io/ip -s ; echo "" ; } >> ~/log.ip

y mi crontab es:

$ crontab -l
* * * * * ~/crontab.ip

Deberia de funcionar.

Respuesta3

Al colocar comandos en el sistema cron, debe utilizar la ruta completa del comando.

Es decir,
fechadebiera ser/bin/fecha
trdebiera ser/usr/bin/tr
rizodebiera ser/usr/bin/curl.
ecodebiera ser/usr/bin/echo

información relacionada