在进程打开时获取进程的PID

时间:2023-01-26 16:44:12

How do I get the pid of a process as soon as it opens. Like lets say we run ./file.pl and then ./file2.pl As both these files will create a pid in /proc/ folder. How do I instantly know if the process has been created when the executable is run. I have a file with all the commands ready to be run as soon as it gets the green signal that there is a new process in the /proc/ folder. How do I do that? EDIT: Please don't answer with a shell command. I don't need to know the pid. I need to develop a script which can know right away that we have a guest in the proc department

如何在流程打开时立即获取流程的pid。比如说我们运行./file.pl然后运行./file2.pl因为这两个文件都会在/ proc /文件夹中创建一个pid。如何在运行可执行文件时立即知道是否已创建进程。我有一个文件,一旦获得绿色信号表明/ proc /文件夹中有一个新进程,就可以运行所有命令。我怎么做?编辑:请不要使用shell命令回答。我不需要知道pid。我需要开发一个脚本,它可以立即知道我们在proc部门有一位客人

4 个解决方案

#1


1  

If you start the process via a shell, then start process in background:

如果您通过shell启动该过程,则在后台启动进程:

./your_prog &

Get the pid:

得到pid:

echo $!

#2


0  

If the script give you the shell prompt back, you can do :

如果脚本返回shell提示符,则可以执行以下操作:

./your_prog
pidof -x your_prog

Tested OK with this perl script :

使用此perl脚本测试好了:

#!/usr/bin/perl

if (fork() == 0) {
    sleep(600);
}

you need to

你需要

chmod +x your_prog

before...

之前...

#3


0  

Every process can get its own pid with the getpid(2) syscall. At process creation by fork(2) the parent process (e.g. some shell) gets the pid of the new child process. Read e.g. Advanced Linux Programming for more. And the kernel (not the program) is creating some subdirectory /proc/1234/ see proc(5) as soon as it creates the process of pid 1234.

每个进程都可以通过getpid(2)系统调用获得自己的pid。在fork(2)创建进程时,父进程(例如某个shell)获取新子进程的pid。阅读例如高级Linux编程更多。并且内核(而不是程序)在创建pid 1234的过程后立即创建一些子目录/ proc / 1234 / see proc(5)。

Actually, /proc/ is not a real file system. It is just a pseudo file system giving a view on the state of the kernel and the entire Linux system.

实际上,/ proc /不是真正的文件系统。它只是一个伪文件系统,可以查看内核状态和整个Linux系统。

Perl gives you its POSIX module to interface the syscalls. The getpid() syscall is interfaced using the $PID or $$ Perl variable.

Perl为您提供了POSIX模块来连接系统调用。 getpid()系统调用使用$ PID或$$ Perl变量进行接口。

The /proc/ pseudo filesystem is filled by the kernel. You could perhaps use inotify to follow change in /proc/ but this is very probably a bad idea.

/ proc / pseudo文件系统由内核填充。您也许可以使用inotify来跟踪/ proc /中的更改,但这可能是一个坏主意。

Your question is not clear, we cannot understand what you really want to do and what you have tried.

你的问题不明确,我们无法理解你真正想做什么和你尝试过什么。

#4


0  

Try out below shell script.(You may have to include changes in below script for your expected output)

试试下面的shell脚本。(你可能必须在下面的脚本中包含对你预期输出的更改)

#!/bin/bash
nr_proc_before=`ls -l /proc | wc -l`
ls /proc > proc_list_before

./any_executable &

nr_proc_after=`ls -l /proc | wc -l`
ls /proc > proc_list_after
nr_new=`expr $nr_proc_after - $nr_proc_before`

echo "$nr_new processes are created newly"
echo "new processes pids are :"
diff proc_list_after proc_list_before > new_pids
sed "1d" new_pids

if [ nr_new > 0 ] ; then
   #trigger your file which has commands.
fi

Insted of any_execuatble you can replace with your things so that new processes will be created.

在any_execuatble中,你可以用你的东西替换,以便创建新的进程。

Note : This is not a script which monitors for new process. This sample of script may give you idea to solve your problem. Please do reply for this answer, i can redefine my answer.

注意:这不是监视新进程的脚本。这个脚本示例可以帮助您解决问题。请回答这个答案,我可以重新定义我的答案。

#1


1  

If you start the process via a shell, then start process in background:

如果您通过shell启动该过程,则在后台启动进程:

./your_prog &

Get the pid:

得到pid:

echo $!

#2


0  

If the script give you the shell prompt back, you can do :

如果脚本返回shell提示符,则可以执行以下操作:

./your_prog
pidof -x your_prog

Tested OK with this perl script :

使用此perl脚本测试好了:

#!/usr/bin/perl

if (fork() == 0) {
    sleep(600);
}

you need to

你需要

chmod +x your_prog

before...

之前...

#3


0  

Every process can get its own pid with the getpid(2) syscall. At process creation by fork(2) the parent process (e.g. some shell) gets the pid of the new child process. Read e.g. Advanced Linux Programming for more. And the kernel (not the program) is creating some subdirectory /proc/1234/ see proc(5) as soon as it creates the process of pid 1234.

每个进程都可以通过getpid(2)系统调用获得自己的pid。在fork(2)创建进程时,父进程(例如某个shell)获取新子进程的pid。阅读例如高级Linux编程更多。并且内核(而不是程序)在创建pid 1234的过程后立即创建一些子目录/ proc / 1234 / see proc(5)。

Actually, /proc/ is not a real file system. It is just a pseudo file system giving a view on the state of the kernel and the entire Linux system.

实际上,/ proc /不是真正的文件系统。它只是一个伪文件系统,可以查看内核状态和整个Linux系统。

Perl gives you its POSIX module to interface the syscalls. The getpid() syscall is interfaced using the $PID or $$ Perl variable.

Perl为您提供了POSIX模块来连接系统调用。 getpid()系统调用使用$ PID或$$ Perl变量进行接口。

The /proc/ pseudo filesystem is filled by the kernel. You could perhaps use inotify to follow change in /proc/ but this is very probably a bad idea.

/ proc / pseudo文件系统由内核填充。您也许可以使用inotify来跟踪/ proc /中的更改,但这可能是一个坏主意。

Your question is not clear, we cannot understand what you really want to do and what you have tried.

你的问题不明确,我们无法理解你真正想做什么和你尝试过什么。

#4


0  

Try out below shell script.(You may have to include changes in below script for your expected output)

试试下面的shell脚本。(你可能必须在下面的脚本中包含对你预期输出的更改)

#!/bin/bash
nr_proc_before=`ls -l /proc | wc -l`
ls /proc > proc_list_before

./any_executable &

nr_proc_after=`ls -l /proc | wc -l`
ls /proc > proc_list_after
nr_new=`expr $nr_proc_after - $nr_proc_before`

echo "$nr_new processes are created newly"
echo "new processes pids are :"
diff proc_list_after proc_list_before > new_pids
sed "1d" new_pids

if [ nr_new > 0 ] ; then
   #trigger your file which has commands.
fi

Insted of any_execuatble you can replace with your things so that new processes will be created.

在any_execuatble中,你可以用你的东西替换,以便创建新的进程。

Note : This is not a script which monitors for new process. This sample of script may give you idea to solve your problem. Please do reply for this answer, i can redefine my answer.

注意:这不是监视新进程的脚本。这个脚本示例可以帮助您解决问题。请回答这个答案,我可以重新定义我的答案。