Linux create process with name

Giving a process a specific name in GNU/Linux?

How do I launch a process so that it would have a specific identifier in ps command? UPD: I have a couple of servers running on one box. Most of them are in deployment mode. However one is in development. All servers are Pythonic and the disease is that all the servers are listed as python in ps , which makes it impossible to killall the one in development leaving others intact. UPD: As David and GNUix suggested I created symlinks to Python interpreter and bash scripts for each server.

4 Answers 4

Creating a symlink should do the trick, however, it would be more helpful if we knew the disease and not the symptom. What exactly are you trying to do? Because there may be a better way

gnuix@defiant)-(08:11pm-:-06/10)-- (~)./0012 my weird program name PID TTY TIME CMD 7805 pts/1 00:00:00 zsh 14020 pts/1 00:00:00 0012 14021 pts/1 00:00:00 ps (gnuix@defiant)-(08:11pm-:-06/10)-- (~)ln -s 0012 weird (gnuix@defiant)-(08:12pm-:-06/10)-- (~)./weird my weird program name PID TTY TIME CMD 7805 pts/1 00:00:00 zsh 14046 pts/1 00:00:00 weird 14047 pts/1 00:00:00 ps (gnuix@defiant)-(08:12pm-:-06/10)-- (~) 

Update: Based on the extra information you could (I’m sure there is an easier way but its not comming to me at the moment) have your servers write out their PID to a file upon startup, then you could kill -9 | cat /var/run/devserver.pid but then that would be a programming question 🙂

Update again: You could also do some shell trickery to get the PID of the servers when you launch them, off the top of my head you could create a shell function called startmyserver for example that would wrap the command you use to start your servers but also capture $! to a file based upon startup name — then we are talking about shell scripting which is ‘inbounds’ @ ServerFault. 🙂

Источник

How to create a process in Linux?

A new process can be created by the fork() system call. The new process consists of a copy of the address space of the original process. fork() creates new process from existing process. Existing process is called the parent process and the process is created newly is called child process. The function is called from parent process. Both the parent and the child processes continue execution at the instruction after the fork(), the return code for the fork() is zero for the new process, whereas the process identifier of the child is returned to the parent.

Fork() system call is situated in library.

System call getpid() returns the Process ID of the current process and getppid() returns the process ID of the current process’s parent process.

Example

Let’s take an example how to create child process using fork() system call.

#include #include #include int main( ) < pid_t child_pid; child_pid = fork (); // Create a new child process; if (child_pid < 0) < printf("fork failed"); return 1; >else if (child_pid == 0) < printf ("child process successfully created!
"); printf ("child_PID = %d,parent_PID = %d
", getpid(), getppid( ) ); > else < wait(NULL); printf ("parent process successfully created!
"); printf ("child_PID = %d, parent_PID = %d", getpid( ), getppid( ) ); > return 0; >

Output

child process successfully created! child_PID = 31497, parent_PID = 31496 parent process successfully created! child_PID = 31496, parent_PID = 31491

Here, getppid() in the child process returns the same value as getpid() in the parent process.

Читайте также:  Magnet link on linux

pid_t is a data type which represents the process ID. It is created for process identification. Each process has a unique ID number. Next, we call the system call fork() which will create a new process from calling process. Parent process is the calling function and a new process is a child process. The system call fork() is returns zero or positive value if the process is successfully created.

Источник

Create my own process name and show in Top Linux [duplicate]

enter image description here

I Learning about process Linux and I know how to create a simple hello world in c. but I want to create a program (process) with the name I choose and show in Top (Time Process Monitor) Example: Show my program name in column COMMAND I want to monitor individual threads of the program once they are created, I would like to see the details (e.g., CPU/memory usage) of individual threads. ¿What is the procedure to accomplish this? Thanks for reading

What have you tried this far? It’s expected that you show some effort of your own in terms of actual code. Please, consider taking a look at some guidelines about asking questions.

1 Answer 1

All processes running on Linux system will be displayed by the top command. It is the name of the executable in the COMMAND column. If you want specific name, rename your program to that specific name and run it. The top command displays limited number of processes by default. If your program is not consuming lot of resource then it will not there in the default list. You can use -n parameter to increase the number of processes displayed to list all processes.

In your hello world program just add a sleep statement(e.g. sleep(60)) and run it. Then check in the «top -n 1000» output.

Linked

Hot Network Questions

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How can I start a process with a different name?

Basically I want to dynamically start some processes which may create their own children processes, also I want to kill a certain group of processes I just created whenever I want. One way I could think of is to start processes with a name (to distinguish as a group), then use pkill to kill them by the name. The question is how to start a process with a name so that I can use pkill to kill them by the name? I am open to other solutions as well.

Are you asking how to start a process with a name different from what it usually uses? Or are you just asking how to start a process from the command-line?

The process name is shown the same as its file name by default in Linux. I dont want to use its default file name as its process name nor change the file name every time I run. So I want to start the processes with a specific name that I want so that I can use ‘pkill -f MyOwnName’ to kill all the processes that I just started named MyOwnName at once .

Читайте также:  Login as root kali linux

3 Answers 3

You can use the exec shell builtin:

bash -c "exec -a MyUniqueProcessName &" 

replaces the current shell, no new process is created, that’s why I’m starting a new shell to call exec .

Then you can kill the process with:

