> For the complete documentation index, see [llms.txt](https://www.shellspells.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.shellspells.net/linux/system-ops/services/create-a-service.md).

# Create a Service

## <mark style="color:red;">Systemd</mark>

Systemd services are defined in unit files with the .service extension. \
Here's a basic structure:

```
[Unit]
Description=Your service description
After=network.target

[Service]
Type=simple
User=your_username
WorkingDirectory=/path/to/service/directory
ExecStart=/path/to/service/executable

[Install]
WantedBy=multi-user.target
```

Replace placeholders: \
Description: Short description of your service. \
After: Specify dependencies (e.g., network services). \
User: User running the service. \
WorkingDirectory: Service's working directory. \
ExecStart: Path to the service executable. \
WantedBy: Target unit to enable automatic startup.

Save the file: \
Save it as \[service\_name].service in /etc/systemd/system/.

Reload and enable:

```bash
sudo systemctl daemon-reload
```

```bash
sudo systemctl enable <service_name>.service
```

Start the service:

```bash
sudo systemctl start <service_name>.service.
```

## <mark style="color:red;">SysV</mark>&#x20;

SysV services are typically shell scripts located in /etc/init.d/. \
Here's a basic example:

```
#!/bin/bash

start() {
  # Start your service here
  /path/to/service/executable &
}

stop() {
  # Stop your service here
  pkill -f /path/to/service/executable
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0
```

Make the script executable:

```bash
chmod +x /etc/init.d/<service_name>
```

Start/stop the service:

```bash
/etc/init.d/<service_name> start/stop
```

Optional: Enable automatic startup by linking the script to /etc/rc?.d/S (depending on your runlevel).

## <mark style="color:red;">Upstart</mark>

Upstart uses job files with the .conf extension usually located in /etc/init.

Here's a basic example:

```
description "Your service description"

start on started network-interfaces
script
  # Start your service here
  /path/to/service/executable &
end script

pre-stop script
  # Stop your service here
  pkill -f /path/to/service/executable
end script
```

Save the file: Save it as \[service\_name].conf in /etc/init/.

Reload and start:

```bash
sudo initctl reload 
```

```bash
sudo start <service_name>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.shellspells.net/linux/system-ops/services/create-a-service.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
