Skip to content

Added cron via env variables #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ To access the PostgreSQL logs you can use `docker exec`. For example:
docker exec -it postgresql tail -f /var/log/postgresql/postgresql-9.4-main.log
```

## Cron

To schedule cron jobs in the container add variables named `POSTGRES_CRON_X` :

```bash
docker run --name postgresql -itd --restart always \
--publish 5432:5432 \
--volume /srv/docker/postgresql:/var/lib/postgresql \
--env "POSTGRES_CRON_1=* * * * * postgres date >> /tmp/crond.log 2>&1"
sameersbn/postgresql:9.5-2
```

# UID/GID mapping

The files and processes created by the container are owned by the `postgres` user that is internal to the container. In the absense of user namespace in docker the UID and GID of the containers `postgres` user may have different meaning on the host.
Expand Down
2 changes: 2 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ if [[ -z ${1} ]]; then

configure_postgresql

create_crond

echo "Starting PostgreSQL ${PG_VERSION}..."
exec start-stop-daemon --start --chuid ${PG_USER}:${PG_USER} \
--exec ${PG_BINDIR}/postgres -- -D ${PG_DATADIR} ${EXTRA_ARGS}
Expand Down
22 changes: 22 additions & 0 deletions runtime/functions
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ exec_as_postgres() {
sudo -HEu ${PG_USER} "$@"
}

create_crond() {
# Provide POSTGRES_CRON_* via environment variable
rm -rf /etc/cron.d/${PG_USER}
for item in `env`; do
case "$item" in
POSTGRES_CRON*)
ENVVAR=`echo $item | cut -d \= -f 1`
printenv $ENVVAR >> /etc/cron.d/${PG_USER}
;;
esac
done

if [ -f /etc/cron.d/${PG_USER} ]; then
sed -i "s/^session required pam_loginuid.so/#session required pam_loginuid.so/g" /etc/pam.d/cron
echo "Running cron(s) "
echo "***************************************************"
cat /etc/cron.d/${PG_USER}
echo "***************************************************"
cron
fi
}

map_uidgid() {
USERMAP_ORIG_UID=$(id -u ${PG_USER})
USERMAP_ORIG_GID=$(id -g ${PG_USER})
Expand Down