duan5731 2014-07-20 07:12
浏览 115
已采纳

如何使用system()从c程序调用php脚本?

I am writing a c program that will call a php script every few seconds in the background. So when you first call this program, it will create a child process through fork, and then exit the parent process, while the child process will be in an infinite loop. So far inside that infinte loop, I have the line system("echo hello >> daemon.txt") inside hte while loop, and it updates accordingly, so I know that the process is working. However when I call my script, it seems like it does not execute.

My php script timeChecker.php

#!/usr/bin/php <?php $handle =fopen("hello","a"); fwrite($handle, "boo "); fclose($handle); ?>

When called from commandline using either php timeChecker.php or ./timeChecker.php, the code executes as it should.

However, after trying multiple methods of calling the command such as system("php timeChecker.php"), system("/usr/bin/php timeChecker.php"), system("/usr/bin/php var/www/timeChecker.php"), system("/var/www/timeChecker.php") and all variations of that, the code still does not execute. Is there something simple that I am doing wrong? Or should I simply use a different method of checking the script?

  • 写回答

1条回答 默认 最新

  • dtm41506 2014-07-20 09:53
    关注

    As you are suggesting in your question, the php script is in "/var/www". Do you have the privileges to execute in that directory?

    test is the c program calling the script through system(). I've made it work this way:

        sudo mkdir /var/www/script_dir
        sudo mv timeChecker.php /var/www/script_dir
        sudo mv test /var/www/script_dir
        sudo chown -R myusername:myusername /var/www/script_dir
    

    Execute test from the script_dir.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)