Skip to content

Docker: Ensure that cmd.sh terminates its child processes when the parent script closes#5172

Merged
lastzero merged 2 commits into
photoprism:developfrom
keif888:ChildProcessCleanup
Aug 24, 2025
Merged

Docker: Ensure that cmd.sh terminates its child processes when the parent script closes#5172
lastzero merged 2 commits into
photoprism:developfrom
keif888:ChildProcessCleanup

Conversation

@keif888

@keif888 keif888 commented Aug 24, 2025

Copy link
Copy Markdown
Contributor

Description

These changes improves the closing of the PhotoPrism bash command shell, so that s6 can stop PhotoPrism within the container.

The following command will stop the PhotoPrism web service, leaving the container up.

docker exec -ti photoprism /package/admin/s6/command/s6-svc -d /run/service/photoprism

The following command will start the PhotoPrism web service, leaving the container up.

docker exec -ti photoprism /package/admin/s6/command/s6-svc -u /run/service/photoprism

or if using Kubernetes, the following will stop the PhotoPrism web service (change the pod id to match yours):

kubectl exec -n photoprism-app photoprism-66c9c7cc59-96qhs  --  /package/admin/s6/command/s6-svc -d /run/service/photoprism

Acceptance Criteria

  • Feature has been tested in container
  • New features or enhancements are fully implemented and do not break existing functionality, so that they can be released at any time without requiring additional work

Testing Evidence

Please note that testing was conducted on Kubernetes, so I haven't tested the docker commands above, which are based on https://docs.photoprism.app/getting-started/docker/#examples

The shutdown of PhotoPrism below was triggered from the kubectl command above.
The startup of PhotoPrism below was triggered from a kubectl command (the -d changed to a -u).
Prior to this fix, PhotoPrism would not shut down.

time="2025-08-24T10:22:34Z" level=info msg="server: listening on 0.0.0.0:2342 [8.981383ms]"
time="2025-08-24T10:22:51Z" level=info msg="shutting down workers"
Terminated
Terminated
time="2025-08-24T10:22:51Z" level=info msg="shutting down..."
time="2025-08-24T10:22:51Z" level=info msg="server: shutting down"
time="2025-08-24T10:22:51Z" level=info msg="server: shutdown complete"
started 250821-postgres as uid 1000 (amd64-prod)
init: account with the user id 1000 already exists
Problems? Our Troubleshooting Checklists help you quickly diagnose and solve them:
https://docs.photoprism.app/getting-started/troubleshooting/
file umask....: "0002" (u=rwx,g=rwx,o=rx)
home directory: /photoprism
assets path...: /opt/photoprism/assets
storage path..: /photoprism/storage
config path...: default
cache path....: default
backup path...: /photoprism/storage/backups
import path...: /photoprism/import
originals path: /photoprism/originals
running as uid 1000
/opt/photoprism/bin/photoprism start
time="2025-08-24T10:24:17Z" level=info msg="server: started as pid 1887"
time="2025-08-24T10:24:17Z" level=info msg="webdav: shared /originals/"
time="2025-08-24T10:24:17Z" level=info msg="webdav: shared /import/"
time="2025-08-24T10:24:17Z" level=info msg="server: disabled auto tls"
time="2025-08-24T10:24:17Z" level=info msg="server: listening on 0.0.0.0:2342 [7.384532ms]"

Comment thread scripts/dist/cmd.sh
#!/usr/bin/env bash

# clean up child processes via trap
trap 'ps -p $$ -o pgid= | xargs pgrep -A -g | xargs kill ;exit' EXIT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! That looks like kung fu! How did you come up with this? :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trap command from google, but the rest through trial and error.
ps $$ -o pgid= returns the group id for the current process (which is the bash script).
xargs pgrep -A -g takes the group id, and searches out all the process id's associated with it, excluding pgrep.
xargs kill takes the output from pgrep and sends a SIGTERM to them to end gracefully.

@lastzero lastzero added enhancement setup Docker images, Helm charts, scripts, and Compose examples merged Changes are merged, but may require further testing labels Aug 24, 2025
@lastzero lastzero moved this to Development 🐝 in Roadmap πŸš€βœ¨ Aug 24, 2025
@lastzero lastzero changed the title Ensure that child processes are terminated when parent script closes Docker: Ensure that cmd.sh terminates its child processes when the parent script closes Aug 24, 2025
@lastzero lastzero merged commit e80c1e1 into photoprism:develop Aug 24, 2025
2 checks passed
@keif888 keif888 deleted the ChildProcessCleanup branch August 24, 2025 10:47
@graciousgrey graciousgrey moved this from Development 🐝 to Preview 🐳 in Roadmap πŸš€βœ¨ Oct 13, 2025
@lastzero lastzero moved this from Preview 🐳 to Release 🌈 in Roadmap πŸš€βœ¨ Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged Changes are merged, but may require further testing setup Docker images, Helm charts, scripts, and Compose examples

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants