4

I'm running some node.js scripts with forever.

I started them as www-data user (i.e. via ssh logging as www-data@server and typing forever start myapp.js, etc.).

Ok, everything goes like a charm; my application works as expected, and typing forever list shows me the processes running.

Then, I wrote a (very simple) script named foreverList.sh and placed in folder /var/www/scripts/ :

#!/bin/bash

echo "checking running node.js scripts:"
forever list

When I execute the script from shell as www-data user, it works. Nothing fancy here. But, this script is to be used with PHP 5.6 through Apache 2.4, with :

<?php
// ....
$resp_script = shell_exec("/var/www/scripts/foreverList.sh 2>&1");
echo "<pre>$resp_script</pre>";

And when I execute it via my web browser (so with the PHP code above), it doesn't work!! I get this forever error message (nodejs, not php):

/usr/lib/node_modules/forever/lib/forever.js:674
procs.forEach(function (proc) {
^
TypeError: Object Error: EACCES, permission denied '/root/.forever/sock' has no method 'forEach'
...
... #(stack trace follows)

I really don't understand why it deals with /root folder, as I'm running all this stuff from /var/www/ folder and as www-data user (added a whoami in the script to check it was the case, and it does).

Do I miss something? Is it a forever bug?

Any help will be much appreciated.

UPDATE : I tried to add echo $HOME in the bash script, and it returns nothing. Does PHP itself erase the home path environment variable of www-data user?

  • That is just...weird. except I believe php has a bit different syntax when it comes to permissions. – Virusboy Nov 26 '14 at 1:49
  • it has different wording. – Virusboy Nov 26 '14 at 19:05
  • Forever needs a store for the process info. Normally it's ~/.forever/. Maybe (wild guess here) www-data doesn't have a homedir configured so it reverts to root's homedir? What's the homedir for your www-data user (try: getent passwd www-data | cut -d: -f6)? Do you use a config.json and are there some clues to this behaviour? – agtoever Nov 28 '14 at 10:48
  • Thx. www-data has its own home dir, which is /var/www. It also has a .forever dir in there, with a config.json file and all logs files of the running processes. – Polosson Nov 28 '14 at 10:53
3

Well, I finally found the solution.

Actually, Apache unset the $HOME environment variable, (see line 4 of /etc/apache2/envvar file: unset HOME)

The only thing to do was to add:

export HOME=/var/www

before the forever list line in the bash script.

And works like a charm.

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.