在Ubuntu 16.04上创build守护进程

我在PHP中开发了一个爬网程序,用于parsing具有特定标头的URL,并将所有内容的URL放入队列中。 它工作正常。

我在ubuntu 14.04中开发了这个代码,并且把.conf文件放在/ etc / init文件夹中,内容如下:

# 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 

它在Ubuntu 14.04中工作正常,我可以使用“sudo服务爬虫启动”和“sudo服务爬虫停止”来启动和停止守护进程

现在在生产环境中,我有一个Ubuntu 16.04服务器,我把相同的代码放在同一个文件夹中,但是当我尝试启动服务时,收到消息“无法启动crawler.service.Unit crawler.service not found”

你能帮我一下吗?

问候

添加到@Juanjo AguilellaMarés的答案是,一旦您将脚本复制/链接到/etc/systemd/system ,您可能希望在服务器启动时自动启动它:

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

来源数字海洋

不要以root身份运行它也是一个好主意。 只需更改脚本中的user行即可:

 [Service] User=some_user 

14.04的init系统是新贵的。 系统为16.04的init系统。 你应该把你的upstart脚本转换成systemd单元文件。 还有很多其他资源可用。

我解决了这个问题:

a)用以下代码在/ etc / systemd / system中创build一个文件crawler.service:

 [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 

我的bash文件包含与此代码相同的php文件并行执行的不同代码:

 #!/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 } 

执行之间的睡眠是保存服务执行速度这个问题所必需的。

如果您对该解决scheme有任何build议,请发表评论,我没有很多bash文件和systemd文件的经验,但目前工作正常。