简体   繁体   English

在服务器重启时运行.SH文件时出现问题-Ubuntu 12.04 LTS

[英]Issue running a .SH file on server reboot - Ubuntu 12.04 LTS

I am having an issue getting the .sh file to run. 我在运行.sh文件时遇到问题。 I can run it via sudo ./starter.sh when inside my app directory, but relying for it on reboot isn't working. 我可以在我的应用程序目录中通过sudo ./starter.sh运行它,但是依靠它重新启动无法正常工作。

I am using an Ubuntu 12.04 VM on Windows 7. I have my files on Windows shared with the VM, so I access my files via /mnt/hgfs/nodejs-test 我在Windows 7上使用Ubuntu 12.04 VM。我在Windows上与该VM共享文件,因此我可以通过/ mnt / hgfs / nodejs-test访问文件

I am running a node.js server with Nginx via my local VM. 我正在通过本地VM在Nginx上运行node.js服务器。

As of right now, I can go to http://node.dev and it will properly load up my server.js located in nodejs-test (/mnt/hgfs/nodejs-test) and output hello world to the screen. 截至目前,我可以转到http://node.dev ,它将正确加载位于nodejs-test(/ mnt / hgfs / nodejs-test)中的server.js并向屏幕输出hello world。

So running the site isn't a problem..but getting forever (forever.js installed globally) to kick in on reboot isn't working. 因此,运行该站点不是问题。.但是永久重启(永久安装了全球安装的forever.js)无法正常工作。 I suspect it simply can't execute my SH file. 我怀疑它根本无法执行我的SH文件。

Here is my starter.sh 这是我的starter.sh

#!/bin/sh


if [ $(ps aux | grep $USER | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]

  then

  export PATH=/usr/local/bin:$PATH

  forever start --sourceDir /mnt/hgfs/nodejs-test/server.js >> /mnt/hgfs/nodejs-test/serverlog.txt 2>&1

fi

Now I have tried sudo crontab -e (and added my path to the file) as well as just crontab -e and did the same thing. 现在,我尝试了sudo crontab -e(并将我的路径添加到文件中)以及crontab -e,并做了同样的事情。 Upon reboot...nothing. 重新启动后...什么都没有。

@reboot /mnt/hgfs/nodejs-test/starter.sh

I tried editing that cronjob to this 我试图编辑这个cronjob

@reboot /var/www/nodejs-test/starter.sh

because I created a symlink in /var/www/nodejs-test to /mnt/hgfs/nodejs-test 因为我在/ var / www / nodejs-test中创建了到/ mnt / hgfs / nodejs-test的符号链接

Where can I check to see if an error fires on reboot, or is it possible my reboot cron isn't running at all? 在哪里可以检查重启时是否引发错误,或者重启cron根本没有运行? I know running the starter.sh DOES work though. 我知道运行starter.sh确实可以。

EDIT The /mnt/hgfs/nodejs-test is owned by root (which might be a windows thing given the files exist on my Windows 7 os). 编辑 / mnt / hgfs / nodejs-test由root拥有(鉴于Windows 7操作系统上存在文件,这可能是Windows的事情)。 My ubuntu user is "bkohlmeier" which I created on installing the VM. 我的ubuntu用户是我在安装VM时创建的“ bkohlmeier”。

EDIT #2 编辑#2

Nov 10 13:05:01 ubuntu cron[799]: (CRON) INFO (pidfile fd = 3)
Nov 10 13:05:01 ubuntu cron[875]: (CRON) STARTUP (fork ok)
Nov 10 13:05:01 ubuntu cron[875]: (CRON) INFO (Running @reboot jobs)
Nov 10 13:05:02 ubuntu CRON[887]: (bkohlmeier) CMD (/mnt/hgfs/nodejs-test/starter.sh)
Nov 10 13:05:02 ubuntu CRON[888]: (root) CMD (/var/www/nodejs-test/starter.sh >/dev/null2>&1)
Nov 10 13:05:02 ubuntu CRON[877]: (CRON) info (No MTA installed, discarding output)
Nov 10 13:05:02 ubuntu CRON[878]: (CRON) info (No MTA installed, discarding output)

Ok, I found a solution. 好的,我找到了解决方案。 Whether it is the "right" solution I don't know. 我不知道这是否是“正确的”解决方案。 Because my system shares files between HOST and GUEST (windows 7 and Ubuntu VM), the /mnt/hgfs/ has to get mounted (I think)..and the reboot happens quick enough I think the mount isn't aware yet. 因为我的系统在HOST和GUEST(Windows 7和Ubuntu VM)之间共享文件,所以/ mnt / hgfs /必须挂载(我认为)..并且重启发生得足够快,我认为挂载还不知道。

So I added this via crontab -e 所以我通过crontab -e添加了它

@reboot /bin/sleep 15; /var/www/nodejs-test/starter.sh

and it worked like a charm. 它就像一种魅力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM