Can process be reloaded by PID?
I run a daemon which cannot be restarted via init.d or service command. Is there a way to restart a process just by passing a process id to some command?
Can you clarify what you mean? You want to restart a running process by passing the PID of that process?
yes, exactly. instead of killing a process and retyping a command with lots of params, i’d like to restart a process just by passing it’s PID (or is there something better for doing it then passing a PID?)
Ok, so is there a reason why it can’t be restarted with init.d/service? If the appropriate script doesn’t exist it might be better to write it.
2 Answers 2
Killing or Reconfiguring a Daemon without Restarting
Restarts the process 1721 by sending the hangup signal.
Causes the daemon to reload its config file by sending the hangup signal.
Restarts inetd by sending signal number 1 which is the hangup signal.
The difference between this example and the previous one is the signal is called by name here rather than number.
thanks a lot for the link. it’s what i’ve been looking for. oh, i’ve forgot to mention that i can reload a process like this, but it only works for programs i installed via apt (like apache, mysql). the one i compiled is not affected
It only works for those applications which explicitly setup a SIGHUP signal handler. It is not a general solution to restart processes.
you can use the following command
CMD=`cat /proc/1234/cmdline |sed 's/\x0/ /g'` && kill 1234 && `$CMD` &
where 1234 is the process id.
What this line does is that first it copies the command line that was used to run that process into a variable, then it kills that process and restarts it using the stored command line.
Update: above command line has been updated
How to restart (or reset) a running process in linux
I have two Linux systems communicating over sockets (Desktop and ARM-based development board). I want to restart (or reset) my client application (running on a development board) when server sends a particular predefined message. I don’t want to restart (reboot) Linux, I just want that client application restart itself automatically. I am unable to understand how it should be done.
5 Answers 5
Make your client exec /proc/self/exe when it receives that paticular message. You don’t need to know where the executable actually resides in the file system. And you can reuse main() ‘s argv to construct a new argument vector.
#include #include #include int main(int argc, char **argv) < char buf[32] = <>; char *exec_argv[] = < argv[0], buf, 0 >; int count = argc > 1 ? atoi(argv[1]) : 0; printf("Running: %s %d\n", argv[0], count); snprintf(buf, sizeof(buf), "%d", count+1); sleep(1); execv("/proc/self/exe", exec_argv); /* NOT REACHED */ return 0; >
This restart.c runs like this:
$ gcc restart.c $ ./a.out 3 Running: ./a.out 3 Running: ./a.out 4 Running: ./a.out 5
The normal way to do this is to let your program exit, and use a monitoring system to restart it. The init program offers such a monitoring system. There are many different init programs (SysVinit, BusyBox, Systemd, etc.), with completely different configuration mechanisms (always writing a configuration file, but the location and the syntax of the file differs), so look up the documentation of the one you’re using. Configure init to launch your program at boot time or upon explicit request, and to restart it if it dies. There are also fancier monitoring programs but you don’t sound like you need them. This approach has many advantages over having the program do the restart by itself: it’s standard, so you can restart a bunch of services without having to care how they’re made; it works even if the program dies due to a bug.
There’s a standard mechanism to tell a process to exit: signals. Send your program a TERM signal. If your program needs to perform any cleanup, write a signal handler. That doesn’t preclude having a program-specific command to make it shut down if you have an administrative channel to send it commands like this.
Bash script to start and then shutdown/restart various processes
When I start up my development environment, there are a variety of processes I need to run in the background. It is a pain to start them all individually so I want to write a script that starts each one. I can do this just fine but the problem comes when I need to restart them (something I will need to do regularly). What is the easiest way to capture the process upon starting it and saving that information so that when I run the script again, it will check to see if that information has been saved and then close down those processes before restarting them. I also want the script to be flexible enough such that if I shutdown the process manually, it will a) not throw an error if it can’t find the process and b) not accidentally shut down some other process that has since then shared whatever identifying information I have stored.
Update
- Change to my working directory
- Start faye as a background process
- Start watchr as a background process
- Start passenger
#!/bin/bash cd ~/path/to/my/working-directory rackup faye.ru -s thin -E production & watch refresh.watchr & passenger start
This works fine but the problem comes when I need to restart all these processes. I first have to track down all their process ids and then kill them before running start-dev again. Therefore I would like to:
4) Type a short command like «restart-dev» which tracks down the processes I have previously run in the background and then kill them before running «start-dev» again. It needs to be able to not throw an error if I have shut down any of these manually and not accidentally shutdown an incorrect process.
Restart process on linux by its pidn number with kill command, how?
Well, I want to be able to restart processes on linux and so I looked into kill manpages for that. Apparently kill -l would list all the signals I could send to a process to do what I need, which are:
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX
I thought that I would get the desired effect by using SIGSTOP signal (number 19) and then SIGCONT signal (number 18) like this:
kill -19 $PID_NUMBER # It stops! nice, we are reaching just what we wanted. kill -18 $PID_NUMBER # Ok. it continues to death. that isn't funny though.
I also tried with signal number 1 : SIGHUP with pretty much the same results, am I missing something? Does anyone know what I need to reach what I want?
2 Answers 2
There is no “restart” signal. You need to record the environment (environ, cwd, cmdline, security context…) from /proc/ and manually start the process again.
SIGHUP is close, but it is only used by convention to ask the program to reload its settings.
SIGHUP is the ‘abbreviation’ of Hang Up. Like a telephone conversation. It is typically used in such situation where the running program is prepared to receive such a signal and restart when it receives the HUP. It doesn’t really restart either. In most cases it just resets its internal state, without really stopping. So, there is no way to restart a program unless the program is written to respond to those signals.
@jcoppens If i have the exact copy of some /proc/[pid] can I recreate it by wrapping it into exec() or something?
I suspect that there are some parameters in the /proc/pid, which might raise alarms. But I’m not an expert on that part.