<![CDATA[Latest posts for the topic "Chạy nodejs như 1 service trong Centos 5.8"]]> /hvaonline/posts/list/24.html JForum - http://www.jforum.net Chạy nodejs như 1 service trong Centos 5.8 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
]]>
/hvaonline/posts/list/44217.html#273137 /hvaonline/posts/list/44217.html#273137 GMT
Chạy nodejs như 1 service trong Centos 5.8 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.]]>
/hvaonline/posts/list/44217.html#273141 /hvaonline/posts/list/44217.html#273141 GMT
Chạy nodejs như 1 service trong Centos 5.8 /hvaonline/posts/list/44217.html#273162 /hvaonline/posts/list/44217.html#273162 GMT Chạy nodejs như 1 service trong Centos 5.8 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 !]]>
/hvaonline/posts/list/44217.html#273222 /hvaonline/posts/list/44217.html#273222 GMT
Chạy nodejs như 1 service trong Centos 5.8 /hvaonline/posts/list/44217.html#273225 /hvaonline/posts/list/44217.html#273225 GMT Chạy nodejs như 1 service trong Centos 5.8 Code:
command=/usr/local/bin/node /usr/local/lib/node_modules/testserver.js
]]>
/hvaonline/posts/list/44217.html#273249 /hvaonline/posts/list/44217.html#273249 GMT