Linux查看已知的进程对应目录

Linux查看已知的进程对应目录,第1张

还是我来告诉你吧!你打开终端》输入命令ps-aux|greptesttest是你要找的进程的关键字。然后在最末尾就是你要找的进程名,如果这个名字是带有斜杠的,那么就是绝对路径,你可以直接去找到这个程序。如果是没有斜杠的,那么一般在/usr/bin/目录下会有叫这个名字的程序。

1.启动子与父目录的CWD:

var exec = require('child_process').exec

var path = require('path')

var parentDir = path.resolve(process.cwd(), '..')

exec('doSomethingThere', {cwd: parentDir}, function (error, stdout, stderr) {

// if you also want to change current process working directory:

process.chdir(parentDir)

})

更新:如果你想找回子的CWD:

var fs = require('fs')

var os = require('os')

var exec = require('child_process').exec

function getCWD(pid, callback) {

switch (os.type()) {

case 'Linux':

fs.readlink('/proc/' + pid + '/cwd', callback)break

case 'Darwin':

exec('lsof -a -d cwd -p ' + pid + ' | tail -1 | awk \'{print $9}\'', callback)

break

default:

callback('unsupported OS')

}

}

// start your child process

// note that you can't do like this, as you launch shell process

// and shell's child don't change it's cwd:

// var child1 = exec('cd .. &sleep 1 &&cd .. sleep 1')

var child1 = exec('some process that changes cwd using chdir syscall')

// watch it changing cwd:

var i = setInterval(getCWD.bind(null, child1.pid, console.log), 100)

child1.on('exit', clearInterval.bind(null, i))

2.

如果你想获得当前的工作目录,而不诉诸 *** 作系统行实用程序,你的“作战测试”shelljs库,这些抽象的东西给你,而子进程。

var sh = require("shelljs")

var cwd = sh.pwd()

有你有它的变量CWD握着你的当前工作目录,无论你在Linux,Windows CodeGo.net,或FreeBSD。

3.

只是一个想法,如果你知道子进程的PID,并pwdx安装(有可能在Linux上),你可以从一个节点得到子的CWD执行。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7154381.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-02
下一篇 2023-04-02

发表评论

登录后才能评论

评论列表(0条)

保存