2

I am trying to run a simple echo script via crontab. I set that to run every minute, but it doesn't give output on shell screen. However, it runs fine when I run the script independently.

Script

#!/bin/bash
echo "Test Script"

Crontab entry:

root@example-server ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
  *  *  *  *  * root /root/test.sh
5
  • 2
    That's a very unique formatting style. But why do you believe the crontabbed script would write to your terminal? – mikeserv Jun 20 '15 at 0:15
  • 2
    All crontab processes are forked upon execution... Try modifying your Test Script like so: echo "Test Script" >> /var/log/crontest.log, then either go read the log in an editor or cat /var/log/crontest.log – eyoung100 Jun 20 '15 at 0:59
  • hi eyoung100, when you fork a new process, does the child process inherit the stdout, stderr file descriptors of the parent cron process? of course, if cron process runs as a daemon, it may not have these file descriptors open in the first place.... – user93868 Jun 20 '15 at 1:36
  • i gave one upvote for the way, Todd has formatted his code.... – user93868 Jun 20 '15 at 1:37
  • Thank you guys. I was in impression that it will give output every minute. – Todd Goodfellow Jun 20 '15 at 1:46
1

The output of a cron job doesn't go to your screen. It can't — you might not even be logged in by the time the job runs!

The output of a cron job is sent via email. A working unix system always has a local email facility, which is independent of a network connection. If you want your local email to be sent to a remote account, create a .forward file in your home directory containing the remote email address. Some distributions don't set up local email by default, in which case cron output disappears in a black hole. You need a mail transfer agent to deliver local email. On an individual machine, configure it not to accept incoming connections from the network (most distributions have an easy way to set this up). Common MTAs include Exim and Postfix; if your distribution has a default MTA, just pick it.

0

you must redirect the output of echo to certain file

1.- run this command: tty

It will tell you something like /dev/pts/X Check for the number X

2.- modify your cron job with the command

crontab -e

*/1 * * * * echo "Hello every minute" > /dev/pts/X

And that's all friend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.