1

I am new to Unix, learning how to schedule a job. Starting with a simple command like

* * * * * echo 'hello'

on vim. However I do not get the standard output at the start of every minute, instead receiving a mail in /var/mail/john. It goes as follows:

From john@MacBook-Pro-3.local  Fri Mar  3 22:43:00 2023
Return-Path: <john@MacBook-Pro-3.local>
X-Original-To: john
Delivered-To: john@MacBook-Pro-3.local
Received: by MacBook-Pro-3.local (Postfix, from userid 503)
    id 5CD99869153; Fri,  3 Mar 2023 22:43:00 -0800 (PST)
From: john@MacBook-Pro-3.local (Cron Daemon)
To: john@MacBook-Pro-3.local
Subject: Cron <john@MacBook-Pro-3> echo 'hello hello'
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=john>
X-Cron-Env: <USER=john>
Message-Id: <20230304064300.5CD99869153@MacBook-Pro-3.local>
Date: Fri,  3 Mar 2023 22:43:00 -0800 (PST)

hello hello

MacBook-Pro-3:~ john$ You have new mail in /var/mail/john MacBook-Pro-3:~ john$

Can anyone throw some light on it, so that the cron job can be executed as intended?

John
  • 11
  • 2
  • What exactly is the intended behavior? Are you aware that each time the job runs, zero, one or more terminals may belong to your user. It's a non-trivial task to find even one of them, cron does not even try. Do you want to see hello in all of them? Or in one? which one? – Kamil Maciorowski Mar 04 '23 at 07:24
  • Typically, I test my time parameters in cron using a crontab entry like */3 * * * * date >> Test.cron and watching the output in another terminal using tail -F Test.cron. – Paul_Pedant Mar 04 '23 at 09:56
  • Hi Kamil, I tried this echo 'echo' > /dev/stdout but I didn't get the hello on screen. My purpose was practising. Did I write anything wrong in that command line? Also thanks for your advice on how I should format my question, I will do it. – John Mar 05 '23 at 22:00

1 Answers1

2

The cron service is running as designed. You have asked a daemon to run a command every minute. It does this regardless of if you or anyone else is logged in. It does this regardless of if you have one or a hundred terminals open.

If there is any output from a cron job it is mailed to you as you may not be around.

If you want to get the word hello sent to you every minute in the current terminal then you should consider doing it a different way. One simple approach would be to write an infinite loop which sleeps for 60 seconds, then outputs hello. This should be run in the background using &

while true
do
    sleep 60
    echo hello
done &

It is not perfect, as the tiny amount of time taken to run the echo will gradually mount up. It doesn't handle the case where you suspend the terminal output. However it is good enough for almost everything.

There is also a command write that may be on your system. It is not much used these days. You could change your cron job to be

* * * * * echo hello | write john

(assuming your user name was john, write would attempt to find a terminal that is associated with you and copy the output of your echo command to that terminal.

icarus
  • 17,920
  • Hi Icarus, thanks a lot, I followed your lines on instruction, and it works. However the standard output came with other pieces as info that I didn't want them to show up on screen. They are like this: /Users/john/Desktop/Screenshot 2023-03-05 at 1.34.17 PM.png. Is there a way just have it print hello? Thanks – John Mar 05 '23 at 21:41
  • Sorry, I don't have access to your machine so I can't see the screenshot. Can you describe what you it shows? – icarus Mar 07 '23 at 03:35