AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 785502
Accepted
Juanjo Aguilella Marés
Juanjo Aguilella Marés
Asked: 2016-06-23 01:32:46 +0800 CST2016-06-23 01:32:46 +0800 CST 2016-06-23 01:32:46 +0800 CST

在 ubuntu 16.04 上创建守护进程

  • 772

我用 PHP 开发了一个爬虫,它解析带有特定标头的 URL,并将所有内容的 URL 放入队列中。它工作正常。

我在 ubuntu 14.04 中开发了这段代码,并在 /etc/init 文件夹中放置了一个 .conf 文件,其中包含以下内容:

# 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 service crawler start”和“sudo service crawler stop”来启动和停止守护进程

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

你能给我任何帮助吗?

问候

ubuntu daemon ubuntu-16.04
  • 4 4 个回答
  • 76643 Views

4 个回答

  • Voted
  1. Best Answer
    Andres
    2017-05-23T08:28:05+08:002017-05-23T08:28:05+08:00

    添加到@Juanjo Aguilella Maré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
    
    • 15
  2. Juanjo Aguilella Marés
    2016-06-24T02:18:44+08:002016-06-24T02:18:44+08:00

    我解决了这个问题:

    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 文件方面没有很多经验,但目前工作正常。

    • 13
  3. user9517
    2016-06-23T01:51:18+08:002016-06-23T01:51:18+08:00

    14.04 的初始化系统是新贵。16.04 的初始化系统是 systemd。您应该将 upstart 脚本转换为 systemd单元文件。还有很多其他可用资源。

    • 5
  4. Shree29
    2018-02-19T23:08:22+08:002018-02-19T23:08:22+08:00

    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

    • 4

相关问题

  • 无法通过 Ubuntu VPN 访问外部网络

  • ubuntu apt-get upgrade - 如何在 shell 中单击确定?

  • VirtualBox 上 Ubuntu 的访客优化技巧 [关闭]

  • 外部硬盘上的 virtualbox 虚拟硬盘驱动器(Vista 主机上的 ubuntu 客户机)

  • 如何在 Ubuntu 上挂载 LVM 分区?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve