systemd

How to setup a daemon/service in Debian

As a web developer you surely have tasks you have to keep running all the time your server is on, but executing the commands every time can be very tiring and tedious. Debian provides the service feature to execute these commands automatically.
In our case we had to keep running an Ionic simulator server at all times. So this solution came to be the best one for our purposes.

First you have to create a Bash file. Our bash file is located in the project folder, we named it project.sh


    cd /path/to/project
    ionic serve

Note that if you need to execute commands within specific directories, you first have to go to said directory, this will be further discussed later. In our case we had to go to our project directory, hence the first “cd” command line.

With our bash file created, we have to create a service file:

Let’s create a file called /etc/systemd/system/project.service:

    [Unit]
    Description=Service for keeping the PROJECT Ionic server on

 

    [Service]
    Type=oneshot
    ExecStart=/bin/bash /path/to/project/project.sh

    [Install]
    WantedBy=multi-user.target

 

The important thing about this service file is that it doesn’t execute the .sh file on the specified directory. Since it is a bash script file, it is run on /bin/bash. That’s the reason you must add the cd command to the project path before executing the actual commands.
 

That’s it. We can now start the service:

    $ systemctl start project

And automatically get it to start on boot, this is useful for when the main server restarts:

    $ systemctl enable service

And now the Ionic server is always running and can be accessed anytime through the server address.