I have a script with this as the shebang #!/usr/bin/env node
.
When cron runs my script, I get this error /usr/bin/env: node: No such file or directory
.
When i run the script as my user the script runs fine, just not as cron. I'm guessing it's because node is not on the PATH of the user that runs cron?
How can I get this shebang to work with cron?
$ which node
gives me /home/myuser/.nvm/v0.11.14/bin/node
Cron jobs run in a restricted environment. In an interactive shell, your $PATH
is probably set in your $HOME/.bash_profile
or $HOME/.bashrc
. Cron jobs are executed in an environment that hasn't sourced those files, so your user-specific $PATH
settings will not be available.
You can see what that environment looks like by temporarily creating a crontab entry like:
* * * * * printenv > crontab-environment
You can explicitly set $PATH
in your crontab, either in the command itself:
* * * * * PATH=$PATH:/home/myuser/.nvm/v0.11.14/bin some_command
or in a separate line in your crontab:
PATH = /usr/bin:/bin:/home/myuser/.nvm/v0.11.14/bin
You can't (directly) use the usual PATH=$PATH:/new/dir
syntax to append a directory to your $PATH
in an environment setting line, because variable references are not replaced in such a line. They are processed in crontab command lines.
man 5 crontab
for details.
Another option is to use an explicit full path in the script itself, changing
#!/usr/bin/env node
to
#!/home/myuser/.nvm/v0.11.14/bin/node
You'll need to customize this for each system where node
is installed in a different place. Or you can arrange for node
(or nodejs
?) to be installed in a consistent place.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.