Cron cannot find node
Cooya opened this issue · comments
It is probably not a bug but instead something I do not understand.
I have installed NodeJS LTS (18.17) then installed n then uninstalled NodeJS to keep only n and avoid version conflicts.
But after that, the cron service on my server cannot find node (error "/bin/sh: 1: node: not found"). I tried to restart it but no effect. To make it work, I had to do :
ln -s /usr/local/bin/node /usr/bin/node
ln -s /usr/local/bin/npm /usr/bin/npm
I do not understand why the cron service cannot find node in /usr/local/bin because my PATH is "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/snap/bin", so it should be all right.
Any explanation ?
The PATH in the cron job is probably different than what you see in an interactive shell session.
Yes indeed, the PATH in my cron tasks is "/usr/bin:/bin", that is something I was not aware of. I guess it would be worth mentioning in the documentation of n that symlinks are required to use NodeJS versions downloaded with n in cron tasks.
I think this is an issue for cron to document, rather than n
.
I think this is an issue for cron to document, rather than
n
.
Due to the fact that the node exec path is non-standard (ie. not in usual dirs for exec files that one may expect on an *nix system), an info note may really help in the readme file.
It's not really intuitive to expect different PATH values for your user and for cron, and this will help dealing with the confusion.
Or, even better: maybe add some additional step / option to the n installer, to add symlinks in an usual location (eg. /usr/bin/node or similar), just as you would get (as far as i remember) from installing node with apt, brew and whatnot.
The three commonly used approaches for coping with cron only searching very limited directories are:
- use full path to the executable, like
/usr/local/bin/node
instead of justnode
- execute the command using a shell, like
bash -lc some-command
to get the bash startup scripts run too - set the desired PATH at the top of the cron job
Or, even better: maybe add some additional step / option to the n installer, to add symlinks in an usual location (eg. /usr/bin/node or similar), just as you would get (as far as i remember) from installing node with apt, brew and whatnot.
You can set N_PREFIX
to /usr
which will install everything under that root including in particular /usr/bin/node
.
The point of using a default prefix of /usr/local
is keep the local additions separate from the system managed directories. Adding symlinks into /usr/bin
would compromise that.
brew
follows the same pattern as n
. It installs to a location outside the cron supported directories (i.e. /bin
and /usr/bin
). Its commands will not be found by cron without extra work either.
(I saw one reference that apt
packages specify the install location, rather than apt setting the prefix/destination. But in a quick read I didn't find more about that.)
Nice example for brew which shows approach (3) setting path, including /usr/local/bin
: