Create a startup script for meteor in linux server

三世轮回 提交于 2021-02-08 08:56:19

问题


I have followed some posts and tutorials as well to create a script to start meteor project when server restart. i have followed answer mentioned in : How to run meteor on startup on Ubuntu server

Then I gave executable permission to script with "chmod +x meteor-server.sh".

I have tried to put this script in /etc/init.d and /etc/init folders but meteor project does not start at the reboot. I'm using ubuntu 16.04.

I would be grateful if you can show me the fault that i have done. Following code is my "meteor.server.sh" script file.

     # meteorjs - meteorjs job file

     description "MeteorJS"
     author "Jc"

     # When to start the service
     start on runlevel [2345]

     # When to stop the service
     stop on runlevel [016]

     # Automatically restart process if crashed
     respawn

     # Essentially lets upstart know the process will detach itself to the background
     expect fork

     # Run before process
     pre-start script
    cd /home/me/projects/cricket
    echo ""
    end script

   # Start the process
   exec meteor run -p 4000 --help -- production

回答1:


First of all, there's already a very good tool mupx that allows you to deploy meteor projects to your own architecture, so there's really no need to do it by yourself unless you have a very good.

If you really need to deploy manually, this will take several steps. I am not going to cover all the details because you're specifically asking about the startup script and the remaining instruction should be easily accessible in the Internet.

1. Prepare your server

  • Install MongoDB unless you are planning to use a remote database.
  • Install NodeJS.
  • Install reverse proxy server, e.g. Nginx, or haproxy.
  • Install Meteor, which we will use as the build tool only.

2. Build your app

I am assuming here that you already have the source code of your app put on the server to which you're planning to deploy. Go to your project root and run the following command:

meteor build /path/to/your/build --directory

Please note that if /path/to/your/build exists it will be recursively deleted first, so be careful with that.

3. Install app dependencies

Go to /path/to/your/build/bundle/programs/server and run:

npm install

4. Prepare a run.sh script

The file can be of the following form:

export MONGO_URL="mongodb://127.0.0.1:27017/appName"
export ROOT_URL="http://myapp.example.com"
export PORT=3000
export METEOR_SETTINGS="{}"

/usr/bin/env node /path/to/your/build/bundle/main.js

I will assume you put it in /path/to/your/run.sh. A couple of notes here:

  • This form of MONGO_URL assumes you have MongoDB installed locally.
  • You will need to instruct your reverse proxy server to point your app trafic to port 3000.
  • METEOR_SETTINGS should be the output of JSON.stringify(settings) of whatever settings object you may have.

5. Prepare upstart script

With all the preparations we've made so far, the script can be as simple as

description "node.js server"

start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [016]

respawn
script
exec /path/to/your/run.sh
end script

This file should go to /etc/init/appName.conf.




回答2:


Finally i got it to work. I have used following 2 scripts to run meteor on the startup. First i put this service file(meteor.service) in /etc/systemd/system

       [Unit]
       Description = My Meteor Application

       [Service]
       ExecStart=/etc/init.d/meteor.sh
       Restart=always

       StandardOutput=syslog
       StandardError=syslog
       SyslogIdentifier=meteor

      [Install]
      WantedBy=multi-user.target

I have called a scipt using this service. I put this following script(meteor.sh) in /etc/init.d

  #!/bin/sh -

  description "Meteor Projects"
  author "Janitha"

  #start service on following run levels
  start on runlevel [2345]

  #stop service on following run levels
  stop on runlevel [016]

  #restart service if crashed
  respawn

  #set user/group to run as
  setuid janitha
  setgid janitha

  chdir /home/janitha/projects/cricket_app

  #export HOME (for meteor), change dir to plex requests dir, and run meteor
  script
    export HOME=/home/janitha
    exec meteor
  end script

I make both these file executable by using

 chmod +x meteor.service
 chmod +x meteor.sh

And i have used following two commands to enable the service

systemctl daemon-reload
systemctl enable meteor.service



回答3:


I used this configurations successfully

In /etc/init.d add a file called meteor.sh


#!/bin/sh 
export HOME="/home/user"
cd /home/user/meteor/sparql-fedquest
meteor --allow-superuser

You must give executions permissions to meteor.sh

sudo chmod 644 meteor.sh

Also you must create meteor.service in /etc/systemd/system

[Unit]
   Description =Portal of bibliographic resources of University of Cuenca
   Author = Freddy Sumba
   [Service]
   ExecStart=/etc/init.d/meteor.sh
   Restart=always
   StandardOutput=syslog
   StandardError=syslog
   SyslogIdentifier=meteor
  [Install]
  WantedBy=multi-user.target

Also you must give permissions to meteor.service

$ sudo chmod 644 meteor.service

Then, we need add the service to this start each time that the server reboot

$  systemctl enable meteor.service

And finally start the service

$ service meteor start


来源:https://stackoverflow.com/questions/38473489/create-a-startup-script-for-meteor-in-linux-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!