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

在后台运行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

  • 写回答

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 <kbd>ctrl</kbd>-<kbd>a</kbd>,<kbd>d</kbd> 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

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
  • ¥15 有人会思科模拟器嘛?
  • ¥30 遇到一个的问题,请教各位
  • ¥20 matlab报错,vflux计算潜流通量
  • ¥15 我该如何实现鼠标按下GUI按钮时就执行按钮里面的操作的方法
  • ¥15 关于#硬件工程#的问题:我这边有个锁相环电路没有效果
  • ¥15 20款 27寸imac苹果一体机装win10后,蓝牙耳机和音响放歌曲卡顿断断续续.
  • ¥15 VB.NET 父窗体调取子窗体报错
  • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形
  • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?