Find the answer to your Linux question:
Results 1 to 5 of 5
Hi everyone! Google and forum search could not help me much. My problem is that cat output differs when the command is run as a cron job. I believe square ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3

    Exclamation Weird cat output in cronjobs


    Hi everyone!

    Google and forum search could not help me much. My problem is that cat output differs when the command is run as a cron job. I believe square brackets '[]' are to blame, but I could not find a good solution myself. Here's how to reproduce the issue:

    Create a file with this content:

    Code:
    [06/08/2012 14:37:44] [TRYING] [ig-231-35-1.some.abc] [abcde] - Some notes here 16.22
    Then try this script:

    Code:
    #!/bin/bash
    LOGFILE=/full/path/to/input.txt
    LASTSTAT="$(cat ${LOGFILE})"
    cat $LOGFILE
    echo $LASTSTAT
    when this code is run by hand, I can see that both lines match:

    Code:
    [06/08/2012 14:37:44] [TRYING] [ig-231-35-1.some.abc] [abcde] - Some notes here 16.22 
    [06/08/2012 14:37:44] [TRYING] [ig-231-35-1.some.abc] [abcde] - Some notes here 16.22
    But when I run this as a cronjob, I see:

    Code:
    [06/08/2012 14:37:44] [TRYING] [ig-231-35-1.some.abc] [abcde] - Some notes here 16.22 
    [06/08/2012 14:37:44] R a b a b - Some notes here 16.22
    Any suggestions will be very much appreciated... Thanks in advance!

    -Mehmet
    Last edited by mehmetb; 06-12-2012 at 08:55 PM. Reason: minor edit

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,601
    Cron jobs are run as background processes without necessarily any usable stdin/stdout/stderr file descriptors, unless you purposely direct the output to a file that you can look at from your user shell.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3
    Looks like this is not a cron issue.

    Code:
    $ String="[06/08/2012 14:37:44] [TRYING] [ig-231-35-1.some.abc] [abcde] - Some notes here 16.22"
    $ echo $String 
    [06/08/2012 14:37:44] R a b a b - Some notes here 16.22
    This does not happen for other users, so it is related to something specific to my account, although I use standard .bashrc and .bash_profile


  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,601
    Try using single quotes instead of double quotes. Alternatively, use backslashes to escape the square brackets.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Jun 2012
    Posts
    3
    Quote Originally Posted by Rubberman View Post
    Try using single quotes instead of double quotes. Alternatively, use backslashes to escape the square brackets.
    Already tried both. This must be related to something in my env. Still investigating. Thank you for your suggestions!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •