node os method and usage example

OS (Operating System)

Operating system-related practical methods are provided.
Usage: const os = require('os');

Methods and examples provided by os

  • os.type()
    Returns the name of the operating system as a string
    eg:

       > os.type()
       'Linux'
    

    Linux System:'Linux'
    macOS system:'Darwin'
    Windows System:'Windows_NT'

  • os.hostname()
    Returns the host name of the operating system as a string
    eg:

       > os.hostname()
       'ocean-VirtualBox'
  • os.release()
    Returns the distribution of the operating system as a string
    eg:

       > os.release()
       '4.4.0-142-generic'
  • os.platform()
    Return Node.js compile-time operating system platform as a string
    Equivalent to process.platform
    eg:

       > os.platform()
       'linux'
  • os.endianness()
    Returns the byte order of the Node.js binary compilation environment as a string
    eg:

       > os.endianness()
       'LE'
  • os.uptime()
    Return to the operating system's online time in a few seconds
    eg:
  • os.totalmem()
    Returns the number of bytes of all system memory in integer form
    eg:

       > os.totalmem()
       1040474112
  • os.freemem()
    Number of bytes returned to idle system memory in integer form
    eg:

       > os.freemem()
       100966400
  • os.tmpdir()
    Returns the default temporary file directory of the operating system as a string.
    eg:

       > os.tmpdir()
       '/tmp'
  • os.homedir()
    Returns the current user's home directory as a string

       > os.homedir()
       '/root'
  • os.getPriority([pid])
    Returns the planned priority of the process specified by pid
    pid default value: 0.
    eg:

       > os.getPriority()
       0
  • os.setPriority([pid, ]priority)
    Setting the schedule priority of the process specified by pid
    If pid is not provided, or 0, the priority of the current process is used
    Priority must be an integer between - 20 (high priority) and 19 (low priority)
  • os.arch()
    Returns a string indicating the operating system CPU architecture used for Node.js binary compilation
    Equivalent to process.arch
    Possible values:'arm','arm64','ia32','mips','mipsel','ppc','ppc64','s390','s390x','x32','x64'
    eg:

       > os.arch()
       'x64'
  • os.cpus()
    Returns an array of objects containing information about each logical CPU kernel.
    eg:

       > os.cpus()
       [ { 
           model: 'Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz',
           speed: 1800,
           times:{ user: 4382300, nice: 22200, sys: 679200, idle: 20595000, irq: 0 } 
       } ]
    
  • os.loadavg()
    Returns an array containing an average load of 1, 5, and 15 minutes.
    eg:

       > os.loadavg()
       [ 0.71142578125, 0.47509765625, 0.224609375 ]
  • os.networkInterfaces()
    Returns an object containing a network interface that has only been assigned a network address
    internal: If the network interface is loopback or similar remote unavailable interface, the value is true, otherwise false
    scopeid: IPv6 Digital Domain Identification Code (available only when family is IPv6)
    CIDR: IPv4 or IPv6 addresses with routing prefixes allocated in CIDR notation. If the netmask parameter is not available, the attribute is null.
    eg:

       {
         lo: [
           {
             address: '127.0.0.1',
             netmask: '255.0.0.0',
             family: 'IPv4',
             mac: '00:00:00:00:00:00',
             internal: true,
             cidr: '127.0.0.1/8'
           },
           {
             address: '::1',
             netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
             family: 'IPv6',
             mac: '00:00:00:00:00:00',
             internal: true,
             cidr: '::1/128'
           }
         ],
         eth0: [
           {
             address: '192.168.1.108',
             netmask: '255.255.255.0',
             family: 'IPv4',
             mac: '01:02:03:0a:0b:0c',
             internal: false,
             cidr: '192.168.1.108/24'
           },
           {
             address: 'fe80::a00:27ff:fe4e:66a1',
             netmask: 'ffff:ffff:ffff:ffff::',
             family: 'IPv6',
             mac: '01:02:03:0a:0b:0c',
             internal: false,
             cidr: 'fe80::a00:27ff:fe4e:66a1/64'
           }
         ]
       }
    
  • os.userInfo([options])
    Returns information about the current valid user
    On POSIX platform:

       > os.userInfo()
       { 
           uid: 0,
           gid: 0,
           username: 'root',
           homedir: '/root',
           shell: '/bin/bash' 
       }

    On Windows: uid and gid are - 1, shell is null

       > os.userInfo()
       { 
         uid: -1,
         gid: -1,
         username: 'yangy',
         homedir: 'C:\\Users\\yangy',
         shell: null 
       }
       
    • os.EOL
      Returns the end-of-line flags associated with the operating system as strings
      On the POSIX system, the value is n:
      On Windows, the value is: rn
  • os.constants
    Returns an object containing generic OS-specific constants such as error codes, signal processing, etc.
    eg:

       > os.constants
       [Object: null prototype] {
           UV_UDP_REUSEADDR: 4,//libuv constant
           dlopen: [Object: null prototype] {//dlopen constant
               RTLD_LAZY: 1,
               RTLD_NOW: 2,
               RTLD_GLOBAL: 256,
               RTLD_LOCAL: 0,
               RTLD_DEEPBIND: 8
           },
           errno: [Object: null prototype] {//Error constant
               E2BIG: 7,
               EACCES: 13,
               EADDRINUSE: 98,
               EADDRNOTAVAIL: 99,
               EAFNOSUPPORT: 97,
               EAGAIN: 11,
               EALREADY: 114,
               EBADF: 9,
               EBADMSG: 74,
               EBUSY: 16,
               ECANCELED: 125,
               ECHILD: 10,
               ECONNABORTED: 103,
               ECONNREFUSED: 111,
               ECONNRESET: 104,
               EDEADLK: 35,
               EDESTADDRREQ: 89,
               EDOM: 33,
               EDQUOT: 122,
               EEXIST: 17,
               EFAULT: 14,
               EFBIG: 27,
               EHOSTUNREACH: 113,
               EIDRM: 43,
               EILSEQ: 84,
               EINPROGRESS: 115,
               EINTR: 4,
               EINVAL: 22,
               EIO: 5,
               EISCONN: 106,
               EISDIR: 21,
               ELOOP: 40,
               EMFILE: 24,
               EMLINK: 31,
               EMSGSIZE: 90,
               EMULTIHOP: 72,
               ENAMETOOLONG: 36,
               ENETDOWN: 100,
               ENETRESET: 102,
               ENETUNREACH: 101,
               ENFILE: 23,
               ENOBUFS: 105,
               ENODATA: 61,
               ENODEV: 19,
               ENOENT: 2,
               ENOEXEC: 8,
               ENOLCK: 37,
               ENOLINK: 67,
               ENOMEM: 12,
               ENOMSG: 42,
               ENOPROTOOPT: 92,
               ENOSPC: 28,
               ENOSR: 63,
               ENOSTR: 60,
               ENOSYS: 38,
               ENOTCONN: 107,
               ENOTDIR: 20,
               ENOTEMPTY: 39,
               ENOTSOCK: 88,
               ENOTSUP: 95,
               ENOTTY: 25,
               ENXIO: 6,
               EOPNOTSUPP: 95,
               EOVERFLOW: 75,
               EPERM: 1,
               EPIPE: 32,
               EPROTO: 71,
               EPROTONOSUPPORT: 93,
               EPROTOTYPE: 91,
               ERANGE: 34,
               EROFS: 30,
               ESPIPE: 29,
               ESRCH: 3,
               ESTALE: 116,
               ETIME: 62,
               ETIMEDOUT: 110,
               ETXTBSY: 26,
               EWOULDBLOCK: 11,
               EXDEV: 18
           },
           signals: [Object: null prototype] {//Signal constant
               SIGHUP: 1,//Send to indicate when the control terminal closes or the parent process exits.
               SIGINT: 2,
               SIGQUIT: 3,
               SIGILL: 4,
               SIGTRAP: 5,
               SIGABRT: 6,
               SIGIOT: 6,
               SIGBUS: 7,
               SIGFPE: 8,
               SIGKILL: 9,
               SIGUSR1: 10,
               SIGSEGV: 11,
               SIGUSR2: 12,
               SIGPIPE: 13,
               SIGALRM: 14,
               SIGTERM: 15,
               SIGCHLD: 17,
               SIGSTKFLT: 16,
               SIGCONT: 18,
               SIGSTOP: 19,
               SIGTSTP: 20,
               SIGTTIN: 21,
               SIGTTOU: 22,
               SIGURG: 23,
               SIGXCPU: 24,
               SIGXFSZ: 25,
               SIGVTALRM: 26,
               SIGPROF: 27,
               SIGWINCH: 28,
               SIGIO: 29,
               SIGPOLL: 29,
               SIGPWR: 30,
               SIGSYS: 31,
               SIGUNUSED: 31
           },
           priority: [Object: null prototype] {//Priority Constant
               PRIORITY_LOW: 19,//Minimum process scheduling priority, corresponding to IDLE_PRIORITY_CLASS on Windows
               PRIORITY_BELOW_NORMAL: 10,//Process scheduling priority is between PRIORITY_LOW and PRIORITY_NORMAL, corresponding to BELOW_NORMAL_PRIORITY_CLASS on Windows
               PRIORITY_NORMAL: 0,//Default process scheduling priority corresponding to NORMAL_PRIORITY_CLASS on Windows
               PRIORITY_ABOVE_NORMAL: -7,//The priority of process scheduling is between PRIORITY_NORMAL and PRIORITY_HIGH, corresponding to ABOVE_NORMAL_PRIORITY_CLASS on Windows.
               PRIORITY_HIGH: -14,//The priority of process scheduling is between PRIORITY_ABOVE_NORMAL and PRIORITY_HIGHEST, corresponding to HIGH_PRIORITY_CLASS on Windows.
               PRIORITY_HIGHEST: -20 //The highest priority for process scheduling, corresponding to REALTIME_PRIORITY_CLASS on Windows
           }
       }
    

Tags: node.js Windows Linux Mac network

Posted on Mon, 22 Apr 2019 14:21:33 -0700 by big-dog1965