Ubuntu 18.04 running as guest on MacOS 10.14.3. Hypervisor: VMWare Fusion 10

Objective: Every minute, append the date/time into a file

Status: Script runs every minute, inserting a blank new line into the text file. The date is not included. That is the problem

When the script is run from the command line, using the same account as the account on the crontab, the date is added to the file, no problem.

Apparent Problem: Echoing date to file works from command line, but same command from cron does not produce date

This is the script that runs every minute in cron.

echo $(date)  >>  $target_file

This is the cron entry

crontab -e

*       *       *       *       *       /home/dan/append_date_to_file.sh

RESULTS When running three times from command line, then viewing the target file:

$ cat /home/dan/timestamps
Sat Aug 17 18:31:12 CDT 2019
Sat Aug 17 18:31:33 CDT 2019
Sat Aug 17 18:31:34 CDT 2019

Then waiting three minutes, to let it run three times in cron, then viewing the target file: (same as above, except three new blank lines after the dates.)

Problems Considered .. file permissions. To address this, here is the file as it is listed with permissions and owner:group:

-rwxrwxrwx 1 dan dan  157 Aug 17 18:43 timestamps*

.. Script not running in cron. Resolved: script is proven to run by the fact that modified time on the file changes at the start of every minute and the file gets a new blank line at the same time.

I cannot think of an explanation for the date not being added to the file when the script is run by cron instead of the command line.

Any ideas would be appreciated.

Thank you.


2 Answers 2


PATH should be a list of directories to search for commands, rather than the name of a particular command, so PATH=/bin/bash: is incorrect, as you can verify by running the following in an interactive shell:

$ PATH=/bin/bash: date
Command 'date' is available in '/bin/date'
The command could not be located because '/bin' is not included in the PATH environment variable.
date: command not found

Although cron runs jobs in a rather limited environment (you can check exactly what by running printenv > /tmp/cronenv or similar as a cron job), its default PATH includes the locations /usr/bin and /bin where the bash shell and date executables are found - so there's no need to modify it at all, just use

* * * * *       /home/dan/append_date_to_file.sh

(provided of course that /home/dan/append_date_to_file.sh is executable).

As an aside, you don't need a command substitution echo $(date) - plain date will achieve the same result.

  • Changed echo $(date) >> $target_file to echo date >> $target_file, that put the word 'date' into the file.
    – DanAllen
    Aug 18, 2019 at 1:46
  • @DanAllen just date - not echo date Aug 18, 2019 at 1:47

cron can be rather $PATH oblivious at times. Many people simply prefix commands with the command's path. For the date command, locate it's path with and one of these three commands:

$ which date

$ type -a date
date is /bin/date

$ command -v date

So change your script from:




You can shorten your script by replacing:

echo $(/bin/date)  >>  $target_file


/bin/date >> /home/dan/timestamps
  • This solved my problem. I wonder, however, why the fully qualified path/filename on $(date) is not required in Debian, but is required in Ubuntu. Since Ubuntu is derived from Debian, is this an unintended consequence of whatever enhancements Ubuntu makes to Debian?
    – DanAllen
    Aug 18, 2019 at 1:26
  • @DanAllen it wouldn't be required - if you hadn't broken the PATH with your preceding assignment Aug 18, 2019 at 1:28
  • @steeldriver Now my brain is not broken anymore, thanks to your answer.
    – DanAllen
    Aug 18, 2019 at 1:43

You must log in to answer this question.

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