Node-launchctl

Native bindings to launchctl commands for node

View project onGitHub

node-launchctl

Native bindings for launchctl commands for node.js

LaunchCTL.listSync(name:String)

launchctl list

Examples:

List All

   try {
     var res = ctl.listSync()
   }
   catch (e) {
     throw e
   }

List specific job

   try {
     var res = ctl.listSync('com.apple.Dock.agent')
   }
   catch (e) {
     throw e
   }

List by regular expression

   try {
     var res = ctl.listSync(/com.apple.(.*)/)
   }
   catch (e) {
     throw e
   }

LaunchCTL.list(name:String, cb:Function)

launchctl list

Examples:

List all

   ctl.list(function(err, jobs) {
     if (err) throw err
     console.log(jobs)
   })

List specific job

   ctl.list('com.apple.Dock.agent', function(err, job) {
     if (err) throw err
     console.log(job)
   })

List by regex

   ctl.list(/com.apple.(.*)/, function(err, jobs) {
     if (err) throw err
     console.log(jobs)
   })

LaunchCTL.startSync(label:String)

Start job with the given label launchctl start

Examples:

   try {
     ctl.startSync('com.apple.Dock.agent')
   }
   catch (e) {
     throw e
   }

LaunchCTL.start(label:String, cb:Function)

Start job with the given label launchctl start

Examples:

   ctl.start('com.apple.Dock.agent', function(err) {
     if (err) throw err
     // Your code
   })

LaunchCTL.stopSync(label:String)

Stop job with the given label launchctl stop

Examples:

   try {
     var res = ctl.stopSync('com.apple.Dock.agent')
   }
   catch (e) {
     throw e
   }

Note: Keep in mind what launchd actually does on a job that has the KeepAlive set to true. It will simply restart the job, not actually stop it

LaunchCTL.stop(label:String, cb:Function)

Stop job with the given label launchctl stop

Examples:

   ctl.stop('com.apple.Dock.agent', function(err) {
     if (err) throw err
     // Your code
   })

Note: Keep in mind what launchd actually does on a job that has the KeepAlive set to true. It will simply restart the job, not actually stop it

LaunchCTL.removeSync(label:String)

Removes a job with the given label from launchd Equivalent to launchctl remove

Examples:

   try {
     ctl.removeSync('com.jobname.test')
   }
   catch (e) {
     throw e
   }

LaunchCTL.remove(label:String, cb:Function)

Removes a job with the given label from launchd Equivalent to launchctl remove

Examples:

   ctl.remove('com.jobname.test', function(err) {
     if (err) throw err
     // Your code
   })

LaunchCTL.loadSync(path:String, opts:Object)

Loads a job launchctl load

Examples:

   try {
     ctl.loadSync('/System/Library/...', {
       editondisk: false, // default
       forceload: false, // default
       session_type: 'Aqua',
       domain: 'user'
     })
   catch (e) {
     throw e
   }

LaunchCTL.load(path:String, opts:Object, cb:Function)

Loads a job

Examples:

   ctl.load('/System/Library/...', {
     editondisk: false,
     forceload: false,
     session_type: 'Aqua',
     domain: 'user'
   }, function(err) {
     if (err) throw err
     // Your code
   })

LaunchCTL.unloadSync(path:String, opts:Object)

Unload job at given path

Examples:

   try {
     ctl.unloadSync('/System/Library/...', {
       editondisk: false,
       forceload: false,
       session_type: 'Aqua',
       domain: 'user'
     })
   }
   catch (e) {
     throw e
   }

LaunchCTL.unload(path:String, opts:Object, cb:Function)

Unloads a job

Examples:

   ctl.unload('/System/Library/..', {
     editondisk: false,
     forceload: false,
     session_type: 'Aqua',
     domain: 'user'
   }, function(err) {
     if (err) throw err
     // Your code
   })

LaunchCTL.submitSync(args:Object)

Submit a job

Examples:

   try {
     ctl.submitSync({
         label: 'com.test.label'
       , program: '/bin/ls'
       , stderr: '/var/log/test.err.log'
       , stdout: '/var/log/test.out.log'
       , args: ['-l', '-a', '-h']
     })
   }
   catch(e) {
     throw e
   }

LaunchCTL.submit(data:Object, cb:Function)

Submit a job

Examples:

   ctl.submit({
       label: 'com.test.label'
     , program: '/bin/ls'
     , stderr: '/var/log/test.err.log'
     , stdout: '/var/log/test.out.log'
     , args: ['-l', '-a', '-h']
   }, function(err) {
     if (err) {
       console.log(err)
     } else {
       console.log('Success')
     }
   })

LaunchCTL.managername()

Gets the name of the current manager (session) launchctl managername

Examples:

   var name = ctl.managername()
   // => 'Aqua'

LaunchCTL.manageruid()

Gets the uid of the current manager launchctl manageruid

Examples:

   var uid = ctl.manageruid()
   // => 501

LaunchCTL.managerpid()

Gets the pid of the current manager launchctl managerpid

Examples:

   var pid = ctl.managerpid()
   // => 263

LaunchCTL.limit(limtype:String, soft:String|Number, hard:String|Number)

Gets/sets the launchd resource limits

Examples:

   var limits = ctl.limit()
   // => {
   // =>   cpu: { soft: 'unlimited', hard: 'unlimited' },
   // =>   filesize: { soft: 'unlimited', hard: 'unlimited' },
   // =>   data: { soft: 'unlimited', hard: 'unlimited' },
   // =>   stack: { soft: '8388608', hard: '67104768' },
   // =>   core: { soft: '0', hard: 'unlimited' },
   // =>   rss: { soft: 'unlimited', hard: 'unlimited' },
   // =>   memlock: { soft: 'unlimited', hard: 'unlimited' },
   // =>   maxproc: { soft: '1000', hard: '2000' },
   // =>   maxfiles: { soft: '8192', hard: '20480' }
   // => }

Get maxproc

   var res = ctl.limit('maxproc')
   // => { soft: '1000', hard: '2000' }

Set maxproc limit

   var res = ctl.limit('maxproc', '1200', '2000')
   // => 0

LaunchCTL.setEnvVar(key:String, val:String)

Sets a launchd environment variable

LaunchCTL.unsetEnvVar(key:String)

Unsets a launchd environment variable

LaunchCTL.getEnvVar(key:String)

Gets an Environment Variable (or all of them)

If no key is passed, it will return an Object If key is passed, if the key exists, it will return a string If key is passed, but does not exist, it will return false

LaunchCTL.getRUsage(who:String)

Gets rusage for either self or children

LaunchCTL.umask(arg:String)

Gets or sets the umask

Example:

  var res = ctl.umask('22')
  var res = ctl.umask()

LaunchCTL.Plist

Construct launchctl plist object

See http://evanlucas.github.io/node-launchd.plist

  var plist = new ctl.Plist()