百分号 % 在 crontab 中不起作用 [英] Percent sign % not working in crontab

查看:33
本文介绍了百分号 % 在 crontab 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 curl 的 cron 问题:

I have a cron issue with curl:

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

效果很好,并使用 total_time 在日志文件中添加一行.

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

但是与 cron 相同的行没有做任何事情.

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

这不是路径问题,因为curl http://myurl.com >>~/log 有效.

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

推荐答案

%crontab 的特殊字符.来自 man 5 crontab:

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

第六个"字段(行的其余部分)指定要执行的命令跑.该行的整个命令部分,直到换行符或"%" 字符,将由/bin/sh 或指定的 shell 执行在 cronfile 的 SHELL 变量中.%"字符在命令,除非用反斜杠 () 转义,否则将被更改为换行符,第一个 % 之后的所有数据将被发送到命令作为标准输入.

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

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

这篇关于百分号 % 在 crontab 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