# \[오류 노트] kill 명령어로 프로세스 종료

## 오류 발생&#x20;

REST API 실습을 위해 json-server를 실행했다.&#x20;

```bash
➜  TodoApp + REST API (with JSON Server) json-server database/db.json --watch

  \{^_^}/ hi!

  Loading database/db.json
  Done

  Resources
  http://localhost:3000/todos
  http://localhost:3000/ediya-menu

  Home
  http://localhost:3000

  Type s + enter at any time to create a snapshot of the database
  Watching...
```

하지만 오류가 발생한다!!&#x20;

`address already in use 127.0.0.1:3000`  → 이미 3000 포트를 사용하고 있다는 것이다.&#x20;

```bash
Some error occurred Error: listen EADDRINUSE: address already in use 127.0.0.1:3000
    at Server.setupListenHandle [as _listen2] (net.js:1318:16)
    at listenInCluster (net.js:1366:12)
    at GetAddrInfoReqWrap.doListen [as callback] (net.js:1503:7)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:69:8) {
  code: 'EADDRINUSE',
  errno: -48,
  syscall: 'listen',
  address: '127.0.0.1',
  port: 3000
}
```

이럴 경우 구동하는 포트 번호를 3001와 같은 식으로 바꾸거나 이미 사용 되고 있는 3000의 서버를 강제 종료하면 된다.&#x20;

## 해결 방법&#x20;

### kill 명령어로 프로세스 종료

서버를 구동시킨 상태에서 losf 명령어를 사용 입력한다. \
-i 옵션 뒤에 프로토콜과 포트를 명시해주면 TCP 3000번 포트를 사용하는 프로세스 정보가 출력된다.&#x20;

```bash
lsof -i TCP:3000 
```

![명령어 출력 결과 ](https://1609565946-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQBEZ0w9UlpTQnX-DAe%2F-MS3E22qj5TR6cxQWNsn%2F-MS3IVs-TEhyzTfQQXZR%2Fimage.png?alt=media\&token=2570838d-6231-470e-a7f2-9feea9841f1e)

위 사진과 같이 정보가 출력된다. 여기서 확인 할 것은 실행된 node의 PID(process ID)이다. 58841번을 기억하고 이제 해당 프로세스를 중지해보자&#x20;

{% hint style="info" %}
서버를 구동 시켰을 때 명령어를 또 입력할 수 없다면?&#x20;

![](https://1609565946-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQBEZ0w9UlpTQnX-DAe%2F-MS3MXPp8Ii2c8UMqoXu%2F-MS3Mz96qWavivD--iTo%2Fimage.png?alt=media\&token=0bd2c13b-4dc4-411c-b897-ad9c66bf385e)

터미널의 우측 상단에 이런 아이콘이 있다. 여기서 가운데 아이콘을 클릭하면 터미널 창이 하나 더 생긴다.&#x20;
{% endhint %}

### kill 명령어

![](https://1609565946-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQBEZ0w9UlpTQnX-DAe%2F-MS3KNKzU3_8nymmYPXh%2F-MS3KhT_E8SanniR4yL4%2Fimage.png?alt=media\&token=4109888b-8337-4f35-88d8-26746c122ea3)

kill -9 명령어를 사용하면 해당 프로세스가 중지 된다.&#x20;

```bash
kill -9 (PID) 또는 kill -KILL (PID)
```

![프로세스가 종료되었다는 안내 ](https://1609565946-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MQBEZ0w9UlpTQnX-DAe%2F-MS3KNKzU3_8nymmYPXh%2F-MS3LyCXYgH_8IbnXV9u%2Fimage.png?alt=media\&token=55114dea-6cef-4f4e-9f48-aa4fc684129a)

{% hint style="danger" %}
`kill -9` 명령어는 해당 프로세스를 즉시 종료하기 때문에 데이터 유실되거나 리소스가 제대로 안닫히는 문제가 발생할 수 있다.&#x20;

권장하는 방법은 `kill -TERM PID` 나 `kill -INT PID` 같이 종료를 의미하는 signal 을 여러 번 전송해서 종료시킨다. 제대로 된 프로그램은 보통 cleanup 코드를 수행하고 종료하게 됩니다.
{% endhint %}

## 참고&#x20;

* [kill 명령어로 안전하게 프로세스 종료시키는 방법 ](https://www.lesstif.com/system-admin/unix-linux-kill-12943674.html)
* [lsof 명령어 사용 방법 ](https://www.lesstif.com/system-admin/lsof-20776078.html)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://shhn0509.gitbook.io/javascript/web/rest-api/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
