Search code examples
cron

How can I get a crontab job to append values onto a new line instead of carrying on from the previous line?


I am learning about crontabs and a tutorial that I am currently looking at gives the following example to generate a random 14-length character password:

cat /dev/urandom | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 14

An example output is this:

T1sBY1lX9RIAl8

I added this to a crontab to be run every minute and for the output to be saved / appended to an output file like so:

* * * * * cat /dev/urandom | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 14 >> ./passwords.log

The problem I am having is that I want each new entry to be added onto a new line but can't seem to figure out how. I can only assume that \n is involved somewhere, but I don't know where.

So this is what I want:

Xf9StaSFajIfqD
UVMyQNvbWQSH7X
mM4x6ei9kDquyG
UJQXO4fYT4hqly

I currently get:

Xf9StaSFajIfqDUVMyQNvbWQSH7XmM4x6ei9kDquyGUJQXO4fYT4hqly

How can I achieve the former result?


Solution

  • You can try with something like:

    * * * * * cat /dev/urandom | env LC_CTYPE=C tr -dc a-zA-Z0-9 | head -c 14 >> ./passwords.log; echo >>./passwords.log
    

    The idea is to execute echo command which in this case print newline and add it to the file. Seems like you filter with tr all but letters and numbers, then get 14 of them. And no newline in this sequence

    And its wise to work with absolute paths, not relative, especially with cron