Create daemon on ubuntu 16.04

Solution 1:

Adding to @Juanjo Aguilella Marés answer, and once you have copied/linked your script to /etc/systemd/system, you may want to automatically start it when the server starts:

sudo systemctl daemon-reload
sudo systemctl enable my_service.service
sudo systemctl start my_service.service

Source Digital Ocean

It is also a good idea not to run it as root. Just change the user line on your script:

[Service]
User=some_user

Solution 2:

I solved the problem:

a) Create a file crawler.service in /etc/systemd/system with this code:

[Unit]
Description=Crawler cache Service
After=network.target

[Service]
User=root
Restart=always
Type=forking
ExecStart=/var/www/execute.sh

[Install]
WantedBy=multi-user.target

my bash file contains a diferent executations on parallel to the same php file with this code:

#!/bin/sh
php /var/www/tiendas.local.mediamarkt.es/crawler.php
sleep 0.1
{
    php /var/www/tiendas.local.mediamarkt.es/crawler.php
}&
sleep 0.2
{
    php /var/www/tiendas.local.mediamarkt.es/crawler.php
}&
sleep 0.3
{
    php /var/www/tiendas.local.mediamarkt.es/crawler.php
}&
sleep 0.4
{
    php /var/www/tiendas.local.mediamarkt.es/crawler.php
}

the sleep between execitions es necessary to save the problem about the execution so fast of the service.

If you have any suggestion about the solution, please comment, I dont have a lot of experience in bash files and systemd files, but at the moment works fine.


Solution 3:

The init system for 14.04 is upstart. The init system for 16.04 is systemd. You should convert your upstart script to a systemd unit file. There are plenty of other resources available too.


Solution 4:

1]. To create a service go to /etc/systemd/system/

2]. Create a file of serviceName e.g chatSocket.service

3]. Put content to file as given bellow

[Unit]
Description=Your PHP Daemon Service
#Requires=mysqld.service memcached.service #May your script needs mysql or other services to run.
#After=mysqld.service memcached.service

[Service]
User=root
Type=simple
TimeoutSec=0
PIDFile=/var/run/server.pid
ExecStart=/usr/bin/php -f /home/shrikant/workspace/app/Http/Controllers/server.php  2>&1> /dev/null #path to script
#ExecStop=/bin/kill -HUP $MAINPID
#ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

Restart=on-failure
RestartSec=42s

StandardOutput=null #If you don't want to make toms of logs you can set it null if you sent a file or some other options it will send all php output to this one.
StandardError=/home/shrikant/workspace/app/Http/Controllers/chatSocket.log #path to error log file
[Install]
WantedBy=default.target

4]. Reload configuration by hitting:

sudo systemctl daemon-reload

5]. Enable service by default so when system start service will automatically start:

sudo systemctl enable my_service.service

6]. Start your service by using command below:

sudo systemctl start my_service.service