1

I have a bash script that calls liquidsoap like so

/bin/sh -c "echo \$\$ > \"${sdir}/pid/${sfile}.pid\" && exec liquidsoap \"${sdir}/liq/${sfile}.liq\" >/dev/null 2>&1 || rm \"${sdir}/pid/{$sfile}.pid\"" &

(For readability, it might look like this with variables filled in)

/bin/sh -c "echo \$\$ > \"/radio/pid/station.pid\" && exec liquidsoap \"/radio/liq/station.liq\" >/dev/null 2>&1 || rm \"/radio/pid/station.pid\"" &

In PHP, the script is called with

return shell_exec("{$this->streamBase}/scripts/{$this->streamName} start config {$stationConfig}");

My problem is, I just had to restart Apache, and when I did, it also killed the liquid soap instances. I would like to get it to run fully independent of Apache such that I could restart Apache and they would keep running.

I'm not sure how I can achieve that.

EDIT: I've tried changing

/bin/sh -c "echo \$\$ > \"${sdir}/pid/${sfile}.pid\" && exec liquidsoap \"${sdir}/liq/${sfile}.liq\" >/dev/null 2>&1 || rm \"${sdir}/pid/{$sfile}.pid\"" &

to

(/bin/sh -c "echo \$\$ > \"${sdir}/pid/${sfile}.pid\" && exec liquidsoap \"${sdir}/liq/${sfile}.liq\" >/dev/null 2>&1 || rm \"${sdir}/pid/{$sfile}.pid\"" & ) &
and
nohup /bin/sh -c "echo \$\$ > \"${sdir}/pid/${sfile}.pid\" && exec liquidsoap \"${sdir}/liq/${sfile}.liq\" >/dev/null 2>&1 || rm \"${sdir}/pid/{$sfile}.pid\"" &

Neither keep liquidsoap running if I restart (or stop/start) Apache. When Apache stops, so do those processes.

Trel
  • 323
  • 4
  • 15
  • Possible duplicate of [Asynchronous shell exec in PHP](http://stackoverflow.com/questions/222414/asynchronous-shell-exec-in-php) – John C Sep 24 '16 at 05:29
  • No, what that's asking for I've done already, I'm asking specifically about how to make the spawned processed detached from the parent. – Trel Sep 24 '16 at 06:06
  • Use `nohup`? By the way, `/bin/sh` is not the same as `bash`. – cdarke Sep 24 '16 at 06:42
  • I'll take a look at nohup (/bin/sh is the line that launches the program, but the line is in a large bash script) – Trel Sep 24 '16 at 13:40
  • What you want is to daemonize your script. See [Best way to make a shell script daemon?](http://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-script-daemon) – strobelight Sep 24 '16 at 20:15
  • No, I don't want to daemonize my script. My script is purely a way to launch and send commands to a different program. My script should be exiting, not left running. – Trel Sep 26 '16 at 03:20
  • @strobelight to clarify, what I want to be running in the background and killed only when I kill it (rather than when its parent ends) is liquidsoap, which is NOT my script, but what my script launches. – Trel Sep 26 '16 at 13:49
  • Try putting the `nohup` immediately before liquidsoap. i.e. `... exec nohup liquidsoap ...` – ccarton Sep 26 '16 at 19:57
  • @ccarton no luck. The liquidsoap process still gets killed when I restart Apache. – Trel Sep 27 '16 at 04:33
  • write a script which [daemonizes itself](http://stackoverflow.com/questions/3430330/best-way-to-make-a-shell-script-daemon) and execs liquidsoap – strobelight Sep 28 '16 at 14:24
  • @strobelight I've already said I do not want my script to daemonize itself, I want the script to exit with an exit code. – Trel Sep 29 '16 at 14:32

1 Answers1

0

for an exit code to be propogated up the chain the parents and grandparents must exist, and if you kill the grandparent, aka apache, yes, you kill the children and grandchildren unless they leave the family and become daemons.

strobelight
  • 267
  • 2
  • 7
  • Yes, and that's what I'm trying to do. My script launches liquidsoap (as a daemon) and writes the PID to a file. Then my script (which is NOT a daemon) returns an exit code. The goal is to have liquidsoap (not my script) remain running if apache restarts. The part I can't get to work is daemonizing liquidsoap when I launch it. – Trel Sep 30 '16 at 23:49