18

I'm trying to run a script every minute (on a Docker container running Ubuntu 16.04).

The /etc/echo.sh simply echo the word "hi"

cat /etc/crontab
* * * * *  root /etc/echo.sh > /var/log/cron.log 2>&1


/etc/init.d/cron reload
 * Reloading configuration files for periodic command scheduler cron     [ OK ]

tail -f /var/log/cron.log
hi

After printing "hi" once, nothing happens anymore.

Any ideas why?

Sig
  • 521

2 Answers2

56

The script does run every minute but > truncates the file each time.

If the file does not exist, it shall be created; otherwise, it shall be truncated to be an empty file after being opened.

(source)

Use >> instead to append to the file. Even then sometimes it may be hard to tell if the file has just grown by one line. This is why it's good to use date instead of echo hi when testing something like this.

-1
*/1 * * * *  root /etc/echo.sh > /var/log/cron.log 2>&1

I think that this is the problem. You must change the crontab minute option to */1 to run that bash every minute.

Worthwelle
  • 4,816
David
  • 1