Un robot en date et heure
Par hr le samedi 14 juin 2008, 20:30 - truc - Lien permanent
C'est un classique et je veux vous en faire profiter, il n'y a pas de raison. Une petite tâche dans crontab qui a besoin de la date courante et *pouf* des messages d'erreur incompréhensibles :
Syntax error: end of file unexpected (expecting ")")
C'est parti pour une petite investigation.
Ma ligne crontab ressemble à quelque chose comme ce qui suit :
59 23 * * * /home/hr/codaz/script.py $(date +%Y%m%d)
Rien de bien sorcier pourtant, je fournis simplement la date courante à un script qui est lancé à 23h59 tous les jours. Et pourtant la source du problème est abordée dans la page de man de 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 % char-
acter, will be executed by /bin/sh or by the shell specified in the SHELL
variable of the cronfile. Percent-signs (%) in the command, unless
escaped with backslash (\), will be changed into newline characters, and
all data after the first % will be sent to the command as standard input.
Il faut donc toujours penser à échapper les caractères '%' dans une ligne de crontab pour éviter ce type de soucis, ma ligne devient donc :
59 23 * * * /home/hr/codaz/script.py $(date +\%Y\%m\%d)
Je ne devrais plus oublier cette fois-ci!
Commentaires