crontab 里 % 引发的问题

写个 crontab ,命令是类似这样的

/path/to/script `date +%Y-%m-%d`

直接运行很正常,但是在 crotnab 里就出错。

/bin/sh: -c: line 1: unexpected EOF while looking for matching “’
/bin/sh: -c: line 2: syntax error: unexpected end of file

google 了好一阵才找到答案。原来 crontab 里的 % 是有特殊意义的,在这里需要转义。man 5 crontab 可以看到,

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.

% 如果没有用 转义,就会被替换成换行。所以之前的 crontab 就出错了。

解决办法:可以在 % 前面都加个 \ ,对于这个例子,写成 date +\%Y-\%m-\%d。

 

swfupload 服务器端输出错误信息

swfupload 上传到服务器后,对任何非 200 的响应都认为是错误,会调用 upload_error_handler 回调。但是在回调里取不到服务器端输出的错误信息,只能得到响应码。

要在 swfupload 处理服务器的错误信息,只能通过 upload_success_handler 。也就是说,不管上传成功失败,都必须是 200 响应。然后输出不同的正文。比如,成功不输出内容,失败时输出错误信息。然后在 upload_success_handler 中通过 serverData 参数来判断。

function uploadSuccess(file, serverData) {
    try {
        var progress = new FileProgress(file, this.customSettings.progressTarget);
        progress.setComplete();
        if (/^s*$/.test(serverData)) {
            progress.setStatus("ok");
        } else {
            progress.setStatus(serverData);
        }
        progress.toggleCancel(false);
    } catch (ex) {
        this.debug(ex);
    }
}