브라우저 환경에서 원격지 서버의 vscode 를 구동시킬 수 있는 서비스
우분투나 CentOS 7 이상에서 설치하는 방법은 아래 레퍼런스 페이지에도 있으므로 AWS 환경에서 설치하는 방법을 정리한다.
github에 정리되어 있는 설치 스크립트를 실행한다.
$ curl -fsSL https://code-server.dev/install.sh | sudo sh
Amazon Linux 2
Installing v3.10.2 rpm package from GitHub releases.
mkdir -p ~/.cache/code-server
... (중간생략)
To have systemd start code-server now and restart on boot:
sudo systemctl enable --now code-server@$USER
Or, if you don't want/need a background service you can run:
code-server
정상적으로 설치가 되면 위와 같은 메시지가 출력될 것이다. 설치한 환경이 Amazon Linux 2 일 경우에는 systemctl
명령어가 있으므로 다음의 명령을 통해 시작 시 구동이 되도록 설정한다.
$ sudo systemctl enable --now code-server@ec2-user
개별적으로 시작/종료를 수행하는 스크립트는 아래와 같다.
# 시작 스크립트
sudo systemctl start code-server@ec2-user
# 종료 스크립트
sudo systemctl stop code-server@ec2-user
오래전에 구성된 EC2에는 이전 버전의 리눅스가 설치되어 있어 systemctl
명령어가 존재하지 않는다.
$ systemctl
-bash: systemctl: command not found
$ cat /etc/os-release
NAME="Amazon Linux AMI"
VERSION="2018.03"
ID="amzn"
ID_LIKE="rhel fedora"
VERSION_ID="2018.03"
PRETTY_NAME="Amazon Linux AMI 2018.03"
ANSI_COLOR="0;33"
CPE_NAME="cpe:/o:amazon:linux:2018.03:ga"
HOME_URL="http://aws.amazon.com/amazon-linux-ami/"
Amazon Linux AMI 2018.03에서는 code-server 에서 제시하는 방법으로 실행이 불가능하므로 다음과 같이 service 스크립트를 생성한다.
$ cd /etc/init.d
$ sudo vi code-server
#!/bin/bash
# chkconfig: 345 90 90
# description: vs code-server
SERVICE="code-server"
case "$1" in
start)
echo "code-server starting..."
su - ec2-user -c "/usr/bin/code-server &"
exit
;;
stop)
echo "code-server stopping..."
PIDs=`ps -ef --sort -pid | grep "$SERVICE" | grep -v 'grep' | awk '{print $2}'`
if pgrep -f "$SERVICE" > /dev/null
then
echo "PID : $PIDs"
for pid in $PIDs; do kill -9 $pid; done
else
echo "$SERVICE stopped"
fi
;;
status)
echo "code-server status..."
if pgrep -f "$SERVICE" > /dev/null
then
echo "$SERVICE is running"
else
echo "$SERVICE stopped"
fi
;;
restart)
echo "code-server restart..."
PIDs=`ps -ef --sort -pid | grep "$SERVICE" | grep -v 'grep' | awk '{print $2}'`
if pgrep -f "$SERVICE" > /dev/null
then
echo "PID : $PIDs"
for pid in $PIDs; do kill -9 $pid; done
sleep 15
su - ec2-user "/usr/bin/code-server &"
exit
else
echo "$SERVICE stopped"
su - ec2-user "/usr/bin/code-server &"
exit
fi
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
;;
esac
추가적으로 권한 설정을 위해 아래의 명령을 실행한다.
$ sudo chmod 755 code-server
$ sudo chkconfig --add code-server
$ chkconfig --list
...
code-server 0:off 1:off 2:off 3:on 4:on 5:on 6:off
개별적으로 시작/종료를 수행하는 스크립트는 아래와 같다.
# 시작 스크립트
sudo service code-server start
# 종료 스크립트
sudo service code-server stop
참고로 stop 스크립트는 불안정하니 kill 을 통해 프로세스를 종료하는 것을 추천한다.