ubuntu php守护,在Ubuntu 16.04上创建守护程序

问题描述

我用PHP开发了一个搜寻器,该搜寻器解析具有特定标头的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”。

您能给我任何帮助吗?

问候

最佳回答

添加到@Juanjo AguilellaMarés答案中,然后将脚本复制/链接到/etc/systemd/system,您可能想在服务器启动时自动启动它:

sudo systemctl daemon-reload

sudo systemctl enable my_service.service

sudo systemctl start my_service.service

最好不要以超级用户身份运行它。只需在脚本上更改user行即可:

[Service]

User=some_user

次佳回答

我解决了这个问题:

a)使用以下代码在/etc /systemd /system中创建文件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

}

执行之间的睡眠对于保存服务执行如此之快的问题是必需的。

如果您对解决方案有任何建议,请发表评论,我对bash文件和systemd文件没有很多经验,但是目前效果很好。

第三种回答

第四种回答

1]。要创建服务,请转到/etc /systemd /system /

2]。创建一个serviceName文件,例如chatSocket.service

3]。按以下内容将内容归档

[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]。通过点击以下命令来重新加载配置:

sudo systemctl daemon-reload

5]。默认情况下启用服务,因此当系统启动服务将自动启动时:

sudo systemctl enable my_service.service

6]。使用以下命令启动服务:

sudo systemctl start my_service.service

参考资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值