Score:0

Writing a specific format of time in a text file every minute using Cron

br flag

I'm rather new to Linux. I recently wanted to learn how to work with Cron. So I wrote the following line to crontab file and it worked:

* * * * * date >> //home/os/system-date.txt

This line will append the current date and time in system-date.txt every minute.

When I run the following command in terminal, time is printed in a specific format:

date +"%H-%M-%S"

For instance, 23-59-59 is printed.

But when I want to do this with Cron, nothing is written in the txt file. To be specific, when I write the following line in crontab

* * * * * date +"%H-%M-%S" >> //home/os/system-date.txt

nothing happens. I wonder why.

FedKad avatar
cn flag
You may have a syntax error on the command line. Try to add `2>>/home/os/cron.err` to the end of the crontab entry to see what the error is.
Fish_n_Chips avatar
br flag
Thanks. I did but no cron.err file gets created. @FedonKadifeli
Score:2
cn flag

You should escape the percent (%) signs in your crontab entries with a backslash (\) like this:

* * * * * date +"\%H-\%M-\%S" >>/home/os/system-date.txt 2>>/home/os/system-date.err

Percent signs have a special meaning in crontab entries: They are interpreted as newline characters. Please, see the man page for crontab(5):

The "sixth" field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. 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. There is no way to split a single command line onto multiple lines, like the shell's trailing "\".

Also, note that the command in a crontab entry will not be executed by /bin/bash normally. So, it is always a good practice to create a Bash script and call that Bash script from crontab. Another point to note is that the PATH environment variable is much simpler in a crontab executed command (or script), so it is again a good practice to use full path names for commands executed in a script which is written to be called by crontab.

Score:0
cn flag

Your could create a simple script file (i.e. addDate.sh) like:

#!/bin/bash
date +"%H-%M-%S" 2>&1 >> //home/os/system-date.txt

The 2>&1 redirects errors also to the specified outputfile.

and in your crontab run this script file.

* * * * * /myscripts/addDate.sh

Where /myscripts is the path where you saved your script file.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.