linux如何根据进程pid获取路径
source link: https://blog.popkx.com/how-linux-gets-the-path-based-on-process-pid/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
linux如何根据进程pid获取路径
今天遇到这么一个需求,我希望把正在运行的某个进程结束掉,配置了其他环境后,再重新启动该进程。可是,因为不知道该进程在哪,结束掉就不好再启动。想起查看进程资源这一节,类比了一下方法,发现是可行的。
先编写 demo,用 demo 测试更安全,代码如下:
// 文件名:test.c
#include <stdio.h>
#include "stdint.h"
#include "stdlib.h"
#include <unistd.h>
int main()
{
int i = 0;
while(1){
sleep(10);
printf("test is running...\n");
}
i = 0;
return 0;
}
功能很简单,就是每隔 10 秒在终端打印
test is running...
编译并在后台执行:
$ gcc -o test test.c
$ ./test &
根据进程 pid 获取进程所在路径
首先使用 ps 命令查看该进程 pid:
$ ps -A |grep test
52734 pts/2 00:00:00 test
接着就可以进入进程的资源目录,列出目录文件:
$ cd /proc/52734
$ ls
attr comm fd map_files net pagemap sessionid status
autogroup coredump_filter fdinfo maps ns personality setgroups syscall
auxv cpuset gid_map mem numa_maps projid_map smaps task
cgroup cwd io mountinfo oom_adj root stack timers
clear_refs environ limits mounts oom_score sched stat uid_map
cmdline exe loginuid mountstats oom_score_adj schedstat statm wchan
注意到 exe
,它表示进程的运行,它指向存储在磁盘中的进程文件即路径,只需再执行一次 ls -l
命令即可得到进程路径
$ ls exe -l
lrwxrwxrwx 1 lcc lcc 0 7月 31 20:21 exe -> /lccRoot/xx_workPlace/job/7--preAlloStor/5--embed_fat32/build/test
这样就得到了路径:
/lccRoot/xx_workPlace/job/7--preAlloStor/5--embed_fat32/build/test
阅读更多: Linux笔记
Recommend
-
33
如何根据一个绝对文件路径生成一个相对文件路径 发布于 2018-06-07 19:30 更新于 2020-06-15 09:23 日常的开发中,获取绝...
-
9
Linux PID 1 和 Systemd Linux PID 1 和 Systemd 要说清 Systemd,得先从Linux操作系统的启动说起。Linux 操作系统的启动首先从 BIOS 开始,然后由 Boot Loader 载入内核,并初始化内核。内核初始化的最后一步就是启动 init...
-
8
整理本文,起源是看到知乎上的一个问题, 为什么Linux的进程pid编号极限最大值( process pid...
-
5
Linux namespace in Go - Part 1, UTS and PIDThis article starts some Golang experiments on Linux namespace and provides context for Container technology. Linux namespace is an im...
-
4
前言¶ 前面我们通过 ebpf 程序获取到了执行操作的对应...
-
4
前言# 最近在开发新的项目,使用的是ASP.NET Core6.0版本的框架。由于项目中存在文件下载功能,没有使用类似MinIO或OSS...
-
5
Linux 中如何获取文件的绝对路径 作者:刘光录 2022-11-04 10:49:56 我们介绍了在 Linux 中获取文件完整路径的4种方法,其中 find 和 ls 命令的方式最常见,而 realpath 和 readlink 这两种方法对许多新用户来说可...
-
3
容器通过 Linux Namespace 技术,对网络、PID、用户等等信息的隔离。 从进程的角度来说,你可以在 Host 上看到所有容器内的进程。 或者更准确的说,当你在 Ho...
-
9
【笔记】Linux 上查看 USB 设备的 PID 和 VID 捕捉一只爱折腾的绯鞠 ...
-
6
通过 /proc 查看 Linux 进程的全路径 2023-11-16 tech
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK