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.

1.7 KiB

Quick Guide for auto start service

  • Reference

  • Reference 2

  • DZone

  • create service script in /etc/systemd/system/myserver.service

    [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

    [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

    #!/bin/sh
    sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.yml
    
  • start service

    
    $ sudo systemctl start myserver.service
    
    // or 
    $ sudo systemctl start myserver
    
  • to restart

    $ sudo systemctl restart myserver.service
    
  • automatically start on boot

$ sudo systemctl enable myserver.service
  • check active

    $ systemctl is-active myserver.service
    
  • All services status

    $ service  --status-all