Crontab-Trivia

Recently I have to use crontab to run a script at midnight (a Hadoop job in our office cluster to collect some tracking data). To follow the script’s log, I use tee to fork the log to some log file named with the date. But after the day I set the cronjob, I found nothing in the grafana neither of the HDFS, this means the job didn’t run.

I wrote the job command as follows:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 1 * * * root bash /home/myname/collect/run.sh | tee /home/myname/collect/log/$(date +%s).log

What do you think of this script? Is it really OK to run?

But the vim shows me a different color of the character %. Firstly I didn’t notice the hit, think it was just a highlight. As soon as I found the job didn’t run, I noticed that the matter isn’t that simple.

Cronjob is not simply a shell script. It has its own meaning of the special character. Wikipedia can tell you that. Cron EN | ZH

We should use backslash \ to escape the special %. Otherwise, all data after the first % are sent to the command as standard input.

0 1 * * * root bash /home/myname/collect/run.sh | tee /home/myname/collect/log/$(date +\%s).log

Sometimes a common, simple thing can cause a big problem for you. What you have to do is RTFM, no matter simple the thing is. Actually, we know nothing.

comments powered by Disqus