1

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
6
  • Why don't you build the project and run the generated bundle?
    – MasterAM
    Jul 20, 2016 at 9:13
  • Thanks for the response, i just wanted to automatically start my app after server reboot
    – Janitha
    Jul 20, 2016 at 11:05
  • But this is not a recommended way of running it in production in the first place.
    – MasterAM
    Jul 20, 2016 at 11:07
  • I tried to put a service on systemd and call the script from it. but still it doesn't work
    – Janitha
    Jul 20, 2016 at 11:45
  • Never use meteor run --production to run your app on your production server, really - don't do that. Here are some materials that you should read: stackoverflow.com/questions/21316344/… guide.meteor.com/deployment.html#never-use-production-flag Jul 20, 2016 at 12:24

3 Answers 3

2

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.

1
  • Thank you for your response, its nicely described the process. I have used a another way to fulfill my requirement. Please go through my following mentioned answer as well.
    – Janitha
    Jul 22, 2016 at 5:03
1

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
2
  • I am happy that you've finally managed to make this working. However, please keep in mind that using meteor command in production is still not the recommended way to serve your application. It may be harmful in many ways. At the end of the day it's your call what you are going to use for yourself but take into account that other people are reading these posts as well and they should be aware that this method is sub-optimal. If I were you, I would at least add a short warning note to your answer. Jul 26, 2016 at 11:54
  • yeah i got it. Thanks a lot @apendua
    – Janitha
    Aug 3, 2016 at 9:47
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.