Skip to content

Instantly share code, notes, and snippets.

@speratus
Created May 29, 2020 13:46
Show Gist options
  • Save speratus/b2b65f647629ec088e4ae244a42edcf1 to your computer and use it in GitHub Desktop.
Save speratus/b2b65f647629ec088e4ae244a42edcf1 to your computer and use it in GitHub Desktop.
An example of a systemd service for starting and stopping a postgres server installed from Homebrew.
#!/bin/bash
# Run these commands after creating your service file to get it installed and running.
# Once the service file is finished, copy it to the correct directory.
# In Ubuntu based systems, this is usually /etc/systemd/system.
sudo cp postgres.service /etc/systemd/service
# Now try starting the service:
sudo systemctl start postgres.service
# If you get an error message saying that the units have changed, you may have to run this command to make
# systemd recognize the new service file.
sudo systemctl daemon-reload
# Once the service is running, stop it by running this command:
sudo systemctl stop postgres.service
# To make the postgres server boot with the machine, run this command:
sudo systemctl enable postgres.service
# Run this command to check the status of the postgres service. If there's a small green dot near
# the beginning of the log entry, then the service is running.
systemctl status postgres.service
[Unit]
# Give a short description of the service. It shows up in systemd logs
Description=Starts the postgresql server
# Describe the service after which this one can start
After=network.target
[Install]
# Tell systemd when the service should start. Unlike the After line above, this line
# tells systemd when it should start this service.
WantedBy=multi-user.target
[Service]
# Postgres can't run as root, the following line tells systemd which user to use for running it.
User=postgres
# Describes the type of process. Forking means that the process started by ExecStart spawns another process
# before it finishes executing.
Type=forking
# The command systemd has to run to start the service. All paths in systemd files have to be absolute, hence
# the verbosity of the following lines.
ExecStart=/home/linuxbrew/.linuxbrew/bin/pg_ctl -D /home/linuxbrew/.linuxbrew/var/postgres start
# The command systemd will run to stop the service.
ExecStop=/home/linuxbrew/.linuxbrew/bin/pg_ctl -D /home/linuxbrew/.linuxbrew/var/postgres stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment