banner

[Rule] Rules  [Home] Main Forum  [Portal] Portal  
[Members] Member Listing  [Statistics] Statistics  [Search] Search  [Reading Room] Reading Room 
[Register] Register  
[Login] Loginhttp  | https  ]
 
Forum Index Thảo luận hệ điều hành *nix Chạy nodejs như 1 service trong Centos 5.8  XML
  [Question]   Chạy nodejs như 1 service trong Centos 5.8 29/01/2013 09:54:50 (+0700) | #1 | 273137
[Avatar]
Yuno
Member

[Minus]    0    [Plus]
Joined: 08/07/2006 16:58:02
Messages: 30
Offline
[Profile] [PM]
Chào các bạn,

Mình đang cố găng cài đặt nodejs cho server chạy CentOS 5.8. việc cài đặt và chạy nodejs cũng đã thành công, nhưng mình gặp 1 vấn đề là khi chạy nodejs thì chương trình chiếm luôn màn hình. Nếu thoát khỏi màn hình của nodejs thì đồng nghĩa với việc stop nodejs. Mình cũng đã cố gắng viết 1 script để cho nodejs chạy như 1 service chạy ngầm nhưng đang gặp vấn đề là: service đã chạy nhưng vẫn bị nodejs chiếm màn hình như trên. Nhờ các bạn xem giúp mình script và khắc phục lỗi giúp mình, do là newbie trong việc viết script nên còn nhiều hạn chế mà tự bản thân mình không sao tìm được đường ra.

Chân thành cám ơn các bạn

đoạn code này mình lấy từ internet về rồi sửa lại các tham số và lệnh trong centos.

Code:
#!/bin/sh
#
# nodejs - this script starts and stops the nodejs
# chkconfig:   - 85 15 
# description:  node /usr/local/lib/node_modules/testserver.js
#               
# processname: node
# config:      
# pidfile:     

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nodejs="/usr/local/bin/node"
prog=$(basename $nodejs)

NODEJS_CONF_FILE="/usr/local/lib/node_modules/testserver.js"

lockfile=/var/lock/subsys/nodejs

start() {
    [ -x $nodejs ] || exit 5
    [ -f $NODEJS_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    supervisor $nodejs $NODEJS_CONF_FILE  # hoac node $NODEJS_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killall -9 $prog
    killall -9 nodejs
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killall -9 $nodejs
    killall -9 nodejs
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nodejs $NODEJS_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac
[Up] [Print Copy]
  [Question]   Chạy nodejs như 1 service trong Centos 5.8 29/01/2013 12:49:01 (+0700) | #2 | 273141
[Avatar]
quanta
Moderator

Joined: 28/07/2006 14:44:21
Messages: 7265
Location: $ locate `whoami`
Offline
[Profile] [PM]
Chạy nodejs như 1 service trong Centos 5.8 

Có nhiều cách lắm: từ cách đơn giản như thêm & ở cuối lệnh:
Code:
runuser -l nodejs -s /bin/bash -c "nodejs testserver.js >> /var/log/nodejs.log &" && echo_success || echo_failure

(`echo_success` và `echo_failure` là 2 functions trong `/etc/init.d/functions` để in ra [ OK ] hoặc [ FAILED ] mà bạn thường thấy khi start/stop những init scripts)

đến cách dùng những công cụ như: supervisor, Forever, nodemon, ...

Ở đây, bạn định dùng `supervisor`. Sau khi cài, mình nghĩ nên:
- đọc tài liệu: http://supervisord.org/.
- mở `/etc/supervisord.conf` ra đọc
- thử thêm một section cho nodejs

Bạn thử chạy `supervisor /usr/local/bin/node testserver.js` từ command line xem, làm sao mà được nhỉ, chưa nói gì đến việc biến thành daemon.
Let's build on a great foundation!
[Up] [Print Copy]
  [Question]   Chạy nodejs như 1 service trong Centos 5.8 30/01/2013 07:28:43 (+0700) | #3 | 273162
[Avatar]
Yuno
Member

[Minus]    0    [Plus]
Joined: 08/07/2006 16:58:02
Messages: 30
Offline
[Profile] [PM]
Cám ơn Quanta, tôi sẽ làm theo hướng dẫn của anh. nếu gặp khó khăn gì nhờ anh hỗ trợ smilie
[Up] [Print Copy]
  [Question]   Chạy nodejs như 1 service trong Centos 5.8 31/01/2013 10:26:35 (+0700) | #4 | 273222
[Avatar]
Yuno
Member

[Minus]    0    [Plus]
Joined: 08/07/2006 16:58:02
Messages: 30
Offline
[Profile] [PM]
Chào anh quanta,

Nhờ anh support giúp trường hợp này.

Tôi đã cài đặt và chỉnh sửa lại file /etc/supervisord.conf và trong /etc/init.d đã có file supervisord.

Với 2 file này thì hiện tại tôi đã làm được như sau :

TH 1. Chạy lệnh #supervisord
thì mọi thứ đều như ý, nghĩa là chuơng trình supervisord và node đều chạy.

TH 2. Chạy lệnh # service supervisord start
Lúc này thì chỉ có một mình supervisord chạy thôi, node không chạy. Do đó tôi bỏ file supervisord vào init.d thì khi restart máy thì chỉ có chương trình supervisord chạy được.

Nhờ anh xem giúp là tôi sai và thiếu ở đâu , hiện tại tôi vẫn đang mài mò thêm bớt một vài câu lệnh vào supervisord mà vẫn fail.

supervisord.conf như sau
Code:
[supervisord]
http_port=/var/tmp/supervisor.sock ; (default is to run a UNIX domain socket server)
;http_port=127.0.0.1:9001  ; (alternately, ip_address:port specifies AF_INET)
;sockchmod=0700              ; AF_UNIX socketmode (AF_INET ignore, default 0700)
;sockchown=nobody.nogroup     ; AF_UNIX socket uid.gid owner (AF_INET ignores)
;umask=022                   ; (process file creation umask;default 022)
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (logging level;default info; others: debug,warn)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;http_username=user          ; (default is no username (open system))
;http_password=123           ; (default is no password (open system))
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;user=chrism                 ; (default is current user, required if root)
;directory=/tmp              ; (default is not to cd during start)
;environment=KEY=value       ; (key value pairs to add to environment)

[supervisorctl]
serverurl=unix:///var/tmp/supervisor.sock ; use a unix:// URL  for a unix socket
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

[program:nodejs]
command=node /usr/local/lib/node_modules/testserver.js  ; the program (relative uses PATH, can take args)
process_name=%node
;priority=999                ; the relative start priority (default 999)
autostart=true              ; start at supervisord start (default: true)
autorestart=true            ; retstart at unexpected quit (default: true)
;startsecs=10                ; number of secs prog must stay running (def. 10)
;startretries=3              ; max # of serial start failures (default 3)
;exitcodes=0,2               ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT             ; signal used to kill process (default TERM)
;stopwaitsecs=10             ; max num secs to wait before SIGKILL (default 10)
;user=chrism                 ; setuid to this UNIX account to run the program
log_stdout=true             ; if true, log program stdout (default true)
log_stderr=true             ; if true, log program stderr (def false)
logfile=/var/log/node.log    ; child log path, use NONE for none; default AUTO
logfile_maxbytes=1MB        ; max # logfile bytes b4 rotation (default 50MB)
;logfile_backups=10          ; # of logfile backups (default 10)


supervisord trong init.d
Code:
#!/bin/bash
#
# supervisord   This scripts turns supervisord on
#
# Author:       Mike McGrath <mmcgrath@redhat.com> (based off yumupdatesd)
#
# chkconfig:    - 95 04
#
# description:  supervisor is a process control utility.  It has a web based
#               xmlrpc interface as well as a few other nifty features.
# processname:  supervisord
# config: /etc/supervisord.conf
# pidfile: /var/run/supervisord.pid
#

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

start() {
        echo -n $"Starting supervisord: "
        daemon supervisord
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/supervisord
}

stop() {
        echo -n $"Stopping supervisord: "
        killproc supervisord
        echo
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/supervisord
}

restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop) 
        stop
        ;;
  restart|force-reload|reload)
        restart
        ;;
  condrestart)
        [ -f /var/lock/subsys/supervisord ] && restart
        ;;
  status)
        status supervisord
        RETVAL=$?
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
        exit 1
