4

I have a init script - /etc/init.d/myservice, which starts some processes, including a CLI process. And from this CLI process, I am adding a command which can be used to restart the service (restart ourselves - kind of).

What I tried - from a script (that is spawned from the CLI command), ran sudo service myservice restart, where restart is stop;start. But, this doesn't work! i.e, even though the stop happens, by the time start part is executing, the script terminates (not with any signals, but plain EXIT).

I tried combinations of nohup, disown etc, but didn't seem to work! I am not sure how to complete the restart without getting terminated - any pointers, or workarounds to debug further ?

To summarize:

  • myservice -> spawns many processes, including CLI
  • CLI -> has a restart command, which invokes a script
  • script does sudo service myservice restart

--More info--

I'm on Ubuntu 12.04, i.e, upstart

5
  • 1
    What's the OS, init system (upstart, systemd, sysvinit...)? Aug 26, 2014 at 13:01
  • How is the "stop" action done? Does it help if you add a trap '' TERM in your restart script? Aug 26, 2014 at 13:02
  • @StéphaneChazelas added more info, stop does a usual kill by process name. If I add trap..TERM wouldn't it affect other ways of start/stop - like, from shell if one does sudo myservice stop ?
    – Ani
    Aug 26, 2014 at 13:09
  • would your kill by process name (it's better to combine that with pid files to avoid problems or use cgroups as systemd) not possibly kill your script? Aug 26, 2014 at 13:16
  • I don't understand cgroups as systemd :(. The script name is not in the list of processes to be stopped.
    – Ani
    Aug 26, 2014 at 13:24

2 Answers 2

1

I had searched for quite some time, but I found one solution here to this SuperUser Q&A titled: How do I detach a process from Terminal, entirely?, after posting my question here.

| at now
0

Before starting the service again you might want to wait for a few seconds so that it stops properly. You can delay your script with the following line

# waiting for 5 seconds
sleep 5

You can see more details here - http://www.cyberciti.biz/faq/linux-unix-sleep-bash-scripting/

1
  • the stop is happening, but at start the script terminates.
    – Ani
    Aug 26, 2014 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .