Linux: Rename the process displayed by the ps command

--A function called setproctitle is available on some systems --Linux does not have setproctitle by default. It is troublesome because there is no choice but to rewrite ʻargv [0] --If you write a command name longer than the original, it will destroy the area with environment variables after ʻargv [1], so you need to copy it to another location in advance. --util-linux has a setproctitle implementation for Linux, so you can easily copy and paste this ――However, please note that it is GPL.

Postscript: When I searched for it, redis also had an implementation. Redis is BSD licensed.

test.c


/*
 *  set process title for ps (from sendmail)
 *
 *  Clobbers argv of our main procedure so ps(1) will display the title.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

//#include "setproctitle.h"

#ifndef SPT_BUFSIZE
# define SPT_BUFSIZE     2048
#endif

extern char **environ;

static char **argv0;
static int argv_lth;

void initproctitle (int argc, char **argv)
{
  int i;
  char **envp = environ;

  /*
   * Move the environment so we can reuse the memory.
   * (Code borrowed from sendmail.)
   * WARNING: ugly assumptions on memory layout here;
   *          if this ever causes problems, #undef DO_PS_FIDDLING
   */
  for (i = 0; envp[i] != NULL; i++)
    continue;

  environ = malloc(sizeof(char *) * (i + 1));
  if (environ == NULL)
    return;

  for (i = 0; envp[i] != NULL; i++)
    if ((environ[i] = strdup(envp[i])) == NULL)
      return;
  environ[i] = NULL;

  argv0 = argv;
  if (i > 0)
    argv_lth = envp[i-1] + strlen(envp[i-1]) - argv0[0];
  else
    argv_lth = argv0[argc-1] + strlen(argv0[argc-1]) - argv0[0];
}

void setproctitle (const char *prog, const char *txt)
{
        int i;
        char buf[SPT_BUFSIZE];

        if (!argv0)
                return;

  if (strlen(prog) + strlen(txt) + 5 > SPT_BUFSIZE)
    return;

  sprintf(buf, "%s -- %s", prog, txt);

        i = strlen(buf);
        if (i > argv_lth - 2) {
                i = argv_lth - 2;
                buf[i] = '\0';
        }
  memset(argv0[0], '\0', argv_lth);       /* clear the memory area */
        strcpy(argv0[0], buf);

        argv0[1] = NULL;
}

//Main function for testing
int main(int argc, char* argv[])
{
  initproctitle(argc, argv);
  setproctitle("hoge", "AIUEO");

  printf("Hello my pid = %d\n", getpid());
  sleep(10);
  
  return 0;
}

reference:

postgres ps_status.c Command name change is realized by various methods for each platform.

/*
 * Alternative ways of updating ps display:
 *
 * PS_USE_SETPROCTITLE
 *	   use the function setproctitle(const char *, ...)
 *	   (newer BSD systems)
 * PS_USE_PSTAT
 *	   use the pstat(PSTAT_SETCMD, )
 *	   (HPUX)
 * PS_USE_PS_STRINGS
 *	   assign PS_STRINGS->ps_argvstr = "string"
 *	   (some BSD systems)
 * PS_USE_CHANGE_ARGV
 *	   assign argv[0] = "string"
 *	   (some other BSD systems)
 * PS_USE_CLOBBER_ARGV
 *	   write over the argv and environment area
 *	   (Linux and most SysV-like systems)
 * PS_USE_WIN32
 *	   push the string out as the name of a Windows event
 * PS_USE_NONE
 *	   don't update ps display
 *	   (This is the default, as it is safest.)
 */

Recommended Posts

Linux: Rename the process displayed by the ps command
Linux: Understand the information displayed by the top command
[linux] kill command to kill the process
About the number (section number) in () displayed by the Linux man command
Process confirmation command ps option memorandum
Linux command # 4
Linux command # 3
[Linux] How to use the echo command
How to use the Linux grep command
Linux process
Linux: Fix the address returned by malloc
Notify Slack when the linux command finishes
Linux command # 5
Adjust file permissions with the Linux command chmod
It's a Mac. What is the Linux command Linux?
Linux command list
linux at command
[Linux] Search command
[Linux] How to display CPU usage, display header, and not display grep process with ps command
Linux command <Basic 2>
Linux [directory command]
Linux server command
Linux # Command Memo 1
Linux command [read]
Linux Command Summary
[Basic] linux command
Linux [shell command]
[Linux] Command / Knowledge
My linux command
Linux command <Basic 1>
ps command "wchan"
Linux command collection
Linux mkdir command
Linux command basics
[Linux] Git command
Linux (command memory)
Gradually display the output of the command executed by subprocess.Popen
[Linux] Execute git pull regularly with the crontab command
Linux user addition, how to use the useradd command
About the process that the Linux kernel handles x86 microcode
Multiply PDF by OCR on command line on Linux (Ubuntu)
(Remember quickly) How to use the LINUX command line
Understand the attributes of Linux files (ls -l command)
Workaround when Authentication Required is displayed when trying to view the HDD by USB booting Linux