Ubuntu – Create daemon on ubuntu 16.04

daemonUbuntuubuntu-16.04

I developed a crawler in PHP that parse an URL with specific headers and put all URLs of content in the queue. It works fine.

I developed this code in a ubuntu 14.04 and I put a .conf file in /etc/init folder with this content:

# Info
description "Warm the varnish to get the list of products"
author      "Juanjo Aguilella"

# Events
start on startup
stop on shutdown

# Automatically respawn
respawn
respawn limit 100 5

# Run the script
# Note, in this example, if your PHP script return
# the string "ERROR", the daemon will stop itself.
script
    [ $(exec /usr/bin/php -f /var/www/crawler.php) = 'ERROR' ] && ( stop; exit 1; )  
end script

It works fine in Ubuntu 14.04 and I can start and stop the daemon using "sudo service crawler start" and "sudo service crawler stop"

Now in production environment I have a Ubuntu 16.04 server and I put the same code on the same folder but when I try to start the service I receive the message "Failed to start crawler.service. Unit crawler.service not found"

Can you give me any help about it?

Regards

Best Answer

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