0

This topic is confusing, I know. I can't really say what my problem is about. In few words I'll explain whats going on - I've got a AWS (amazon web service) where I've got node.js with script that looks like that:

#!/usr/bin/env node
var fs = require('fs');
var outfile = "slogan.txt";
var text = "A startup is a business built to grow rapidly.\n";
fs.writeFileSync(outfile,text);
console.log("Script: " + __filename + "\nWrote: " + text + "To: " + outfile);

pretty simple, right? Cool! I want to execute it from my computer so i'm doing it like that

ssh host1 node week2/part1.js

where host1 is working properly in 100% and its connection with .pem between MAC and AWS. Script got executed:

Bitmakers-iMac:~ Bitmaker$ ssh host1 node week2/part1.js
Script: /home/ubuntu/week2/part1.js
Wrote: A startup is a business built to grow rapidly.
To: slogan.txt

but... there is no slogan.txt! I have to get conenction inside server

ssh host1 

and then run it form server. Can anyone can explain me why its working like that? I udnerstand its may still be confusing but I hope this screenshot will help a bit in understanding this. Thanks for reading this long post! I hope you know the answer. enter image description here

4

2 回答 2

3

ssh在 shell 中执行命令的方式有一个基本的区别。

在外壳中,您说cd week2然后发出node part1.js,而您ssh host1 node week2/part1.js在另一种情况下说。该文件slogan.txt位于包含的目录中part1.js,当您说node week2/part1.js.

更改以下行

var outfile = "slogan.txt";

改为指定完整路径slogan.txt

如果您尝试,您将能够重现该问题

cd ~ && node week2/part1.js

从壳。

于 2013-07-01T08:24:42.457 回答
1

当您 ssh 到服务器时,shell 所做的第一件事就是读取所有环境文件(.profile、.bashrc、.kshrc 等...)。但是,在执行远程命令时,不会发生这种情况。在执行之前尝试获取所需的 env 文件,例如:

ssh host ". ./.profile; node week2/part1.js"

可能还需要使用. ./.profile; . ./.bashrc;or . ./.profile; . ./.kshrc;,具体取决于您使用的 shell 以及这些文件是否也具有 env(主要是 PATH)设置。

于 2013-07-01T08:20:55.163 回答