Find the answer to your Linux question:
Results 1 to 6 of 6
I want to write the date & time and a text string to a file from crontab. The following line works fine in the CL: echo $(/bin/date +"%F %T")" Some ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3

    cmd works in bash, not crontab??

    I want to write the date & time and a text string to a file from crontab.

    The following line works fine in the CL:
    echo $(/bin/date +"%F %T")" Some text" >> /home/me/foo.txt

    I installed in crontab and no text appears in the file that it is redirected to.

    The crontab entry looks like:
    * * * * * echo $(/bin/date +"%F %T")" Some text" >> /home/me/foo.txt

    Tried a version to just write to stdout....

    * * * * * echo $(/bin/date +"%F %T")" Some text"

    No date, time or text appears at the command line, nada.

    Shell is bash.
    Any suggestion what the problem is and how to fix???
    thanks,
    -J

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    You might want to run the command with nohup instead. Some applications will barf if they aren't attached to some sort of terminal for all of stdin, stdout, and stderr.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Try this :
    Code:
    /bin/echo
    in your cron file. cron ignores the system's PATH variable. You have to redefine it in the cron file or use absolute paths.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

  4. #4
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3
    Quote Originally Posted by Rubberman View Post
    You might want to run the command with nohup instead. Some applications will barf if they aren't attached to some sort of terminal for all of stdin, stdout, and stderr.
    OK, well I tried nohup, as well as &, and neither command worked...e.g. no text in the target file. Here are the excerpts from crontab:

    * * * * * nohup echo $(/bin/date +"%F %T")" From cron" >> /home/me/foo.log&


    and,
    * * * * * echo $(/bin/date +"%F %T")" From cron" >> /home/me/foo.log&

    Not sure I used nohup correctly, but the command worked when entered from the CL.
    Good suggestion...am sorry it didn't fix the problem....sigh
    -J
    Last edited by three_jeeps; 07-16-2010 at 05:33 PM. Reason: mistake

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Use single quotes with the date command:

    Code:
    $(/bin/date +'%F %T')" Some text" >> /home/me/foo.txt
    Or escape the % character:

    Code:
    $(/bin/date +"\%F \%T")" Some text" >> /home/me/foo.txt

  6. #6
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3

    Resolved!

    Quote Originally Posted by Franklin52 View Post
    Use single quotes with the date command:

    Code:
    $(/bin/date +'%F %T')" Some text" >> /home/me/foo.txt
    Or escape the % character:

    Code:
    $(/bin/date +"\%F \%T")" Some text" >> /home/me/foo.txt
    Using the escape in the date command, in crontab worked:
    Code:
    * * * * * echo $(/bin/date +"\%F \%T")" Some text" >> /home/me/foo.txt
    Interestingly, using the single quotes did not....hmmm
    Code:
    * * * * * echo $(/bin/date +'%F %T')" Some text" >> /home/me/foo.txt
    Thank you!
    -J

Posting Permissions

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