Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs: Bash can't find node?

Tags:

bash

node.js

#!/bin/sh
exec node /opt/nodejs/first/app.js 1>>/opt/nodejs/first/log/output 2>>/opt/nodejs/first/log/error

This shell script throws an error:

exec: 2: node: not found

I'm trying to launch it on system boot:

sudo update-rc.d autostart.sh defaults 95

I'm doing something wrong?

Maybe boot level is wrong or order number, or something else?

Thanks ;)

like image 432
Somebody Avatar asked Apr 24 '26 23:04

Somebody


2 Answers

You need to set your PATH environment variable to include the directory where your node binary lives. For starting on boot, what OS are you running? I suggest Ubuntu where you can use the upstart system. Here's a simple upstart script to make a node server work as a daemon.

description "start and stop your node.js server"
version "1.0"
author "You <[email protected]>"

start on startup
respawn

env NODE_ENV=production
env PATH=/path/to/node/bin
chdir /path/to/your/app/root
exec su -c 'node app/server.js' www-data  >> var/log/stdout.log 2>&1
like image 142
Peter Lyons Avatar answered Apr 26 '26 18:04

Peter Lyons


I don't know node, but the typical error here is that the PATH variable at the time of execution of the script does not contain the path to your program. The easiest fix is to simply use the full path:

#!/bin/bash
exec /path/to/node ...
like image 33
DarkDust Avatar answered Apr 26 '26 16:04

DarkDust



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!