While running a cron job in Linux, I sometimes get this error and I had been struggling to solve this until I came across this post.

Although that particular post mentions the cause being Vagrant, mine was due to a cron job.

First the solution …

Modify .profile under home folder

Open ~/.profile

Replace the following line –

mesg n || true

with

tty -s && mesg n

The reason for this is given very well in the post linked above.

Whenever a cron job runs, it’s running in a non-interactive login shell. That is, there’s no place to show any output. The command mesg n ensures that no other user can write your terminal device.

But since the cron is running from a non-interactive shell, there is no terminal device to start with! This is what gives the error.

This error usually occurs when there’s a shell command being run from the cron which give an output in the terminal.