1

I've configure a cron task for running a NodeJS script everyday.

crontab -l

# Edit this file to introduce tasks to be run by cron.
# 
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
55 23 * * * /usr/bin/node /getGames/.

But my script is never executed (the script write data on a file, and the last modification time of this file is never updated.) When I run the script manually, it's working.

I've no idea why it's not working

4
  • are you sure that this actually runs? ... /usr/bin/node /getGames/.
    – jsotola
    Commented Jun 18, 2021 at 0:15
  • Is your parameter for the node command /getGames/. (including the dot)? Is /getGames/ in your root directory? Commented Jun 18, 2021 at 0:19
  • Yeah, /usr/bin/node /getGames/. exist (/usr/bin/node /getGames/index.js) Commented Jun 18, 2021 at 9:28
  • The command /usr/bin/node /getGames/. run manually Commented Jun 18, 2021 at 9:28

1 Answer 1

0

Jobs run through cron, or at, or batch, aren't run in the same runtime environment that you have on your desktop. None of your PATH changes, or other environment variable settings are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

5
  • Even with a bash script, cron not running Commented Jun 22, 2021 at 23:38
  • Did you compare your desktop environment with the cron environment?
    – waltinator
    Commented Jun 23, 2021 at 0:44
  • How to show env of cron ? Commented Jun 23, 2021 at 9:22
  • I showed the commands to use in my answer, what don't you understand?
    – waltinator
    Commented Jun 23, 2021 at 14:12
  • When i run echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias it shows the env of 'root', how to show env of cron Commented Jun 23, 2021 at 23:06

You must log in to answer this question.

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