Json-server: Run as a background task.

Created on 14 May 2015  路  12Comments  路  Source: typicode/json-server

$ json-server -p 1337 db.json > /dev/null 2>&1 &

and then

$ curl http://localhost:1337/posts/1

Just hangs forever, bit wired, am I missing something here? //cc @typicode

Most helpful comment

If a bg process attempts to read from TTY, signal SIGTTIN is generated and the default action is to suspend the process.

Right, @kesu

@typicode json-server -p 1337 db.json >> ./json-server.log 2>&1 </dev/null & did it, thanks. :+1:

All 12 comments

Hmm... it shouldn't, I tried with:

$ json-server -p 1337 http://jsonplaceholder.typicode.com/db > /dev/null 2>&1 &
$ curl http://localhost:1337/posts/1
{
  "userId": 1,
  "id": 1,
  "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
  "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}

BTW I think there's a missing > just before /dev/null.

Is it only when you run it as a background task that you have this?

Yup only as a bg task, i'm noticing this issue.

If you only run:

json-server -p 1337 db.json &

Do you get some kind of error message? You're on OS X?

@typicode
The problem is background processes cannot read from TTY.

If a bg process attempts to read from TTY, signal SIGTTIN is generated and the default action is to suspend the process.

You can reproduce the issue as below.

kesu@kjp:~$ json-server -p 1337 db.json >> ./json-server.log 2>&1 &
[1] 27155
kesu@kjp:~$ curl "http://localhost:1337/posts/1"   #This will work
{
  "id": 1,
  "title": "json-server",
  "author": "typicode"
}kesu@kjp:~$echo "NOPE" >> ./json-server.log  # This will suspend the process

[1]+  Stopped                 json-server -p 1337 db.json >> ./json-server.log 2>&1
kesu@kjp:~$ sudo strace -p 27155
[sudo] password for kesu:
Process 27155 attached - interrupt to quit
--- SIGTTIN (Stopped (tty input)) @ 0 (0) ---
read(10, 0xa99b490, 65536)              = ? ERESTARTSYS (To be restarted)
--- SIGTTIN (Stopped (tty input)) @ 0 (0) ---
--- SIGTTIN (Stopped (tty input)) @ 0 (0) ---
read(10, 0xa99b490, 65536)              = ? ERESTARTSYS (To be restarted)
....
....

@hemanth
Forgot to mention the solution. You need to redirect stdin from /dev/null too to fix.

kesu@kjp:~$ json-server -p 1337 db.json >> ./json-server.log 2>&1 </dev/null &
[1] 27199
kesu@kjp:~$ curl "http://localhost:1337/posts/1"   #This will work
{
  "id": 1,
  "title": "json-server",
  "author": "typicode"
kesu@kjp:~$ echo "NOPE" >> ./json-server.log
kesu@kjp:~$ curl "http://localhost:1337/posts/1"   #This will still work
{
  "id": 1,
  "title": "json-server",
  "author": "typicode"
}

If a bg process attempts to read from TTY, signal SIGTTIN is generated and the default action is to suspend the process.

Right, @kesu

@typicode json-server -p 1337 db.json >> ./json-server.log 2>&1 </dev/null & did it, thanks. :+1:

@kesu Thanks for the detailed explanation. Wasn't aware of this. Do you think it's possible/should be prevented in the code?

@hemanth BTW thanks for the cool article about the project on NMOTW! I've added a link in the README.

Thank you @typicode :)

@typicode It _can_ be prevented in code but creating a custom handler for SIGTTIN signal - but I think that would be done in the node.js code. I don't think it _should_ be fixed, it is not a problem as long as the stdin is redirected when running as a bg task - since bg tasks by definition should not have any user interaction via TTY.

thanks for the info @kesu!

json-server -p 1337 db.json >> ./json-server.log 2>&1 </dev/null & 

It's work for me. Thanks.

If some one wants to run with pm2 , its working fine.

pm2 start /home/gaganb/my-porject/node_modules/.bin/json-server -- /home/gaganb/my-db.json

I thought may be useful for persons coming here for json-server as service.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boydenhartog picture boydenhartog  路  3Comments

casvil picture casvil  路  4Comments

sboudouk picture sboudouk  路  3Comments

fishenal picture fishenal  路  3Comments

pantchox picture pantchox  路  3Comments