pkill -f MyUniqueProcessName 

You can start more than one process under the same name, then pkill -f will kill all of them.

@VNVN you can’t. If a command has been run, it has run. To «undo» it, you need to know exactly what it did and manually undo that, if possible. There is not Ctrl+Z when running commands.

+1. my observation is: the name used for exec -a name sleep 100 shows up when i do ps -ef but not when i do pstree -p .

@TrevorBoydSmith, it does not show in ps -e , shows in ps -ef . The answer IMO should start with disclaimer that is does not change process name, but still can be used with pkill via adding -f / —full option

AMItac / The Solaris Agency

I had an issue with an audio transcoding tool that ran 3 times under the same name.

I did following. Went to the bin directory and copyied the transcoding script 3 times and gave each one a new name. tc-1 , tc-2 , tc-3 (it´s a small tool so it doesn’t eat much drive space (with large binaries you shouldn’t use this method)

so the process started with an unique Name and can be killed with this unique Name without the danger of killing other transcoding processes i want to continue.

add a #bash script Name.sh, make it executable. Type in your commands there and start the bash script itself. On Centos it uses then the Bashscript Name you excecuted and not the bin Name itself.

Hope something helps someone out there.

Источник

Linux CreateProcess?

I developing on the Linux platform. I want to create a new proccess in my library without replacing the current executing image. Because I am developing a library, I don’t have a main function. And I want to continue the new process after the invoker application closes (Just like CreateProcess Windows API). Is it possible in Linux or not? something like this function:

void Linux_CreateProcess(const char* app_name) < // Executing app_name. // . what is the code . // app_name is running and never close if current application close. return; >
  • system() blocks the current process, it is not good. I want to continue the current process.
  • exec() family replace the current executing image, it is not good.
  • popen() closes the new process if the current process closed.

8 Answers 8

The fork / exec combination was already mentioned, but there is also the posix_spawn family of functions that can be used as a replacement for fork + exec and is a more direct equivalent to CreateProcess . Here is an example for both possibilities:

#include #include #include #include #include #include extern char **environ; void test_fork_exec(void); void test_posix_spawn(void); int main(void) < test_fork_exec(); test_posix_spawn(); return EXIT_SUCCESS; >void test_fork_exec(void) < pid_t pid; int status; puts("Testing fork/exec"); fflush(NULL); pid = fork(); switch (pid) < case -1: perror("fork"); break; case 0: execl("/bin/ls", "ls", (char *) 0); perror("exec"); break; default: printf("Child id: %i\n", pid); fflush(NULL); if (waitpid(pid, &status, 0) != -1) < printf("Child exited with status %i\n", status); >else < perror("waitpid"); >break; > > void test_posix_spawn(void) < pid_t pid; char *argv[] = ; int status; puts("Testing posix_spawn"); fflush(NULL); status = posix_spawn(&pid, "/bin/ls", NULL, NULL, argv, environ); if (status == 0) < printf("Child id: %i\n", pid); fflush(NULL); if (waitpid(pid, &status, 0) != -1) < printf("Child exited with status %i\n", status); >else < perror("waitpid"); >> else < printf("posix_spawn: %s\n", strerror(status)); >> 

posix_spawn() seems to return 0 (success) even if the executable path does not exist, which is not very helpful.

Читайте также:  Run linux kernel on windows

posix_spawn is probably the preferred solution these days.

Before that fork() and then execXX() was the way to do this (where execXX is one of the exec family of functions, including execl , execlp , execle , execv , execvp , and execvpe ). In the GNU C library currently, at least for Linux, posix_spawn is implemented via fork/exec anyway; Linux doesn’t have a posix_spawn system call.

You would use fork() (or vfork() ) to launch a separate process, which will be a clone of the parent. In both the child and parent process, execution continues, but fork returns a different value in either case allowing you to differentiate. You can then use one of the execXX() functions from within the child process.

Note, however, this problem — text borrowed from one of my blog posts (http://davmac.wordpress.com/2008/11/25/forkexec-is-forked-up/):

There doesn’t seem to be any simple standards-conformant way (or even a generally portable way) to execute another process in parallel and be certain that the exec() call was successful. The problem is, once you’ve fork()d and then successfully exec()d you can’t communicate with the parent process to inform that the exec() was successful. If the exec() fails then you can communicate with the parent (via a signal for instance) but you can’t inform of success – the only way the parent can be sure of exec() success is to wait() for the child process to finish (and check that there is no failure indication) and that of course is not a parallel execution.

i.e. if execXX() succeeds, you no longer have control so can’t signal success to the original (parent) process.

A potential solution to this problem, in case it is an issue in your case:

[. ] use pipe() to create a pipe, set the output end to be close-on-exec, then fork() (or vfork()), exec(), and write something (perhaps errno) to the pipe if the exec() fails (before calling _exit()). The parent process can read from the pipe and will get an immediate end-of-input if the exec() succeeds, or some data if the exec() failed.

(Note that this solution through is prone to causing priority inversion if the child process runs at a lower priority than the parent, and the parent waits for output from it).

There is also posix_spawn as mentioned above and in other answers, but it doesn’t resolve the issue of detecting failure to execute the child executable, since it is often implemented in terms of fork/exec anyway and can return success before the exec() stage fails.

Источник

Оцените статью
Adblock
detector