0

When I run the following command in an interactive bash shell:

$ curl ipinfo.io/ip
123.45.67.890

If I create /etc/cron.d/myjob:

24 * * * * tim ( date && curl ipinfo.io/ip && date ) > /tmp/cron.log 2>&1

in /tmp/cron.log, I will get the following monitoring information from curl:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
^M  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0^M100    14  100    14    0     0    147      0 --:--:-- --:--:-- --:--:--   148
123.45.67.890

why do I get the monitoring information from curl in some case and not in other case? What are the two kinds of cases? Thanks.

0

1 Answer 1

3

The difference is that in the first case you’re writing to a terminal; the curl manpage says of its progress meter that

curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress meter as otherwise it would mess up the output mixing progress meter and response data.

When the progress meter isn’t disabled curl outputs it to standard error, and the downloaded data to standard input; in your cron job, neither of these are a terminal, and they are both redirected to /tmp/cron.log, so you see both in the log file.

This behaviour means curl “behaves nicely” in common usage scenarios. If you use curl to retrieve data and output it to the terminal, as in your first example, all you see is the data. If you use curl to retrieve data and output it to a file, or a pipe, you see progress information in the terminal.

In you cron job, you’re mixing up the two with your dual redirection to /tmp/cron.log. If you don’t want to see the output mixed up, you can disable the progress meter entirely with the -s option, or redirect standard error to another file:

24 * * * * tim ( date && curl -s ipinfo.io/ip && date ) > /tmp/cron.log 2>&1

or

24 * * * * tim ( date && curl ipinfo.io/ip && date ) > /tmp/cron.log 2>/tmp/cron.err
2
  • Thanks. Why would cron behave differently when writes to terminal or a file? In a file, it still mess up the output mixing progress meter and response data.
    – Tim
    Commented Oct 30, 2018 at 10:43
  • cron isn’t behaving differently, curl is. And you’re the one arranging for output to go to the same file! If you don’t want progress information, disable it (-s) or redirect errors to another file. Commented Oct 30, 2018 at 10:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .