0

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 [email protected]  Fri Mar  3 22:43:00 2023
Return-Path: <[email protected]>
X-Original-To: john
Delivered-To: [email protected]
Received: by MacBook-Pro-3.local (Postfix, from userid 503)
    id 5CD99869153; Fri,  3 Mar 2023 22:43:00 -0800 (PST)
From: [email protected] (Cron Daemon)
To: [email protected]
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: <[email protected]>
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?

Kamil Maciorowski
  • 19,242
  • 1
  • 50
  • 94
John
  • 1
  • 1
  • Does this answer your question? [cron jobs printing to /dev/stdout](https://unix.stackexchange.com/questions/520170/cron-jobs-printing-to-dev-stdout) – Kamil Maciorowski Mar 04 '23 at 07:10
  • 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

1

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,420
  • 1
  • 37
  • 54
  • 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