
Quero registrar meu IP público em um arquivo usando o cron. Algo assim:
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
Aqui está o que eu juntei:
* * * * * { date +%FT%T | tr "\n" " "; curl https://ipinfo.io/ip -s ; echo "" ; } >> /home/mario/logs/pubip.log
Funciona* no prompt sh, mas depois de colocar os asteriscos na frente e colocá-los em crontab -e recebo o seguinte erro:
/bin/sh: 1: Syntax error: end of file unexpected (expecting "}")
SO: Ubuntu 20.04.2 LTS
*Deve haver uma maneira mais elegante de lidar com a formatação. O que eu frankensteind parece muito estranho.
Responder1
Então acabaram sendo os símbolos '%'. Deveria ter lido mais a documentação: 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.
A linha cron correta que funcionou no final é
* * * * * { date +\%FT\%T | tr "\n" " "; curl https://ipinfo.io/ip -s ; echo "" ; } >> /home/mario/logs/pubip.log
Dito isso, mudarei isso para um script apenas para facilitar a leitura, como sugeriu @scimerman.
Responder2
Não consegui reproduzir o seu. Se o comando do cron for muito volumoso, a maneira mais elegante é envolvê-lo em um script cron separado e chamá-lo do crontab:
$ cat ~/crontab.ip
#!/bin/bash
{ date +%FT%T | tr "\n" " "; curl https://ipinfo.io/ip -s ; echo "" ; } >> ~/log.ip
e meu crontab é:
$ crontab -l
* * * * * ~/crontab.ip
Deveria funcionar.
Responder3
Ao colocar comandos no sistema cron, você deve usar o caminho completo do comando.
Ou seja,
datadeveria estar/bin/data
trdeveria estar/usr/bin/tr
ondulaçãodeveria estar/usr/bin/curl.
ecodeveria estar/usr/bin/echo