esac

exit $RETVAL


Chân thành cám ơn !
[Up] [Print Copy]
  [Question]   Chạy nodejs như 1 service trong Centos 5.8 31/01/2013 13:06:05 (+0700) | #5 | 273225
[Avatar]
quanta
Moderator

Joined: 28/07/2006 14:44:21
Messages: 7265
Location: $ locate `whoami`
Offline
[Profile] [PM]
Làm việc trên Linux, mỗi khi chương trình hoạt động không như mong muốn, việc đầu tiên bạn nên làm là mở log ra xem có gì.

Giờ bạn chia Terminal làm 2: cái trên chạy `tail -f /var/log/supervisor/supervisord.log`, cái dưới gõ `sudo /etc/init.d/supervisord` (re)start` rồi nhìn lên trên xem có gì.
Let's build on a great foundation!
[Up] [Print Copy]
  [Question]   Chạy nodejs như 1 service trong Centos 5.8 01/02/2013 09:12:35 (+0700) | #6 | 273249
[Avatar]
Yuno
Member

[Minus]    0    [Plus]
Joined: 08/07/2006 16:58:02
Messages: 30
Offline
[Profile] [PM]
Cám ơn anh quanta đã hướng dẫn và giúp tôi thực hiện điều mình cần, tôi đã chỉnh lại được rồi, và giờ mọi thứ đã chạy như ý.

Do tôi đọc không kỹ các link mà anh quanta đưa nên không nắm rõ cách thức hoạt động và truyền biến cho các lệnh. mài mò mãi mới phát hiện ra gắn tham số cho lệnh command trong supervisord.conf sai. hix. smilie

Code:
command=/usr/local/bin/node /usr/local/lib/node_modules/testserver.js




[Up] [Print Copy]
[digg] [delicious] [google] [yahoo] [technorati] [reddit] [stumbleupon]
Go to: 
 Users currently in here 
1 Anonymous

Powered by JForum - Extended by HVAOnline
 hvaonline.net  |  hvaforum.net  |  hvazone.net  |  hvanews.net  |  vnhacker.org
1999 - 2013 © v2012|0504|218|