doubingqi5829
2017-04-13 06:17
浏览 211
已采纳

在后台运行php脚本,永远不会死linux

I have a php script that use Mosquitto client. I am using putty SSH to access to the server. I want to run the php script in background continously even when I disconnect from putty. I have tried screen and nohup but it stop when closing putty window

Thank you

图片转代码服务由CSDN问答提供 功能建议

我有一个使用Mosquitto客户端的php脚本。 我使用putty SSH访问服务器。 我想连续在后台运行php脚本,即使我从putty断开连接。 我已经尝试了屏幕和nohup,但是在关闭putty窗口时停止了

谢谢

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drqvsx1228 2017-04-13 06:49
    已采纳

    A simple solution is

    nohup php script.php &
    

    So you run script in background and disconnect the process from the terminal. If it doesn't help, try disown command after it. There is a good answer with detailed explanation of differences between these commands.

    To get full control of your script, a good choice would be a System V init script. There is a template at https://github.com/fhd/init-script-template, which you can use.

    Copy template to /etc/init.d directory and rename it. In template you need to change variables:

    dir="/your/working/directory"
    cmd="nohup php script.php"
    user="your user"
    

    Doing that you will be able to control your script by

    /etc/init.d/your_script start
    /etc/init.d/your_script stop
    

    Make sure you have permissions to write in /var/log/ and /var/run/, or run script as sudo(leave user="" empty)

    点赞 打赏 评论
  • drmcm84800 2017-04-13 08:25

    Using screen is a much better solution than nohup.

    screen let's you name sessions and rejoin them later so you don't need to resort to using ps to find your backgrounded applications

    You can start a named screen session like this

    screen -S [session name]
    

    Which you can detach from with ctrl-a,d then reattach with

    screen -r [session name]
    

    You can also start a session with a command in the background with

    screen -dmS [session name] [command]
    

    https://www.gnu.org/software/screen/manual/screen.html

    点赞 打赏 评论

相关推荐 更多相似问题