Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

bash - Percent sign % not working in crontab

I have a cron issue with curl:

curl -w "%{time_total}
" -o /dev/null -s http://myurl.com >> ~/log

works great and add a line in log file with total_time.

But the same line with cron doesn't do anything.

It's not a path problem because curl http://myurl.com >> ~/log works.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

% is a special character for crontab. From man 5 crontab:

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.

So you need to escape the % character:

curl -w "%{time_total}
" -o /dev/null -s http://myurl.com >> ~/log

to

curl -w "\%{time_total}
" -o /dev/null -s http://myurl.com >> ~/log
         ^

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...