2

I am learning how to use crontab and in that pursuit what I wanted to do was print something simple in the shell. So I did crontab -e and typed the following

* * * * * bash /etc/test.sh

test.sh

echo $NODE_ENV

Now what I expect is the cronjob to print the value of NODE_ENV every one minute to the shell, which it doesn't. Why doesn't it print that? Should it not print, or am I doing something wrong?

Some things that I have already tried.

$ ps aux | grep crond    
ubuntu   15438  0.0  0.0  11284   936 pts/1    S+   09:37   0:00 grep --color=auto crond

/var/log does not have a cron.log file create so no logging of if my job has actually run.

I am running this command in Amazon EC2 instance in ubuntu 16.04, any more output you need please let me know.

1 Answer 1

6

Cron doesn't run commands using a terminal you opened. It runs jobs in the background, and saves the output to be mailed to you (if you have setup mail delivery).

If you want to view the output of a cronjob, the simplest way is to redirect the output:

* * * * * bash /etc/test.sh >> /some/output/file 2>&1

And then look at that file in a terminal you opened:

tail -f /some/other/file

On Ubuntu 16.04 and above, using systemd, to view cron's logs, do:

journalctl -u cron

And the cron daemon is just cron, not crond:

$ systemctl status cron
● cron.service - Regular background program processing daemon
   Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-01-12 19:02:04 IST; 1 weeks 1 days ago
     Docs: man:cron(8)
 Main PID: 1022 (cron)
    Tasks: 1
   Memory: 8.6M
      CPU: 24.967s
   CGroup: /system.slice/cron.service
           └─1022 /usr/sbin/cron -f
5
  • Can you please tell what 2>&1 means.
    – Saras Arya
    Jan 21, 2017 at 10:49
  • @SarasArya that's redirecting stderr to the same output as stdout. See stackoverflow.com/q/818255/2072269
    – muru
    Jan 21, 2017 at 10:51
  • Not getting the output still. * * * * * bash /etc/test.sh >> /etc/output.txt 2>&1
    – Saras Arya
    Jan 21, 2017 at 10:59
  • I even created this file output.txt. Should I have needed to create this file?
    – Saras Arya
    Jan 21, 2017 at 11:00
  • @SarasArya do you have permission to write to /etc?
    – muru
    Jan 21, 2017 at 11:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .