How to Set Process Id in Linux for a Specific Program

How to set process ID in Linux for a specific program

Actually, there is a way to do this. Since kernel 3.3 with CONFIG_CHECKPOINT_RESTORE set(which is set in most distros), there is /proc/sys/kernel/ns_last_pid which contains last pid generated by kernel. So, if you want to set PID for forked program, you need to perform these actions:

  1. Open /proc/sys/kernel/ns_last_pid and get fd
  2. flock it with LOCK_EX
  3. write PID-1
  4. fork

Voilà! Child will have PID that you wanted.
Also, don't forget to unlock (flock with LOCK_UN) and close ns_last_pid.

You can checkout C code at my blog here.

How to get only process ID in specify process name in Linux?

You can use:

ps -ef | grep '[j]ava'

Or if pgrep is available then better to use:

pgrep -f java

Changing the process ID (PID) in Linux

No, it's not possible to do such a thing on Unix. You might be able to use fork to achieve this effect, but you have to tell us what you are trying to achieve.

pid_t pid;

/* I want a new PID. */
pid fork();
if (pid == 0) {
/* getpid() will show I've got a new PID. */
else
_exit(0); /* Parent or check for -1 (tinfoil hat)

Obviously, like I said before, you "might" be able to use this.

How to get the process ID of the program that called a system function

When running in kernel code, information about the currently running process is stored in the current global variable (it's actually a platform specific macro rather than a global variable, to be precise), which is a struct task_struct. If you are inside a syscall handler (or a hook to one), then current will be the process which started the syscall, and you can just check current->pid to get its PID.

To get the current process' UID, GID, EUID, EGID (and so on) you can use the set of macros defined in linux/cred.h. From the relative kernel documentation page:

There are convenience wrappers for retrieving specific aspects of a task’s credentials (the value is simply returned in each case):

uid_t current_uid(void)     // Current's real UID
gid_t current_gid(void) // Current's real GID
uid_t current_euid(void) // Current's effective UID
gid_t current_egid(void) // Current's effective GID
/* ... */

Shell script to capture Process ID and kill it if exist

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

Hope it helps.

How to run a program and know its PID in Linux?

Greg's wiki to the rescue:

  • $! is the PID of the last backgrounded process.
  • kill -0 $PID checks whether $PID is still running. Only use this for processes started by the current process or its descendants, otherwise the PID could have been recycled.
  • wait waits for all children to exit before continuing.

Actually, just read the link - It's all there (and more).

$$ is the PID of the current shell.

And yes, each shell will have its own PID (unless it's some homebrewed shell which doesn't fork to create a "new" shell).

How do you get the process ID of a program in Unix or Linux using Python?

Try pgrep. Its output format is much simpler and therefore easier to parse.

Use the PID of a prog as an argument of this same prog

Simplest way is to exec the C program.

#!/bin/bash

# pid of current shell
PID1=$$
echo $PID1

# do stuff

(
# this is a subshell

# $$ doesn't change but BASHPID does
PID2=$BASHPID
echo $PID2

# the pid of this C program will be $PID2
exec myCprogram $(./StringFromPid $PID2)

# exec doesn't return
echo "can't get here"
)

# do more stuff

If you're on a platform that supports it, it may be possible to use the LD_PRELOAD trick to override getpid().

But exec is simpler.



Related Topics



Leave a reply



Submit