15

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

4 Answers4

15

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
Andres
  • 274
  • 2
  • 4
13

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.

Palo
  • 103
  • 5
5

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.

user9517
  • 114,104
  • 20
  • 206
  • 289
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

Shree29
  • 141
  • 5