Comprimento máximo de comando do Crontab

20

Existe um limite de caracteres que um comando em um crontab possa ter?

Eu tenho um crontab com um comando de 178 caracteres e parece estar truncado em 164 quando executado. Posso distinguir esse número do e-mail que recebo e das cores do vi alteradas a partir desse ponto.

Então, é uma limitação "oficial"? Não consigo encontrar nenhuma documentação sobre isso.

Ponytech
fonte
Hmm, se tivesse um limite de comprimento, duvido que seria tão baixo quanto você está vendo. Valeria a pena trabalhar no serverfault.com/questions/449651/… e, se não ajudar a atualizar sua pergunta com sua entrada real (adequadamente redigida) no crontab.
user9517 suporta GoFundMonica
Você estava certo, o comprimento não era o problema. Veja minha resposta. Obrigado pelo link.
Ponytech

Respostas:

36

Uau, eu descobri qual é o meu problema e não tinha nada a ver com o comprimento da linha.

Acontece que meu comando tinha um% (sinal de porcentagem) nele, que tem um significado especial no crontab. Ele é usado para inserir texto no STDIN (consulte Por que meu crontab não está funcionando e como posso solucioná-lo? ).

Então eu tive que escapar disso. Meu comando que era:

gzip -c /path/to/a/file > /backup/dir/file-$(date +%F_%T).gz

torna-se

gzip -c /path/to/a/file > /backup/dir/file-$(date +\%F_\%T).gz
Ponytech
fonte
A data '+% F_% T' não seria suficiente?
jirib
Acho que ele está fazendo o eco para eliminar o caractere de nova linha que a data coloca no final de sua produção
Ed.
2
Fiquei me perguntando por que editar o crontab com destaque para vim e sintaxe fez meu comando parecer um pouco colorido. Isso %causou consternação em algumas horas!
precisa saber é o seguinte
12

Experimentalmente , há um truncamento de 999 caracteres por linha em todas as minhas máquinas Ubuntu, embora ainda não tenha encontrado nenhuma documentação explicativa relacionada.

Testei no Ubuntu 10.04, 12.04 e 14.04, e a contagem de 999 caracteres é iniciada no primeiro caractere que não seja um espaço em branco após o cronograma (no meu exemplo, o cronograma é * * * * *igual a 1 vez a cada minuto).

Observar:

  • Primeiro, contamos os caracteres do comando que adicionaremos ao nosso crontab - sem a programação :

    davidamick@myputer:~$ echo -n "/bin/echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' > /tmp/test.log" | wc -c
    999
    davidamick@myputer:~$
    
  • Adicione esse comando ao nosso crontab, para executar a cada minuto: (WARN isso substitui todas as entradas existentes do crontab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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    davidamick@myputer:~$
    
  • Comece do zero, remova o crontab e o arquivo de log:

    davidamick@myputer:~$ crontab -r
    davidamick@myputer:~$ rm /tmp/test.log
    davidamick@myputer:~$
    
  • Adicione o mesmo comando de antes, apenas 1 caractere a mais (no meu caso B):

    davidamick@myputer:~$ echo "* * * * * /bin/echo 'Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' > /tmp/test.log" | crontab -
    davidamick@myputer:~$
    
  • Espere mais um minuto, verifique se um arquivo chamado /tmp/test.lofoi criado com o seu "a" s. Isso ocorre porque o 1000º caractere foi truncado.
  • Para confirmar, remova o adicionado Be adicione um 1ao nome do arquivo, da seguinte maneira:

    davidamick@myputer:~$ echo "* * * * * /bin/echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' > /tmp/test1.log" | crontab -
    
  • Espere mais um minuto, observe que um arquivo chamado /tmp/test1.loé criado com o seu "a" s.
  • Para obter uma prova de bônus, crie um link simbólico de /bin/echopara /usr/bin/echoe descubra que você precisará remover 4 caracteres adicionais do seu comando.
damick
fonte
3
Acabei de verificar isso, é verdade. cronno meu Ubuntu 14.04, silenciosamente, trunca o comando para 999 caracteres. Isso é tão louco que demorei um pouco para digeri-lo. E este parece ser o único lugar na Internet para documentá-lo, com 1 votação positiva.
Nh2
Imagine que você tem um comando como ... && stop-power-plant; start-power-plante cron apenas silenciosamente corta; start-power-plant
NH2
1
Depois de quase 30 anos (cron é de, pelo menos, 1988), alguns brilhante alma tornou lançar um erro: github.com/systemd-cron/crontab/commit/...
NH2
1
Parece que o debian tinha um relatório de bug para isso: bugs.debian.org/cgi-bin/bugreport.cgi?bug=686223
nh2:
Se você tiver sorte, o truncamento acontece dentro de uma cotação e você recebe /bin/sh: 1: Syntax error: Unterminated quoted string, mas não há nada que garanta isso.
Nh2