You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
1.7 KiB
Markdown

# Quick Guide for auto start service
* [Reference](https://medium.com/@benmorel/creating-a-linux-service-with-systemd-611b5c8b91d6)
* [Reference 2](https://linuxconfig.org/how-to-create-systemd-service-unit-in-linux)
* [DZone](https://dzone.com/articles/run-your-java-application-as-a-service-on-ubuntu)
* create service script in `/etc/systemd/system/myserver.service`
```ini
[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=centos
ExecStart=/usr/bin/env php /path/to/server.php
[Install]
WantedBy=multi-user.target
```
* sample java application
```sh
[Unit]
Description=My Webapp Java REST Service
[Service]
User=ubuntu
# The configuration file application.properties should be here:
#change this to your workspace
WorkingDirectory=/home/ubuntu/workspace
#path to executable.
#executable is a bash script which calls jar file
ExecStart=/home/ubuntu/workspace/my-webapp
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
* script `/home/ubuntu/workspace/my-webapp`. Change to executable `sudo chmod u+x my-webapp`
```sh
#!/bin/sh
sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.yml
```
* start service
```sh
$ sudo systemctl start myserver.service
// or
$ sudo systemctl start myserver
```
* to restart
```sh
$ sudo systemctl restart myserver.service
```
* automatically start on boot
```sh
$ sudo systemctl enable myserver.service
```
* check active
```sh
$ systemctl is-active myserver.service
```
* All services status
```sh
$ service --status-all
```