0

Currently I got stuck by trying to execute a shell script via crontab. It does not work and I can't figure out what is wrong here.

What I want to do is: Execute a javascript (index.js) file with nodejs periodically.

The file run-logger.sh is executable (-rwxr-xr-x) and located under /home/pi/apps/fritz-client.

run-logger.sh:

#!/bin/bash

# execute index.js and save all output to log
/usr/bin/env node /home/pi/apps/fritz-client/index.js >> fritz.log

If I run this command standalone /usr/bin/env node /home/pi/apps/fritz-client/index.js >> fritz.log everything went fine! Even If I do cd /home/pi/apps/fritz-client && ./run-logger.sh

crontab -e:

#
# lots of comments
#

*/1 * * * * /home/pi/apps/fritz-client/run-logger.sh

crontab -l shows it too.

I tried:

  1. */1 * * * * /home/pi/apps/fritz-client/run-logger.sh
  2. */1 * * * * /bin/bash /home/pi/apps/fritz-client/run-logger.sh
  3. */1 * * * * /bin/sh /home/pi/apps/fritz-client/run-logger.sh
  4. */1 * * * * bash /home/pi/apps/fritz-client/run-logger.sh

The command more /proc/version results in:

Linux version 4.1.18-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611) ) #846 SMP Thu Feb 25 14:22:53 GMT 2016

Update

This is my first week using linux and a raspberry pi. So be please patient :)

syslog output

Mar 14 21:08:01 raspberrypi CRON[3609]: (pi) CMD (/home/pi/apps/fritz-client/run-logger.sh)
Mar 14 21:08:01 raspberrypi CRON[3602]: (CRON) info (No MTA installed, discarding output)
Mar 14 21:09:01 raspberrypi CRON[3626]: (pi) CMD (/home/pi/apps/fritz-client/run-logger.sh)
Mar 14 21:09:01 raspberrypi CRON[3619]: (CRON) info (No MTA installed, discarding output)
Mar 14 21:10:01 raspberrypi rsyslogd-2007: action 'action 17' suspended, next retry is Mon Mar 14 21:11:31 2016 [try http://www.rsyslog.com/e/2007 ]
Mar 14 21:10:01 raspberrypi CRON[3642]: (pi) CMD (/home/pi/apps/fritz-client/run-logger.sh)
Mar 14 21:10:01 raspberrypi CRON[3635]: (CRON) info (No MTA installed, discarding output)
Mar 14 21:10:06 raspberrypi crontab[3651]: (pi) BEGIN EDIT (pi)
Mar 14 21:10:19 raspberrypi crontab[3651]: (pi) REPLACE (pi)
Mar 14 21:10:19 raspberrypi crontab[3651]: (pi) END EDIT (pi)

ps aux | grep cron

root       382  0.0  0.2   5548  2452 ?        Ss   19:01   0:00 /usr/sbin/cron -f
pi        3683  0.0  0.2   4772  1936 pts/0    S+   21:11   0:00 grep --color=auto cron
6
  • Is the cron daemon running? Does it log any issues in /var/log/syslog or similar? Mar 14, 2016 at 20:10
  • Yes, it seems so. Please note my update... Maybe i'v created a great mess :) Mar 14, 2016 at 20:14
  • 2
    So, it did run your cron job, but that didn't do what you expected for some other reason. Note how it said it discarded output - this was the stderr output, which usually gets e-mailed. You can capture it in the same file by adding 2>&1 to the command. Mar 14, 2016 at 20:16
  • Like so? cd /home/pi/apps/fritz-client && /usr/bin/env node /home/pi/apps/fritz-client/index.js 2>&1 fritz.log Mar 14, 2016 at 20:19
  • No, just add it, append it at the end, e.g. >whatever.log 2>&1 Mar 14, 2016 at 20:20

2 Answers 2

2

You shouldn't use /usr/bin/env it only searches for the node executable through /bin and /usr/bin/. Specify the full path to the node executable.

Alternatively you can extend the PATH in your crontab:

PATH=$PATH:/usr/local/bin

etc.

3
  • I tried that already but did not work either... cd /home/pi/apps/fritz-client && /usr/local/bin/node /home/pi/apps/fritz-client/index.js >> fritz.log 2>&1 ` Mar 14, 2016 at 20:23
  • Why the cd and why the ...$ you don't have that in your original post. Also, if you are not going to install an MTA, make sure your crontab invocation output (stdout and stderr) are appended to some log file.
    – Anthon
    Mar 14, 2016 at 20:30
  • That was a copy and paste error as I tried a lot of variations. My vault... stdout and stderr are now (hopefully) appended to fritz.log file. Mar 14, 2016 at 20:36
1

The cronjob is running. The run-logger.sh script is being found and executed, or otherwise syslog would indicate that. But output from that is being sent to your mail inbox which is broken. Fix the MTA so it sends you mail (locally perhaps) so that you can see the output. Alternatively, modify your cronjob so that you collect the output in a file.

* * * * * /home/pi/apps/fritz-client/run-logger.sh &>/var/tmp/logger.out

You don't need the /1 -- it's redundant.

Modify your node-js script so that it also logs stderr:

/usr/bin/env node /home/pi/apps/fritz-client/index.js >> fritz.log 2>&1

It's possible that you cannot write to fritz.log. It defaults to the user's home directory. Also, is pi the user of the cronjob?

1
  • Thanks for answering! pi is the user of the cronjob. Appending 2>&1 like @Josip Rodin mentioned fixed the problem. Mar 15, 2016 at 9:24

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.