8

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?

neziric
  • 869
  • Can you clarify what you mean? You want to restart a running process by passing the PID of that process? – Roger Light Dec 01 '10 at 13:18
  • 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?) – neziric Dec 01 '10 at 13:24
  • 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. – Roger Light Dec 01 '10 at 14:05
  • i tried to avoid that, but for this program it seems i had to do it. thanks everyone – neziric Dec 01 '10 at 14:44

2 Answers2

11

Killing or Reconfiguring a Daemon without Restarting

kill -HUP 1721

Restarts the process 1721 by sending the hangup signal.

killall -HUP inetd

Causes the daemon to reload its config file by sending the hangup signal.

killall -1 inetd 

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.

Reference: http://www.comptechdoc.org/os/linux/usersguide/linux_ugprocesses.html

aneeshep
  • 30,321
  • Certainly worth checking, but you can't depend on this behaviour for any given program. – Roger Light Dec 01 '10 at 14:00
  • 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 – neziric Dec 01 '10 at 14:01
  • It only works for those applications which explicitly setup a SIGHUP signal handler. It is not a general solution to restart processes. – João Pinto Dec 01 '10 at 15:25
0

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

binW
  • 13,034
  • I strongly doubt you mean "&" here. Regardless, that doesn't work. – Roger Light Dec 01 '10 at 13:57
  • Can u plz try now. cmdline in /proc has spaces replaced with null charcters. i have added a command to replace them back. also use of '&' between commands was a typo which i have corrected – binW Dec 02 '10 at 06:41