注册 登录
编程论坛 PHP技术论坛

这段代码如何通过PHP脚本运行,请给个示例

kongjun 发布于 2022-10-26 08:16, 1090 次点击
想做成脚本定时运行并且让网页调用,希望高手给改成PHP脚本,先谢谢!(以下希望弄成2个脚本,我好参照)
第一脚本
rm /home/hts/xmltv.log
rm /home/hts/epg.xml
touch /home/hts/xmltv.log
echo "Download starting..." >> /home/hts/xmltvlog/xmltv.log
echo "Time: $(date)" >> /home/hts/xmltvlog/xmltv.log
wget -O /home/hts/epg.xml https://
cat /home/hts/epg.xml | sudo socat - UNIX-CONNECT:/home/hts/.hts/tvheadend/epggrab/xmltv.sock
echo "" >> /home/hts/xmltv.log
echo "Time: $(date)" >> /home/hts/xmltv.log
echo "EPG Updated successfully." >> /home/hts/xmltv.log

第二个脚本
cat /home/hts/epg.xml | sudo socat - UNIX-CONNECT:/home/hts/.hts/tvheadend/epggrab/xmltv.sock
2 回复
#2
阳光上的桥2023-01-13 11:15
最简单的方法是使用system,每一行修改为:
system('原来的行内容');

例如:
system('rm /home/hts/xmltv.log');
system('rm /home/hts/epg.xml');
system('touch /home/hts/xmltv.log');

注意事项是引号需要处理。

可以进一步把某些命令直接翻译为PHP的相应语句,例如:
shell命令:
rm /home/hts/xmltv.log
PHP语句:
unlink('/home/hts/xmltv.log');
#3
阳光上的桥2023-01-16 11:51
添加日志的shell语句:
echo "Download starting..." >> /home/hts/xmltvlog/xmltv.log

可以这样用PHP代码实现:
$fp=fopen('/home/hts/xmltvlog/xmltv.log', 'a');
fputs($fp, "Download starting...\n");
fclose($fp);

其它的wget、sudo等语句就采用system吧,否则太复杂了。
1